Docker Commands for DevOps
Table of contents
No headings in the article.
docker –version:get the currently installed version of docker
ubuntu@ip-172-31-31-147:~$ docker --version Docker version 20.10.21, build 20.10.21-0ubuntu1~22.04.3
docker pull:pull images from the docker repository(hub.docker.com)
ubuntu@ip-172-31-31-147:~$ docker pull ubuntu
docker run:create a container from an image.
docker run --env MYSQL_ROOT_PASSWORD=my-secret-pw --detach mysql
ubuntu@ip-172-31-31-147:~$docker run -it -d ubuntu
docker ps:list the running containers.
ubuntu@ip-172-31-31-147:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker ps -a:show all the running and exited containers.
ubuntu@ip-172-31-31-147:~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39bf6796afdd mongo "docker-entrypoint.s…" 2 days ago Exited (0) 2 days ago mongo e597aad3892c my-app:latest "python app.py" 2 days ago Exited (137) 2 days ago my-app-container c5f57ccd5ee5 busybox "sh" 3 days ago Exited (0) 3 days ago my-busy-box 8052135aa2fe nginx "/docker-entrypoint.…" 3 days ago Exited (0) 2 days ago nginx-custom-2 431e5f9ee070 nginx "/docker-entrypoint.…" 3 days ago Exited (0) 2 days ago nginx-custom-1 886d56933ea5 nginx "/docker-entrypoint.…" 3 days ago Exited (0) 2 days ago nginx-default
docker exec:access the running container.
docker exec -it <container id> bash(or sh)
docker exec -it test_db bash mysql -uroot -pmy-secret-pw SHOW DATABASES;
ubuntu@ip-172-31-31-147:~$ docker exec -it e597aad3892c bash
docker stop:stops a running container.
docker stop <container id>
ubuntu@ip-172-31-31-147:~$ docker stop c5f57ccd5ee5 c5f57ccd5ee5
docker kill:kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it.
docker kill <container id>
ubuntu@ip-172-31-31-147:~$ ^Ccker kill c5f57ccd5ee5
-
docker commit <conatainer id> <username/imagename>
This command is used to create or save an image of the edited container on the local system.
ubuntu@ip-172-31-31-147:~$ docker commit c5f57ccd5ee5 ubuntu sha256:a0b163c8b65d160dd8c0f02b2f4d65ae75648b3ab33baf3538650d1ec0a42b84
docker login:login to the docker hub repository
ubuntu@ip-172-31-31-147:~$ docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: gouutam Password: WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
-
docker push <username/image name>
This command is used to push an image to the docker hub repository
ubuntu@ip-172-31-31-147:~$ docker push gouutam/mongo
docker images:lists all the locally stored docker images
ubuntu@ip-172-31-31-147:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest a0b163c8b65d 3 minutes ago 4.26MB my-app latest 55326f26c9c6 2 days ago 98.4MB mongo latest fb5fba25b25a 3 days ago 654MB nginx latest 021283c8eb95 12 days ago 187MB ubuntu <none> 5a81c4b8502e 2 weeks ago 77.8MB busybox latest 5242710cbd55 2 weeks ago 4.26MB python alpine3.7 00be2573e9f7 4 years ago 81.3MB
-
docker rm <container id>
This command is used to delete a stopped container
ubuntu@ip-172-31-31-147:~$ docker rm 39bf6796afdd 39bf6796afdd
-
docker rmi <image-id>
This command is used to delete an image from local storage
ubuntu@ip-172-31-31-147:~$ docker rmi 00be2573e9f7
-
docker build <path to docker file>
This command is used to build an image from a specified docker file
ubuntu@ip-172-31-31-147:~$ docker build .
Docker search
The “docker search” command searches for specific images through the Docker hub.
docker search MySQL NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 14318 [OK] mariadb MariaDB Server is a high performing open sou… 5466 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 835 [OK] percona Percona Server is a fork of the MySQL relati… 617 [OK] bitnami/mysql Bitnami MySQL Docker Image 93 [OK] databack/mysql-backup Back up mysql databases to... anywhere! 88 ubuntu/mysql MySQL open source fast, stable, multi-thread… 51 linuxserver/mysql-workbench 50 linuxserver/mysql A Mysql container, brought to you by LinuxSe… 38 circleci/mysql MySQL is a widely used, open-source relation… 29 google/mysql MySQL server for Google Compute Engine 23 [OK] rapidfort/mysql RapidFort optimized, hardened image for MySQL 23 rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 9 rapidfort/mysql-official RapidFort optimized, hardened image for MySQ… 7 bitnami/mysqld-exporter 5 drupalci/mysql-5.5 https://www.drupal.org/project/drupalci 3 [OK] newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK] vitess/mysqlctld vitess/mysqlctld 1 [OK] hashicorp/mysql-portworx-demo 0 docksal/mysql MySQL service images for Docksal - https://d… 0 mirantis/mysql 0 bitnamicharts/mysql 0 eclipse/mysql Mysql 5.7, curl, rsync 0 [OK] cimg/mysql 0 drupalci/mysql-5.7 https://www.drupal.org/project/drupalci 0
Docker restart
This command is used to restart the stopped container. It is recommended to use this after rebooting the system. Here is how to do it –
docker restart f8c52bedeecc
Docker network
The ‘docker network’ command is used to know the details of the list of networks in the cluster.
$ docker network ls NETWORK ID NAME DRIVER SCOPE 85083e755f04 bridge bridge local f51d1f2379e0 host host local 5e5d9a154c00 none null local Apart from this, you can also use the following docker network commands to manage networks - Connect - to connect a container to a network Disconnect - to disconnect a container from a network Create - to create a network Ls - to list all networks Inspect - to display detailed information on one or more networks prune - to remove all unused networks rm - to remove one or more networks Docker history
Docker history :This command is used to check the history of a docker image. The image name has to be mentioned while using the command. Here is how to use it -
$ docker history httpd
Docker copy:This command copies a file from docker to the local system.
Docker logs :This command is used to check the logs of all the docker containers with the corresponding contained id mentioned in the command.
docker logs 09ca6feb6efc
Docker volume:This command creates a volume so that the docker container can use it to store data.
ubuntu@ip-172-31-31-147:~$ docker volume create e22d43790c191cad19a2f0f93dbedda11314c5ad82ec800f009b2760ae6a3257 ubuntu@ip-172-31-31-147:~$ docker volume ls DRIVER VOLUME NAME local 9ecee412205e8b382358be5768dd7b77c60df9362e17a5485fdfe8070044b800 local 20d795020d2cb4353ef6935e53fc4a2ed1add3c108bc2a9d2422813930afd801 local 423b3b73617a54915ca5e5ae65fda2f53c58457035f21ec44478b8440788390a local e22d43790c191cad19a2f0f93dbedda11314c5ad82ec800f009b2760ae6a3257 local f5128f24c4f5d0db15c6eb6f7e879a9356318fcb9d773ee6a0887276bfab067a
Docker logout :This command will log you out of the docker hub.
ubuntu@ip-172-31-31-147:~$ docker logout Removing login credentials for https://index.docker.io/v1/