Docker container is one of the most emerging technologies now a days. Docker containers are generally used in CI/CD (Continuous Integration/Continuous Deployment) platform. Containers are the light weight VMs (Virtual Machines) which make use of underlying hypervisors resources like (RAM,CPU,HDD and Kernel).

Docker command is used to manage containers and images from command line. In this article we will cover 20 useful docker command examples in Linux. I am assuming docker is already installed on your Linux system and your regular user is added to docker group.

1) Verify Docker Version

First important task while working on docker containers is to know your docker version, run below docker command to know version

$ docker --version
Docker version 20.10.12, build e91ed57
$

2) View system wide Information

‘docker info’ command is used to view the system wide information like Docker root directory, OS version, Kernel Version, Docker Version, RAM, CPU and Docker Registry.

$ docker info

Docker-info-command-Linux

Docker-Info-Registry-Kernel

3) Search Docker Images

Using  ‘docker search’ command we can search the docker container images from docker hub registry, Let’s assume I want to search latest nginx docker images.

$ docker search nginx

Output of above command would look like below,

Docker-Search-Command-Output

4) Download Docker Container Images

Docker pull command is used to download container images from docker hub registry.

Syntax :

$ docker pull <Image-Name>

Docker pull command always  try to download latest version of image, though we can specify the particular version of  image. Let’s assume I want to download latest version of rsyslog image

$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
5eb5b503b376: Pull complete
1ae07ab881bd: Pull complete
78091884b7be: Pull complete
091c283c6a66: Pull complete
55de5851019b: Pull complete
b559bad762be: Pull complete
Digest: sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
$

Downloading specific image based on version, let’s assume, we want to download Ubuntu:20:04 docker image, run

$ docker pull ubuntu:20.04
20.04: Pulling from library/ubuntu
08c01a0ec47e: Pull complete
Digest: sha256:669e010b58baf5beb2836b253c1fd5768333f0d1dbcb834f7c07a4dc93f474be
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04
$

5) View Downloaded Docker Container Images

As we know that docker’s root directory is ‘/var/lib/docker’, so whenever we download images using docker pull command then all images will be saved in docker’s root directory.

To view all downloaded docker container images, run

$ docker image ls
or 
$ docker images

Output

Docker-Images-Command-Output

6) Run Docker Container

Containers are launched with ‘docker run’ command, let assume I want to run a container from nginx image.

$ docker run -it -p 9000:80 --name=myapp nginx

output

Docker-Run-Command-Example

Above will run a container with the name “myapp” and we also set pat rule in such a way that if any request comes to 9000 port on docker host then that request will be redirected to myapp container on 80 port. If you might have noticed that we directly get the console just after executing the command. Type exit to stop / shutdown the container and if want to get out from the container without stopping it then type “ctrl+p+q

Now you can access you nginx app on port 9000 using curl command,

$ curl http://<Docker-Host-IP-Address>:9000/

7) Run Container in detach mode

We can run a container in detach mode using -d option in ‘docker run’ command.Here detach mode means we will get command prompt after executing the docker run command. Example is shown below,

$ docker run -it -d -p 9080:80 --name=myapp2 httpd
f7dc2ece64708a9de5e53f341b0c7d17ac9189fb7c99079ad8744348bc5a6faa
$

8) View All Running Containers

Run ‘docker ps’ command is used to view all running containers

$ docker ps

docker-ps-command-output

To view all running along with exited or stopped containers, run

$ docker ps -a

9)  Container Console Access

To get container console access run beneath ‘docker exec’ command

sysops@linuxtechi:~$ docker exec -it myapp /bin/bash
root@3d5a0eef3e7a:/#

To come out of console type exit command

10) Start, Stop, Restart and Kill Containers

Just like virtual machines we can start, stop and restart docker containers.

Use below command to stop a running container

$ docker stop myapp2
myapp2
$

In place of container name we can also use container id

Use below command to start a container.

$ docker start myapp2
myapp2
$

Use below command to restart a container.

$ docker restart myapp2
myapp2
$

Just like process we can also kill a container, Use below command to kill a container.

sysops@linuxtechi:~$ docker kill myapp2
myapp2
sysops@linuxtechi:~$

11) Remove a Container

Docker Container is removed or deleted using ‘docker rm‘ command. ‘docker rm’ will work only when docker is stopped / shutdown.

Syntax:

$ docker rm {container_name_OR_container_id}

$ docker stop test-app
test-app
$ docker rm test-app
test-app
$

To remove a running container forcefully then use ‘-f’ option in docker rm command. Example is shown is below

$ docker rm -f nginx-app

Remove-container-forcefully

12) Remove Docker Container Images

Just like containers we can also delete or remove docker images. ‘docker rmi’ command is used to remove docker container images.

Let’s assume, we want to delete a docker image ‘Ubuntu:20.04’, run

$ docker rmi ubuntu:20.04
Untagged: ubuntu:20.04
Untagged: ubuntu@sha256:669e010b58baf5beb2836b253c14f7c07a4dc93f474be
Deleted: sha256:54c9d81cbb440897908abdcaa98674db831e40a66f704f
Deleted: sha256:36ffdceb4c77bf34325fb695e64ea447f629593310578d2
$

In above command we can use image id in place of image name

$ docker rmi 54c9d81cbb44

13)  Save and Load Docker Container Image

To save a docker image to a tar file, use ‘docker save‘ command, example is shown below:

$ docker save nginx -o mynginx.tar
$ ls -l mynginx.tar
-rw------- 1 sysops sysops 145931776 Feb 5 10:30 mynginx.tar
$

To load a docker image from a tar file, use ‘docker load‘ command

$ docker load -i mynginx.tar
Loaded image: nginx:latest
$

Note: These commands become useful when we want to transfer docker image from one Docker Host to another.

14) Copy files/folder to Container

We can copy files or folder to a container from the docker host using ‘docker cp’ command. In the following we are copying ‘mycode’ folder to myapp container

$ docker cp mycode myapp:/tmp

Copy-files-folder-Docker-Container

15) View History of a Docker Image

History of Docker image here means what commands are being executed while building docker images, we can view these commands using ‘docker history’

Syntax : #

$ docker history {Image_Name_OR_Image_id}

$ docker history nginx

Docker-History-Command-Output

16) View Logs from the Container

We can view the logs from the containers without login into it, Run ‘docker logs’ command

Syntax :

$ docker logs {container_name_or_container_id}

$ docker logs myapp

To view the live logs use ‘-f’ option in docker logs command

$ docker logs -f myapp

To view last 10 logs from a container, run

$ docker logs --tail 10 myapp

View-logs-from-docker-conatiner

17) Display Container(s) Resource Usage Statistics

To display CPU, memory, and network I/O usage of all the containers, run ‘docker stats’ command

$ docker stats

Docker-Stats-Command-Output

Above command will show live streaming of resource usage statistics of all the Containers.

To view stats of a specific container the run docker stats followed by container

$ docker stats myapp

Resource usage statistics without live streaming

$ docker stats --no-stream

We can display the running processes of a container with ‘docker top‘ command.

Syntax: #

docker top {Container_Name_OR_ID}

$ docker top myapp

docker-top-command-output

18) View Container IP Address

Container low-level information is displayed using ‘docker inspect’ command. We can fetch the ip address of a container from ‘docker inspect’ command

Syntax:

$ docker inspect -f ‘{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}’  <Container-Name>

Example

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp
172.17.0.2
$

19) Build Docker Container Image

Docker container image is build using ‘docker build‘ command but for docker build command to first we must create Dockerfile. Let’s suppose we Dockerfile is created under mycode folder then run following command to build docker image,

$ cd mycode
$ docker build -t mynginx:v2 .

Also ReadHow to build Docker Container Images with Dockerfile

20) Set Tag to Docker Image

docker tag‘ command is used to set tag to a image.

Syntax :

$ docker tag source_image{:tag}  target_image{:tag}

Let’s suppose I want to set tag of source image ‘mariadb:latest’ as ‘mariadb:10.03’

$ docker tag mariadb:latest mariadb:10.03
$ docker images | grep -i mariadb
mariadb 10.03 45a5a43e143a 3 days ago 410MB
mariadb latest 45a5a43e143a 3 days ago 410MB
$

If you wish to upload image to your private registry then you tag the image in such a way that it should have registry url followed project and image.

$ docker tag mariadb:latest myregistry:5000/db/mariadb:10.03

Now login to your private registry using ‘docker login‘ command and run docker push command to upload it.

$ docker push myregistry:5000/db/mariadb:10.03

That’s all from this article. I hope you have found these examples informative. Please feel free to share your queries and comments in below comments sections.

Read Also: How to Create and Use MacVLAN Network in Docker

The post 20 Useful Docker Command Examples in Linux first appeared on LinuxTechi.

Leave a Comment