Commonly user docker commands for quick access


Docker version

docker version

λ docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:23:10 2020
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:29:16 2020
  OS/Arch:          linux/amd64
  Experimental:     false
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
  Version:          0.18.0
  GitCommit:        fec3683

Docker Info

docker info


Docker Run

docker run {image-name}

λ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:

For more examples and ideas, visit:

Common parameters:

-d : Detached mode
--name : Name
-it : Interactive mode

Start a simple web-server:

λ docker run -d --name http-web -p 8080:80 httpd

You can see the result @ http://localhost:8080/

Start to terminal / bash in interactive mode

λ docker run -it --name ubuntu-it ubuntu:latest /bin/bash

Ctr P + Q to exit the bash shell without exiting the container

Run container with a python installation to Alpine using apk

$ docker container run alpine apk add --no-cache python3
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
Executing busybox-1.31.1-r16.trigger
OK: 53 MiB in 24 packages

Docker list containers

docker ps

docker ps -a

docker container ls -a

$ docker container ls -l (latest container)

λ docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                          PORTS               NAMES
b7aa50d480bb        httpd               "httpd-foreground"   4 minutes ago       Exited (0) About a minute ago                       http-web
4540687eabcb        hello-world         "/hello"             8 minutes ago       Exited (0) 7 seconds ago                            serene_fermat

Stop Docker Containers

docker stop {name/container-id}

docker stop $(docker ps -aq) (To stop all active containers)

FOR /f "tokens=*" %i IN ('docker ps -q') DO docker stop %i (Tricky Windows command-line version)

λ docker stop http-web

λ docker stop b7aa50d480bb

Start a stopped container

docker start {container-id/name}

λ docker start 4540687eabcb

Remove a docker container

docker rm {container-id/name}
docker rm $(docker ps -aq) (To remove all containers)
FOR /f "tokens=*" %i IN ('docker ps -aq') DO docker rm %i (To remove all containers – windows)

λ docker rm 4540687eabcb

Docker container difference

$ docker container diff e94ae1c23f8e


Docker list images

docker images

λ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              a24bb4013296        4 weeks ago         5.57MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

Docker pull image

docker pull {image-name}

λ docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
df20fa9351a1: Pull complete
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest

λ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              a24bb4013296        4 weeks ago         5.57MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

Docker Remove Image

docker rmi {image-name/id}

λ docker rmi bf756fb1ae65
Untagged: hello-world:latest
Untagged: hello-world@sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

Create image from container

$ docker container commit -m "Added Python" e94ae1c23f8e my-python-img:1.0

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
my-python-img       1.0                 1874de311462        About a minute ago   48.9MB
alpine              latest              a24bb4013296        5 weeks ago          5.57MB

Flattening docker container image (Docker export and Docker Import)

docker container export

docker image import

$ docker container export -o my-python-img-1.0.tar e94ae1c23f8e

$ docker image import my-python-img-1.0.tar my-python-img:1.1

$ docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-python-img       1.1                 ecc166ee53c0        51 seconds ago      48.8MB
my-python-img       1.0                 1874de311462        20 minutes ago      48.9MB
alpine              latest              a24bb4013296        5 weeks ago         5.57MB

Docker Image History

docker image history

$ docker image history my-python-img:1.0
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
1874de311462        26 minutes ago      apk add --no-cache python3                      43.3MB              Added Python
a24bb4013296        5 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           5 weeks ago         /bin/sh -c #(nop) ADD file:c92c248239f8c7b9b…   5.57MB

Docker file

Sample Dockerfile for a Spring boot java application

FROM openjdk:14
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Build Docker image

docker build -t{tag} {context path}

 docker build -t logicalsapien/joan .
Sending build context to Docker daemon  69.85MB
Step 1/4 : FROM openjdk:14
 ---> dee15a342cac
Step 2/4 : ARG JAR_FILE=build/libs/*.jar
 ---> Running in 13e73d2417ee
Removing intermediate container 13e73d2417ee
 ---> 37b10e8d2311
Step 3/4 : COPY ${JAR_FILE} app.jar
 ---> 82db2f7b1c32
Step 4/4 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 3894adb86517
Removing intermediate container 3894adb86517
 ---> 248b3ef28416
Successfully built 248b3ef28416
Successfully tagged logicalsapien/joan:latest

Push image to docker hub

need to login to docker hub first


docker login -u {username}

$ docker login -u logicalsapien
Login Succeeded

If yo’re using a windows machine and bacs shell, you can use something like this :

$ winpty docker login -u logicalsapien
Login Succeeded

To push the image

docker push {imagename}

$ docker push logicalsapien/joan
The push refers to repository [docker.io/logicalsapien/joan]
5730b01a75f3: Preparing
c1a9ce1e221e: Preparing
41f691265dc2: Preparing
351f02e4b003: Preparing
41f691265dc2: Mounted from library/openjdk
c1a9ce1e221e: Mounted from library/openjdk
351f02e4b003: Mounted from library/openjdk
5730b01a75f3: Pushed
latest: digest: sha256:a982eb3fcb07b687ac1367b424f1360bab95f4b8531de size: 1166


List networks

docker network ls

λ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
9a74c109118a        bridge              bridge              local
38c6238bcf19        host                host                local
5da6ef33f9d0        none                null                local

Network Inspect (Details)

docker network inspect {networkname/id}

λ docker network inspect bridge                                                                                                      
        "Name": "bridge",                                                                                                            
        "Id": "9a74c109118a93cdbcdb43da734d0275ad1ea57fa1580a231067d54d074a352d",                                                    
        "Created": "2020-07-02T23:51:23.8227395Z",                                                                                   
        "Scope": "local",                                                                                                            
        "Driver": "bridge",                                                                                                          
        "EnableIPv6": false,                                                                                                         
        "IPAM": {                                                                                                                    
            "Driver": "default",                                                                                                     
            "Options": null,                                                                                                         
            "Config": [                                                                                                              
                    "Subnet": "",                                                                                       
                    "Gateway": ""                                                                                          
        "Internal": false,                                                                                                           
        "Attachable": false,                                                                                                         
        "Ingress": false,                                                                                                            
        "ConfigFrom": {                                                                                                              
            "Network": ""                                                                                                            
        "ConfigOnly": false,                                                                                                         
        "Containers": {                                                                                                              
            "8a598f453cab6034ee76415d8c1456214e4111d2332d72e08b3ad8959c99fdb3": {                                                    
                "Name": "elegant_babbage",                                                                                           
                "EndpointID": "fa8ac23b634a95738ff8e11e6ecb3b72c4d39ac08859b2898f7664840b7a00e9",                                    
                "MacAddress": "02:42:ac:11:00:03",                                                                                   
                "IPv4Address": "",                                                                                      
                "IPv6Address": ""                                                                                                    
            "eeb90789f0f66d1c44560efc0287e49f04cfe66a05f32d48f4c50c9096c1f39f": {                                                    
                "Name": "postgres1",                                                                                                 
                "EndpointID": "d6f68665bccc4f59767c451ef77cf913a3dbac2e5143b93eea2b43b77730ff25",                                    
                "MacAddress": "02:42:ac:11:00:02",                                                                                   
                "IPv4Address": "",                                                                                      
                "IPv6Address": ""                                                                                                    
        "Options": {                                                                                                                 
            "com.docker.network.bridge.default_bridge": "true",                                                                      
            "com.docker.network.bridge.enable_icc": "true",                                                                          
            "com.docker.network.bridge.enable_ip_masquerade": "true",                                                                
            "com.docker.network.bridge.host_binding_ipv4": "",                                                                
            "com.docker.network.bridge.name": "docker0",                                                                             
            "com.docker.network.driver.mtu": "1500"                                                                                  
        "Labels": {}