Docker – CLI Commands

This is a simple tutorial to learn basic Docker commands. To install Docker on Linux, please refer to this post: Docker – Install on Linux.

Getting Help

docker --help
docker image --help
docker image ls --help

Docker Management Commands

Management command were introduced in Docker engine v1.13

docker builder
docker config
docker container
docker engine
docker image
docker network
docker node
docker plugin
docker secret
docker service
docker stack
docker swarm
docker system
docker trust
docker volume

Docker Image Management Commands

docker image build # Build an image from a dockerfile
docker image history # Show the history
docker image import # Import the contents from a tarball
docker image inspect # Display detailed information
docker image load # Load an image from a tar file or STDIN
docker image ls # List images
docker image prune # Remove unused images
docker image pull # Pull an image from a registry
docker image push # Push an image to a registry
docker image rm # Remove one or more images
docker image save # Save one or more images to a tar file 
docker image tag # Create a tag TARGET_IMAGE


Docker Container Management Commands

docker container attach # Attach local standard input, output, and error streams to a running container
docker container commit # Create a new image from a container's changes
docker container cp # Copy files/folders between a container and the local file system
docker container create # Create a new container
docker container diff # Inspect changes to files or directories on a container's filesystem
docker container exec # Run a command in a running container
docker container export # Export a container's filesystem as a tar archive
docker container inspect # Display detailed information on one or more containers
docker container kill # Kill one or more running containers
docker container logs # Fetch the logs of a container
docker container ls # List containers
docker container pause # Pause all processes within one or more containers
docker container port # List port mappings or a specific mapping for the container
docker container prune # Remove all stopped containers
docker container rename # Rename a container
docker container restart # Restart one or more containers
docker container rm # Remove one or more containers
docker container run # Run a command in a new container
docker container start # Start one or more stopped containers
docker container stats # Display a live stream of container(s) resource usage statistics
docker container stop # Stop one or more running containers
docker container top # Display the running processes of a container
docker container unpause # Unpause all processes within one or more containers
docker container update # Update configuration of one or more containers
docker container wait # Block until one or more containers stop, then print their exit codes


Check all Docker System Information

docker system info

Pulling an Image from a Registry

docker image pull <image>  // docker image pull nginx

Checking Images

docker image ls  # list images
docker image ls --digests   # with a hash value
docker image inspect <image>   # return low-level information 

Creating/Running Containers

# publish to random ports & detach (run it in a background)
docker container run -p -d nginx  
# -p <host>:<container>
docker container run -p 8080:80 -d nginx

Options of docker container run command

  • –detach, -d: run container in background and print the container ID
  • –interactive, -i: keep STDIN open even if not attached
  • -p: publish a container’s port(s) to the host
  • –tty, -t: allocate a pseudo-TTY
  • –name: assign a name to the container

Checking Containers

docker container ls         # list running containers
docker container ls -a      # list all containers

Checking the container is working – 1

docker container inspect <container>  # detailed info

At the end of the detailed info, you can see the IP Address.

curl 172.17.0.2

It will return the default ngix home page.

Checking the container is working – 2

docker container port <container>         # port mappings

Open the browser with the public IP of the machine and the mapped port

http://3.101.79.152:8080

You can see the nginx welcome page.

Displaying the running processes of a container

docker container top <container>

Stopping/Starting the container

docker container stop <container>
docker container ls -a
docker container start <container>

Checking container logs and live resource stats

docker container logs <container>
docker container stats <container>    # Ctrl + C to exit

Checking networking

ifconfig                            # check the "docker0" entry
docker network ls                   # list networks - bridge, host, and none
docker network inspect <network>  # detailed info - check subnet and gateway

Creating a network

docker network create <name>      # default bridge driver
docker network create --subnet 10.1.0.0/24 --gateway 10.1.0.1 --ip-range=10.1.4.0/24 --driver=bridge <name>   # with other options
docker network ls
docker network inspect <name>

Connecting/Disconnecting a container to a network

docker network connect <network> <container>
docker container inspect <continer>           # check the Networks section
docker network disconnect <network> <container>

Deleting a network

docker network rm <network>
docker network ls

Creating a volume

docker volume create html-volume    # volume for nginx files
docker volume ls

Running a container with a volume – using the mount flag

docker container stop <container>
docker container ls
docker container run -d --name nginx-volume1 --mount type=volume,source=html-volume,target=/usr/share/nginx/html/ nginx

docker container inspect nginx-volume1              # check the Mounts section
docker volume inspect html-volume
sudo ls /var/lib/docker/volumes/html-volume/_data   # list the contents of html-volume

Running a container with a volume – using the volume flag

docker container run -d --name nginx-volume2 --volume html-volume:/usr/share/nginx/html/ nginx
docker container inspect nginx-volume2              # check the Mounts section

Now the volume is shared by 2 containers

Editing the file in the shared volume

sudo vim /var/lib/docker/volumes/html-volume/_data/index.html   # Edit index.html

docker container inspect nginx-volume1          # Get the private IP of the container 1
curl <private-ip>                                 # Check the edited html is shown

docker container inspect nginx-volume2          # Get the private IP of the container 2
curl <private-ip>                                 # Check the edited html is shown

Deleting containers

docker container stop <container>
docker container rm <container> # use -f for running container
docker container ls -a

Deleting volumes

docker volume ls
docker volume rm <volume>

Deleting images

docker image ls
docker image rm nginx
docker image ls

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s