Docker commands

In this article, we will browse through some of the commonly used docker commands

  1. docker images – List down the images

2. docker pull <image> – Pull an image from a registry

$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
f7ec5a41d630: Pull complete
9444bb562699: Pull complete
6a4207b96940: Pull complete
181cefd361ce: Pull complete
8a2090759d8a: Pull complete
15f235e0d7ee: Pull complete
d870539cd9db: Pull complete
5726073179b6: Pull complete
eadfac8b2520: Pull complete
f5936a8c3f2b: Pull complete
cca8ee89e625: Pull complete
6c79df02586a: Pull complete
Digest: sha256:6e0014cdd88092545557dee5e9eb7e1a3c84c9a14ad2418d5f2231e930967a38
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

3. docker rmi <image> – Remove one or more images

4. docker ps – List the currently running containers only.

docker ps -a – Lists all containers – both running and stopped ones.

5. docker build – Build an image from a Dockerfile

$ docker build -t spring-mvc-form-validator .
[+] Building 4.5s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 399B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:8 4.4s
=> [auth] library/openjdk:pull token for registry-1.docker.io 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 87B 0.0s
=> [1/2] FROM docker.io/library/openjdk:8@sha256:8a64e423901d81b7b410ca5b3851e4bfb4d2995371d548055dcef13607004a8a 0.0s
=> CACHED [2/2] ADD target/SpringMVCFormValidator-1.0.jar SpringMVCFormValidator-1.0.jar 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:4c57b2f1a438cad910d60a2e971b5bffae1d1b157ea4a8cdf2a62df5af67e4be 0.0s
=> => naming to docker.io/library/spring-mvc-form-validator

Use docker –help build to understand the usage

6. docker run – runs a image in a new container.

$ docker run -it -p 8887:8084 tomcat:8.0
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /docker-java-home/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
17-Apr-2021 16:10:30.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53
17-Apr-2021 16:10:30.303 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 29 2018 14:42:45 UTC
17-Apr-2021 16:10:30.303 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0
17-Apr-2021 16:10:30.303 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
17-Apr-2021 16:10:30.303 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 4.19.121-linuxkit
———————-
————————
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 29 ms
17-Apr-2021 16:10:31.838 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [“http-apr-8080”]
17-Apr-2021 16:10:31.855 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [“ajp-apr-8009”]
17-Apr-2021 16:10:31.859 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1287 ms

Please use docker run –help to understand the usage.

7. docker stop – It stops one or more running containers

8. docker kill – It kills one or more containers

9. docker rm – remove one or more stopped containers

10. docker logs – Fetches the logs of a container

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e9d7cf02293 tomcat:8.0 “catalina.sh run” 11 minutes ago Up 11 minutes 0.0.0.0:8888->8080/tcp hungry_germain
$ docker logs 4e9d7cf02293
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /docker-java-home/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
17-Apr-2021 15:28:46.659 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 662 ms
————-

—————-
17-Apr-2021 15:28:48.078 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [“http-apr-8080”]
17-Apr-2021 15:28:48.111 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [“ajp-apr-8009”]
17-Apr-2021 15:28:48.122 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1255 ms

11. docker push <image> – Push an image or a repository to a registry

12. docker –help – It will list out all the docker commands

13. docker -v – It displays the docker version

$ docker -v
Docker version 20.10.5, build 55c4c88

14. docker volume – create and manage volumes

$ docker volume create logdata
logdata

We can mount this volume while running an image in a container to a target location

$ docker run -it –mount type=volume, source=logdata, target=<target_location> nginx

15. docker exec – Run a command in a running container

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e9d7cf02293 tomcat:8.0 “catalina.sh run” 13 hours ago Up 13 hours 0.0.0.0:8888->8080/tcp hungry_germain
$ docker exec -it 4e9d7cf02293 /bin/bash
root@4e9d7cf02293:/usr/local/tomcat# ls -la
total 136
drwxr-sr-x 1 root staff 4096 Sep 12 2018 .
drwxrwsr-x 1 root staff 4096 Sep 5 2018 ..
-rw-r–r– 1 root root 57011 Jun 29 2018 LICENSE
-rw-r–r– 1 root root 1446 Jun 29 2018 NOTICE
-rw-r–r– 1 root root 6794 Jun 29 2018 RELEASE-NOTES
-rw-r–r– 1 root root 16258 Jun 29 2018 RUNNING.txt
drwxr-xr-x 2 root root 4096 Sep 12 2018 bin
drwxr-xr-x 1 root root 4096 Apr 17 15:28 conf
drwxr-sr-x 3 root staff 4096 Sep 12 2018 include
drwxr-xr-x 2 root root 4096 Sep 12 2018 lib
drwxrwxrwx 1 root root 4096 Apr 17 15:28 logs
drwxr-sr-x 3 root staff 4096 Sep 12 2018 native-jni-lib
drwxr-xr-x 2 root root 4096 Sep 12 2018 temp
drwxr-xr-x 7 root root 4096 Jun 29 2018 webapps
drwxrwxrwx 1 root root 4096 Apr 17 15:28 work
root@4e9d7cf02293:/usr/local/tomcat#

Leave a Reply

Your email address will not be published. Required fields are marked *