Docker is a tool designed to create, deploy and run applications using containers. With a container, developers can package applications with all parts they need such as libraries, dependencies and ship it all out as on. Docker provides a REST API to talk with its daemon which is executed by the command-line interface.
This article shows you the 25 most useful docker commands with examples of each.
Check Docker version
$ docker version
As soon as you install docker, check the version. It displays the following information.
Docker info log inspect and stats
See the Docker system-wide information
$ docker info
To see system-wide information of your Docker installation use the 'docker info' command.
Docker log
To see the log of the container you can use the following command. Here, my_container is the name of the container, you can use container id as well.
$ docker log my_container
Docker inspect
You can see docker configuration in JSON by using inspect command, which is very helpful.
$ docker inspect my_container
Docker stats
The docker stats command shows container CPU, memory utilization, Network, and I/O bandwidth.
$ docker stats my_container
Docker images
Pull the latest image
To download the latest image from the Docker official repository i.e. docker hub https://hub.docker.com/ you can use the pull command as below.
$ docker pull nginx
Pull image with tag
When you don’t specify any tag, the image will be of the latest version. But, you can download the previous version using image name and tag as below.
$ docker pull nginx:1.15.12
List docker images
To list docker images in your local system just type,
$ docker images
Docker Container
Create a new container from an image
Containers are the running state of an image. You can simply create a container using docker run and image with a tag.
$ docker run nginx:1.15.12
Map host port to container port and run container in the background
To run a container in the background use option -d and to map port use option -p with port number as shown below. You can also give the container name using --name flag.
$ docker run -d -p 80:80 --name my_server nginx
List all containers
To list all running, existing containers use docker ps with -a option.
$ docker ps -a
Execute interactive bash terminal a container
To log in to a running container use docker exec command with -it (interactive terminal) and using bash shell.
$ docker exec -it [container_id or name] bash
Rename a container
You can also rename the container simply by running the command as below. Here, my_container is old name, my_new_container is new name.
$ docker rename my_container my_new_container
Start/Stop the container
You can start and stop the container just by running the following command.
$ docker stop my_container
$ docker start my_container
Copy a file from host to container
Sometimes you may need to copy files from host to running container, to do this use docker cp command. Here, we are going to copy the /var/my_file.txt file to the container /tmp directory.
$ docker cp /var/my_file.txt my_container:/tmp
Turn a container into image
If you have some important file or configuration inside the running container and you want to use as an image, create running container using docker commit as below.
$ docker commit my_container my_final_container:v1
Create temporary container
This will download centos image and make a container, login to it. As soon as you exit out of the container it will be destroyed.
$ docker run --rm -it centos bash
Execute a command from the host inside the container
You can also execute a command from the host passing -c option in the docker exec command as below.
$ docker exec my_container bash -c "ping google.com"
Docker Volumes
Bind volumes map directory to your container
The following example binds the volume /backup/mysql of the host node to the container /var/lib/mysql directory. This creates persistent volume as well. Here, passing -e as an environmental variable your container mysql password will be applied.
$ docker run -d -v /backup/mysql:/var/lib/mysql --name mysql_instance -e "MYSQL_ROOT_PASSWORD=My$ecret123#" mysql:5.7
Create normal volume
The volume will be created in the root directory of docker installation. Further you can map the volume to your container.
$ docker volume create jenkins_home
List docker volumes
To list the normal type of volume you can use the following command. Remember it only displays normal volume. To see bind volume you have to use the docker inspect command.
$ docker volume ls
Remove volume
To remove docker volume use the following command.
$ docker volume rm jenkins_home
Docker networking
Create new network using bridge interface
In the following example, my_network network is created with a subnet of 192.168.10.0/24 and gateway 192.168.10.1. It is associate with bridge interface, however, you can associate with host or none interface as well.
$ docker network create -d bridge --subnet 192.168.10.0/24 --gateway 192.168.10.1 my_network
List docker network interfaces
To list all docker networks paste the following command in your terminal.
$ docker network ls
See details of a network
The bridge network is the default network in docker. It handles all the networking the container. To see details of a network use as below.
$ docker network inspect bridge