Docker Cheat Sheet

1. Either use pip3 on an ubuntu server and install docker:

       # apt-get install python3-pip -y

   # pip3 install docker
   # pip3 install --upgrade docker

2. Or use apt-get on ubunytu to install docker:

       # apt-get install docker.io -y

3. Start and enable docker service:

       # systemctl start docker
   # systemctl enable docker

   Check the status of docker:

   # systemctl status docker

4. To check the docker version and docker system wide information:

       # docker -v
   # docker info

5. To search docker image in docker registry:

        # docker search httpd

   To check a specific version of image:

     # docker search ubuntu:18.04

6. To download docker images:

    # docker pull ubuntu:18.04
    18.04: Pulling from library/ubuntu
    5667fdb72017: Pull complete
    d83811f270d5: Pull complete
    ee671aafb583: Pull complete
    7fc152dfb3a6: Pull complete
    Digest: sha256:b88f8848e9a1a4e4558ba7cfc4acc5879e1d0e7ac06401409062ad2627e6fb58
    Status: Downloaded newer image for ubuntu:18.04

   To download the latest version of image:

          # docker pull httpd
    Using default tag: latest
    latest: Pulling from library/httpd
    b8f262c62ec6: Pull complete
    2c31b9311798: Pull complete
    7422a3cdf4e3: Pull complete
    1919d4fbf9e1: Pull complete
    60812fa1ab4c: Pull complete
    Digest: sha256:39d7d9a3ab93c0ad68ee7ea237722ed1b0016ff6974d80581022a53ec1e58797
    Status: Downloaded newer image for httpd:latest

7. To check all the available local images:

     # docker images --all
  REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
     ubuntu                  18.04               2ca708c1c9cc        4 days ago          64.2MB
     httpd                     latest              19459a872194        10 days ago         154MB

8. To run a container from a docker images:

     # docker run -it -p 8080:80 --name=Ubuntu ubuntu:18.04

   Where:
     - ubuntu is the image name
     - "18.04" is its respective tag
     - "-p" is used to map the <hostPort>:<containerPort>

   To run a container in detached mode:

         # docker run -d -it -p 8080:80 --name=Ubuntu ubuntu:18.04

   To check running containers:

         # docker ps -a

9. To attach to the console of docker container:

     # docker run -d -it -p 8080:80 --name=Ubuntu ubuntu:18.04
    41690b9a54d6b0dfde47566060aafa050d041095f13afc9d839cf9c86621874f

  # docker ps -a
  CONTAINER ID        IMAGE                  COMMAND         CREATED         STATUS                    PORTS                            NAMES
    41690b9a54d6        ubuntu:18.04        "/bin/bash"     5 seconds ago       Up 4 seconds        0.0.0.0:8080->80/tcp   Ubuntu

  # docker attach Ubuntu

   OR

     # docker exec -it Ubuntu "/bin/bash"

   To come out of docker console:

      # exit

10. To start the container by mapping the host IP and Port:

       # docker run -d -it -p <host_ip>:8080:80 --name=Ubuntu ubuntu:18.04

11. To start, stop and restart a container:

       # docker stop <docker_name>/<docker_id>
   # docker start <docker_name>/<docker_id>
   # docker restart <docker_name>/<docker_id>

12. To remove a stopped docker:

   # docker rm <docker_name>/<docker_id>

   To remove a running docker:

       # docker rm -f <docker_name>/<docker_id>

13. To delete a docker image:

   # docker rmi  <image_id>

     To delete all the images:

   # docker rmi $(docker images -a -q) 

  To stop the running container:
 
   # docker ps -a | egrep <dkr_name>| awk '{print $1}' | xargs docker stop

  To remove the stopped container:

   docker ps -a | egrep <dkr_name>| awk '{print $1}' | xargs docker rm  
    

14. After saving some data in a container, to export/save that for future use:

   # docker save <docker_name> -o <new_name>.tar

   OR

   # docker export <docker_name> -o <new_name>.tar

   To import a new docker:

   # docker import <new_name>.tar

15. To display the history of command used to build the docker image:

  # docker history  image:<tag>

   Eg:

     # docker images -a
  REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu                    18.04               2ca708c1c9cc        4 days ago          64.2MB
    httpd                       latest              19459a872194        10 days ago         154MB

  # docker history ubuntu:18.04
  IMAGE                CREATED             CREATED BY                                                             SIZE                   COMMENT
    2ca708c1c9cc    4 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]                      0B
    <missing>           4 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'do…     7B
    <missing>           4 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /…             745B
    <missing>           4 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]                   987kB
    <missing>           4 days ago          /bin/sh -c #(nop) ADD file:288ac0434f65264f3…    63.2MB


16. To check the container resource utilization statistics:

     # docker stats
  CONTAINER ID        NAME      CPU %        MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
    41690b9a54d6        Ubuntu              0.01%        960KiB / 1.947GiB   0.05%               766B / 0B           0B / 0B             1

17. To check the logs of container:

   # docker logs <docker_name>

   To check the logs of running container:

   # docker logs -f <docker_name>

18. To check the docker statistics without live streaming:

     # docker stats --no-stream
  CONTAINER ID        NAME             CPU %    MEM USAGE / LIMIT   MEM %       NET I/O             BLOCK I/O           PIDS
    41690b9a54d6        Ubuntu            0.00%          960KiB / 1.947GiB   0.05%         1.05kB / 0B         0B / 0B             1

19. To inspect the container:

   # docker inspect <container_name>

   To check specific object in the container like:
 

  • To check the status of the container:

    # docker inspect -f "{{.State.Status}}"  <container_name>


  • To check the IP address of the container:

    # docker inspect -f "{{.NetworkSettings.IPAddress}}" <container_name>
 

  • To check the port mapping between host and the container:

     # docker inspect -f "{{.NetworkSettings.Ports}}" <container_name>
     
      OR

   # docker inspect -f "{{.HostConfig.PortBindings}}" <container_name>

Comments

Popular posts from this blog

VNC Configuration using Ansible in CentOS 7

How to build Ubuntu Server 20.04 LTS OVA with vAPP Properties ?

LVM Configuration using Ansible in CentOS 7