CodeGym /Courses /Docker SELF /Basic Docker Commands

Basic Docker Commands

Docker SELF
Level 10 , Lesson 1
Available

7.1 Commands for working with containers

Docker CLI (Command Line Interface) or just the command line interface. Here, you can type commands to manage containers, images, networks, and volumes. Sure, you can do a lot through the visual and simpler Docker Desktop interface, but getting familiar with the main Docker CLI commands will help you work more efficiently and make full use of what Docker offers. In this lecture, we'll go through the key Docker commands to help you get started.

The list of main commands is shown in the table:

Command Description
docker run Launch a new container
docker ps List running containers
docker stop Stop a running container
docker start Start a stopped container
docker restart Restart a container
docker rm Remove a stopped container
docker logs View container logs
docker exec Run a command inside a container

1. Launching a new container (docker run):

This command pulls the nginx image and runs it in detached mode (-d), assigning the container the name my_container.

Terminal


docker run -d --name my_container nginx

2. List running containers (docker ps):

Displays a list of all running containers. Use the -a flag to see all containers, including stopped ones.

Terminal


docker ps

3. Stopping a container (docker stop):

Stops the container named my_container.

Terminal


docker stop my_container

4. Starting a stopped container (docker start):

Restarts a previously stopped container.

Terminal


docker start my_container

5. Restarting a container (docker restart):

Stops and then immediately restarts the container.

Terminal


docker restart my_container

6. Removing a container (docker rm):

Removes a stopped container. The container must be stopped before removing it.

Terminal


docker rm my_container

7. Viewing container logs (docker logs):

Displays the container's logs, which is handy for debugging and monitoring.

Terminal


docker logs my_container

8. Running a command inside a container (docker exec):

Opens an interactive terminal inside the running container.

Terminal


docker exec -it my_container /bin/bash

7.2 Commands for Working with Images

A list of basic commands is shown in the table below:

Command Description
docker pull Download an image from Docker Hub
docker build Build an image from a Dockerfile
docker images List of local images
docker rmi Delete images
docker tag Assign a new tag to an image

1. Downloading an Image (docker pull):

Downloads the nginx image with the latest tag from Docker Hub.

Terminal


docker pull nginx:latest

2. Building an Image (docker build):

Builds a new image from the Dockerfile in the current directory (designated by a dot) and assigns it the tag my_image.

Terminal


docker build -t my_image .

3. Listing Local Images (docker images):

Displays a list of all locally saved images.

Terminal


docker images

4. Deleting Images (docker rmi):

Deletes an image with the tag my_image. Make sure the image is not being used by running containers.

Terminal


docker rmi my_image

5. Assigning a New Tag to an Image (docker tag):

Assigns the new image my_image a tag and name my_repo/my_image:latest, which makes version management easier.

Terminal


docker tag my_image my_repo/my_image:latest

7.3 Commands for Working with Networks

A list of basic commands is shown in the table:

Command Description
docker network ls List all Docker networks
docker network create Create a new network
docker network inspect View details about a network
docker network connect Connect a container to a network
docker network disconnect Disconnect a container from a network

1. List all networks (docker network ls):

Outputs a list of all available Docker networks.

Terminal


docker network ls

2. Create a new network (docker network create):

Creates a new network named my_network.

Terminal


docker network create my_network

3. View details about a network (docker network inspect):

Provides detailed information about the my_network, including connected containers.

Terminal


docker network inspect my_network

4. Connect a container to a network (docker network connect):

Connects the container my_container to the network my_network.

Terminal


docker network connect my_network my_container

5. Disconnect a container from a network (docker network disconnect):

Disconnects the container my_container from the network my_network.

Terminal


docker network disconnect my_network my_container

7.4 Commands for Working with Volumes

A list of basic commands is shown in the table:

Command Description
docker volume ls List all Docker volumes
docker volume create Create a new volume
docker volume inspect View volume details
docker volume rm Delete a volume

1. List all volumes (docker volume ls):

Displays a list of all volumes created in Docker.

Terminal


docker volume ls

2. Create a new volume (docker volume create):

Creates a new volume named my_volume.

Terminal


docker volume create my_volume

3. View volume details (docker volume inspect):

Provides detailed information about the volume my_volume, including its location and driver.

Terminal


docker volume inspect my_volume

4. Delete a volume (docker volume rm):

Deletes the volume named my_volume. Make sure that the volume is not used by any containers before deleting it.

Terminal


docker volume rm my_volume

3
Task
Docker SELF, level 10, lesson 1
Locked
Running and managing a container
Running and managing a container
3
Task
Docker SELF, level 10, lesson 1
Locked
Working with Docker images
Working with Docker images
3
Task
Docker SELF, level 10, lesson 1
Locked
Managing Docker Networks
Managing Docker Networks
3
Task
Docker SELF, level 10, lesson 1
Locked
Working with Docker Volumes
Working with Docker Volumes
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION