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.

Abbreviate

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

Installation

For all platforms, check this.

Linux

  • 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 \
          software-properties-common
      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) \
        stable"
      # 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
    

MacOS

Check this.

Windows

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.
    # POWERSHELL
    # 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.

Uninstall

Linux

# 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

Images

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

Containers

# 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>

Others

# 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)

Delete

Read more here.

Everything

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

Images

# 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

Containers

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

Build an image

Create

# build image with Dockerfile
docker build -t <container_name> .
# with docker-compose
docker-compose up
# if success
# service name "docker_thi"
docker run -it <service_name> bash

Dockerfile

List of commands:

  • 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: change directory when you build the image (install this on that directory, for example).
  • 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)

Create a container

CLI

docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started

docker-compose.yml

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"
      services:
      web:
          build: .
          depends_on:
          - db
          - redis
      redis:
          image: redis
      db:
          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.

# 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

# 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=0.0.0.0"  ]

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

# run by `docker-compose up`
version: '3'
services:
  dataswati:
    container_name: docker_thi
    image: docker_thi_img:latest
    ports:
      - "8888:8888"
    volumes:
      - "/local-folder/:/docker-folder/"

Reference