Docker Commands
In this tutorial we will discuss about docker Commands that you will be using frequently while you are working with Docker.
1. docker –version
This command is used to display the version of Docker that is currently installed on the system.
$ docker --version Docker version 19.03.13, build 4484c46d9d
2. Docker login
Before pulling/pushing any docker image from/to docker hub repository, one needs to login to the docker hub repository. Hence this command is used to login the docker hub repository.
$ docker login --username={DOCKERHUB_USERNNAME} --email={DOCKERHUB_EMAIL}
To login your custom docker registry then you can specify your docker registry name.
$ docker login your.docker.host.com Username: ashok Password: ********
3. Docker pull
This command is used to pull images from the docker repository. By default, this command always downloads the latest version of image, though we can specify the particular image by using the tag name. (<image_name>:<tag_name>.
$ docker pull ubuntu:18.04 18.04: Pulling from library/ubuntu 171857c49d0f: Pull complete 419640447d26: Pull complete 61e52f862619: Pull complete Digest: sha256:646942475da61b4ce9cc5b3fadb42642ea90e5d0de46111458e100ff2c7031e6 Status: Downloaded newer image for ubuntu:18.04 docker.io/library/ubuntu:18.04
4. Docker build
This command is used to build the docker image from a specified Docker file. To name the image ‘-t’ flag is used.
$ docker build -t myapp:2.0.0 .
5. Docker run
This command is used to create a container from the specified image.
$ docker run -it -d myapp:2.0.0 429f6560018e1808df890273c3572a23b46596542112725eab2881428c212aa9
If a port is exposed in the dockerfile, we need to map it in the container by using ‘-p’ flag.
$ docker run -it -p 8080:8080 myapp:2.0.0
6. Docker tag
This command is used to rename the existing docker image.
$ docker tag myapp:2.0.0 waytoeasylearn/myapp:3.0.0 or $ docker tag myapp:2.0.0 repo.waytoeasylearn.com/myapp:3.0.0
Before pushing the docker image to docker hub repository, it is necessary to tag the image as <username/image_name> or <your_private_repo_hostname /image_name>.
7. Docker push
This command is used to push the image to the docker hub repository.
$ docker push repo.waytoeasylearn.com/myapp:3.0.0
8. Docker ps
This command is used to list all the running containers on the system.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 429f6560018e myapp:2.0.0 "java -jar app.jar" 15 minutes ago Up 15 minutes agitated_poitras
To display the list of all the running and the stopped containers on the system use ‘-a’ flag.
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 429f6560018e myapp:2.0.0 "java -jar app.jar" 19 minutes ago Up 19 minutes agitated_poitras c20c1d17c593 ubuntu:18.04 "/bin/bash" 21 minutes ago Exited (0) 21 minutes ago jolly_chatterjee 90778571ce78 alpine "sh -c 'while sleep …" 45 hours ago Exited (137) 39 hours ago alpine-dev 25e6353fc651 hello-world "/hello" 3 days ago Exited (0) 3 days ago cool_euler
9. Docker exec
This command is used to access the docker container and run commands inside the container.
$ docker exec -ti 429f6560018e /bin/bash root@429f6560018e:/# date Mon Oct 19 06:11:33 UTC 2020 root@429f6560018e:/#
10. Docker stop
This command is used to stop a running container.
$ docker stop 429f6560018e 429f6560018e
11. Docker kill
This command is used to kill a running container immediately.
$ docker kill 429f6560018e 429f6560018e
The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, while ‘docker kill’ command kills the running container abruptly.
12. Docker pause
This command is used to pause a running container.
$ docker pause 429f6560018e 429f6560018e
When you do not need a container momentarily to perform administrative functions, it is convenient to pause other than stop containers.
13. Docker unpause
This command is used to resume a paused container.
$ docker unpause 429f6560018e 429f6560018e
14. Docker rm
This command is used to delete (remove) a stopped container.
$ docker rm 429f6560018e 429f6560018e
15. Docker images
This command is used to list the docker images stored on the system.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE myapp 2.0.0 429f6560018e 10 minutes ago 149MB alpine 3.12 a24bb4013296 4 months ago 5.57MB alpine latest a24bb4013296 4 months ago 5.57MB alpine 3.9 78a2ce922f86 5 months ago 5.55MB hello-world latest bf756fb1ae65 9 months ago 13.3kB
16. Docker rmi
This command is used to delete (remove) a docker image.
$ docker rmi myapp:2.0.0
17. Docker info
This 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 Client: Debug Mode: false Server: Containers: 4 Running: 1 Paused: 0 Stopped: 3 Images: 119 Server Version: 19.03.13 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 8fba4e9a7d01810a393d5d25a3621dc101981175 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd init version: fec3683 Security Options: apparmor seccomp Profile: default Kernel Version: 5.4.0-51-generic Operating System: Ubuntu 18.04.5 LTS OSType: linux Architecture: x86_64 CPUs: 8 Total Memory: 15.5GiB Name: ashok ID: 7DV7:KXYB:5VIB:GOA5:DEE5:JTPZ:H4JS:IG7U:FZWS:Q46Q:OCOC:77G4 Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: docker-registry.domain.com:5000 hub.waytoeasylearn.com:2550 127.0.0.0/8 Live Restore Enabled: false WARNING: No swap limit support
18. Docker search
This command is used to search the docker image with the keyword from the docker hub repository.
$ docker search alpine NAME DESCRIPTION STARS OFFICIAL AUTOMATED alpine A minimal Docker image based on Alpine Linux… 6847 [OK] mhart/alpine-node Minimal Node.js built on Alpine Linux 475 anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.28 over A… 453 [OK] frolvlad/alpine-glibc Alpine Docker image with glibc (~12MB) 247 [OK] gliderlabs/alpine Image based on Alpine Linux will help you wi… 183 alpine/git A simple git container running in alpine li… 150 [OK] mvertes/alpine-mongo light MongoDB container 116 [OK] yobasystems/alpine-mariadb MariaDB running on Alpine Linux [docker] [am… 71 [OK] alpine/socat Run socat command in alpine container 59 [OK] kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 45 [OK]
19. Docker attach
This command to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. attach the running container with the following command. you need to press enter once execute this command, docker is waiting for your input.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 429f6560018e ubuntu:18.04 "/bin/bash" About an hour ago Up About an hour agitated_poitras $ docker attach 429f6560018e root@429f6560018e:/# exit exit $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
once attached, you can do all the cli commands like you can check the node version, run the bundle file, exit etc.
But the problem here once you exit out of this, your container is no logger in the up status. your container is also in exit status.
what if you want to do all this stuff without exiting the running container. that’s where docker exec command comes into picture.
20. Docker copy
This command is used to copy the files from local machine to inside the container and vice versa.
From docker container to host machine
Syntax
$ docker cp <container_id>:<src_path_inside_the_container> <dest_path_of_host_machine>
$ docker cp 429f6560018e:/home/myapp.json /home/ashok/
From host machine to inside the docker container
Syntax
docker cp <src_path_of_host_machine> <container_id>:<dest_path_inside_the_container>
docker cp /home/ashok/newapp.json 429f6560018e:/home/
21. Docker commit
This command is used to create a new image of an edited container on the local system.
$ docker commit 26f7b932215f newapp:2.0.0 sha256:05f3e2fd58293e0817ab3ba1670d08e14642d11bc87f73b17d8dfe1aa45d5d9d $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE newapp 2.0.0 05f3e2fd5829 33 seconds ago 63.2MB
22. docker restart
This command is used to restart the docker container with container id mentioned in the command.
$ docker restart 05f3e2fd5829 05f3e2fd5829
Run the command below and check the STATUS parameter to verify if the container started recently.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 05f3e2fd5829 newapp:2.0.0 "java -jar app.jar" 15 minutes ago Up 3 seconds 8080/tcp agitated_poitras
23. docker network
This command is used to lists the details of all the network in the cluster.
$ docker network list NETWORK ID NAME DRIVER SCOPE 9d6476976908 bridge bridge local 52df6aaaf0a1 host host local 7420165cada5 none null local
There are several other docker network commands.
$ docker network Usage: docker network COMMAND Manage networks Commands: connect Connect a container to a network create Create a network disconnect Disconnect a container from a network inspect Display detailed information on one or more networks ls List networks prune Remove all unused networks rm Remove one or more networks Run 'docker network COMMAND --help' for more information on a command.
24. docker history
This command is used to show the history of a docker image with the image name mentioned in the command.
$ docker history ubuntu:18.04 IMAGE CREATED CREATED BY SIZE COMMENT 56def654ec22 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 3 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 3 weeks ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B <missing> 3 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:4974bb5483c392fb5… 63.2MB
25. docker logs
This command is used to show the logs of the docker container with contained id mentioned in the command.
$ docker logs 26f7b932215f Hello welcome to waytoeasylearn docker tutorial
26. docker logout
This command is used to logging out from docker hub.
$ docker logout Removing login credentials for https://hub.waytoeasylearn.com