What and Why?

What's docker Souce rollout.io.

Container  vs Virtual Machine Container vs Virtual Machine, souce docker.com.

RAM usage: Docker  vs Virtual Machine RAM usage: Docker vs Virtual Machine, souce eureka.com.


  • ps = process status
  • -i = interactive
  • -t = terminal
  • -m = memory


For all platforms, check this.


  • For Linux, check this!
      # uninstall old versions
      sudo apt-get remove docker docker-engine docker.io containerd runc
      sudo apt-get update
      sudo apt-get install \
          apt-transport-https \
          ca-certificates \
          curl \
          gnupg-agent \
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
      # make sure: 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
      sudo apt-key fingerprint 0EBFCD88
      sudo add-apt-repository \
        "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
        $(lsb_release -cs) \
      # install docker engine
      sudo apt-get update
      sudo apt-get install docker-ce docker-ce-cli containerd.io
      # check if everything is ok
      sudo docker run hello-world
      # incase docker-compose isn't installed
      sudo apt install docker-compose

    If you use Ubuntu 20.04+, replace $(lsb_release -cs) with eoan because docker currently (17 May 20) doesn’t support 20.04 yet!

  • If wanna run docker without root, check this.
    sudo groupadd docker # create a docker group
    sudo usermod -aG docker <user> # add <user> to group
    newgrp docker # activate the changes
  • Configure docker start on boot (Ubuntu 15.04 or later)
    sudo systemctl enable docker


Check this.


If meet the error Failed to construct a huffman tree using the length array. The stream might be corrupted.

  1. You must have Windows 10: Pro, Enterprise, or Education (Build 15063 or later). Check other requirements.
    # check window version
    Get-WmiObject -Class Win32_OperatingSystem | % Caption
    # check window build number
    Get-WmiObject -Class Win32_OperatingSystem | % Buildnumber
  2. Active Hyper-V and Containers (you can do it manually in Turn Windows features on or off)
    # Open PowerShell with Administrator and run following
    Enable-WindowsOptionalFeature -Online -FeatureName containers –All
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V –All
    # restart
  3. Download and install.
  4. Check docker version.
  5. Try docker run hello-world.

Make NVIDIA work in docker (Linux)

Idea: Using NVIDIA driver of the base machine, don’t install anything in docker!

  1. First, maker sure your base machine has an NVIDIA driver.

     # list all gpus
     lspci -nn | grep '\[03'
     # check nvidia & cuda versions
  2. Install nvidia-container-runtime

     curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | sudo apt-key add -
     distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
     curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
     sudo apt-get update
     sudo apt-get install nvidia-container-runtime
  3. Note that, we cannot use docker-compose.yml in this case!!!
  4. Create an image img_datas with Dockerfile is

     FROM nvidia/cuda:10.2-base
     RUN apt-get update && \
         apt-get -y upgrade && \
         apt-get install -y python3-pip python3-dev locales git
     # install dependencies
     COPY requirements.txt requirements.txt
     RUN python3 -m pip install --upgrade pip && \
         python3 -m pip install -r requirements.txt
     COPY . .
     # default command
     CMD [ "jupyter", "lab", "--no-browser", "--allow-root", "--ip="  ]
  5. Create a container,

     docker run --name docker_thi --gpus all -v /home/thi/folder_1/:/srv/folder_1/ -v /home/thi/folder_1/git/:/srv/folder_2 -dp 8888:8888 -w="/srv" -it img_datas
     # -v: volumes
     # -w: working dir
     # --gpus all: using all gpus on base machine

This article is also very interesting and helpful in some cases.


Docker can't connect to docker daemon,

# check if daemon is running?
ps aux | grep docker

# run
sudo /etc/init.d/docker start

sudo systemctl restart docker meets Job for docker.service failed because the control process exited with error code.

  1. Try to remove failed daemon.json file in /etc/docker/ (if the problem comes from here)
  2. Try running either sudo /etc/init.d/docker start or sudo service docker restart (twice if needed).



# from docker official
sudo apt-get remove docker docker-engine docker.io containerd runc
# identify what installed package you have
dpkg -l | grep -i docker

# uninstall
sudo apt-get purge -y docker-engine docker docker.io docker-ce docker-ce-cli
sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce
# remove images containers
sudo rm -rf /var/lib/docker /etc/docker
sudo rm /etc/apparmor.d/docker
sudo groupdel docker
sudo rm -rf /var/run/docker.sock

Login & Download images

docker login
# using username (not email) and password
  • Download at Docker Hub.
  • Download images are store at C:\ProgramData\DockerDesktop\vm-data (Windows) by default.

Check info

# docker's version
docker --version


# list images on the host
docker images
# check image's info
docker inspec <image_id>


# list running containers
docker ps
docker ps -a # all (including stopped)
# only the ids
docker ps -q
docker ps -a -q
# container's size
docker ps -s
docker ps -a -s
# container's names only
docker ps --format '{{.Names}}'
docker ps -a --format '{{.Names}}'
# Check the last command in container
docker ps --format '{{.Command}}' --no-trunc
# check log
# useful if we wanna see the last running tasks's
docker container logs <container_name>


# RAM & CPU usages
docker stats
docker stats <container_name>

Attach / Start / Stop

We can use sometimes interchangeable between <container_id> and <container_name>.

# get info (container's id, image's id first)
docker ps -a
# start a stopped container
docker start <container_id>
# stop a container
docker stop <container_id>
# going to running container env
docker exec -it <container_name> bash
# stop all running containers
docker stop $(docker ps -a -q)


Read more here.


# any resources
docker system prune
# with all unused images
docker system prune -a


# list all images
docker images -a
# remove a specific image
docker image rm <IMAGE_ID>

Dangling images are layers that have no relationship to any tagged images.

# list dangling images
docker images -f dangling=true
# remove dangling images
docker images purge


# remove a specific containers
docker rm -f <container-id>
# remove all containers
docker rm -f $(docker ps -a -q)

Build an image


# build image with Dockerfile
docker build -t <img_name> .

# custom Dockerfile.abc
docker build -t <img_name> . -f Dockerfile.abc
# with docker-compose
docker-compose up
# if success
# service name "docker_thi"
docker run -it <service_name> bash
# from current container
docker ps -a # check all containers
docker commit <container_id> <new_image_name>


  • FROM: the base image you use, can be obtained from Docker Hub. For example, FROM ubuntu:18.04 (18.04 is a tag, latest is default)
  • WORKDIR app/: Use app/ as the working directory.
  • RUN: install your application and packages requited, e.g. RUN apt-get -y update.
    • RUN <command> (shell form)
    • RUN ["executable", "param1", "param2"] (exec form)
  • CMD: sets default command and/or parameters, which can be overwritten if docker container runs with command lines. If there are many CMDs, the last will be used.
      # in Dockerfile
      CMD echo "Hello world"
      # run only
      docker run -it <image>
      # output: "Hello world"
      # run with a command line
      docker run -it <image> /bin/bash
      # output (CMD ignored, bash run instead): [email protected]:/#
    • CMD ["executable","param1","param2"] (exec form, preferred)
    • CMD ["param1","param2"] (sets additional default parameters for ENTRYPOINT in exec form)
    • CMD command param1 param2 (shell form)
  • ENTRYPOINT: configures a container that will run as an executable. Look like CMD but ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters.
      # Dockerfile
      ENTRYPOINT ["/bin/echo", "Hello"]
      CMD ["world"]
      # run
      docker run -it <image>
      # produces 'Hello world'
      # but run
      docker run -it <image> John
      # produces 'Hello John'
    • ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
    • ENTRYPOINT command param1 param2 (shell form)
  • EXPOSE 5000: Listen on the specified port
  • COPY . app/: Copy the files from the current directory to app/

Create a container


# container test from an image
docker create --name container_test -t -i <image_id> bash
docker start container_test
docker exec -it container_test bash
docker run  --name <container_name> -dp 3000:3000 -v todo-db:/etc/todos <docker_img>


Use to create various services with the same image.

  • depends_on:[ref] Express dependency between services (with orders).

    In docker-compose.yml: db and redis start before web.

      version: "3.8"
          build: .
          - db
          - redis
          image: redis
          image: postgres
  • command:[ref] Override the default command.
  • stdin_open: true and tty: true: keep container alive!
  • volumes (outside containers): volumes controlled by docker. They’re located on different places,
    # check info of a volume
    docker volume inspect <volume_name>
  • restart: always (auto start container after logging in), no (default), on-failure.

Example of Dockerfile and docker-compose

Build an image ubuntu containing installed python and run jupyter notebook at the beginning. If you wanna integrate GPU in docker container, check this section.

# Dockerfile

# base image
FROM ubuntu:18.04

# update & upgrade
RUN apt-get update && \
    apt-get -y upgrade && \
    apt-get install -y python3-pip python3-dev locales git

RUN echo 'alias python="python3"' >> ~/.bashrc

# install dependencies
COPY requirements.txt requirements.txt
RUN python3 -m pip install --upgrade pip && \
    python3 -m pip install -r requirements.txt
COPY . .

# default command
CMD [ "jupyter", "lab", "--no-browser", "--allow-root", "--ip="  ]

# Then run with `docker build -t docker_thi_img`
# docker-compose.yml

# run by `docker-compose up`
version: '3'
    container_name: docker_thi
    image: docker_thi_img:latest
      - "8888:8888"
      - "/local-folder/:/docker-folder/"
	working_dir: /srv