Docker

Anh-Huy Dinh

What and Why?

Abbreviate

  • ps = process status : check running containers (with a for all)
  • i = interactive : used in docker exec or docker run
  • t = terminal : used in docker exec or docker run
  • m = memory
  • v or -volume : corresponding folders in/out containers.
  • -rm : create temprarily a container (removed after exit)

Installation

👉
For all platforms, check official guide. It’s up to date!
You mind find this article is useful for Ubuntu/Pop!_OS.
After installing, if you meet Got permission denied while trying to connect to the Docker daemon socket, check this.

Linux

  • For Linux, check this!
    • Unninstall old versions
      1sudo apt-get remove docker docker-engine docker.io containerd runc
      2sudo apt-get update
      3sudo apt-get install \
      4    apt-transport-https \
      5    ca-certificates \
      6    curl \
      7    gnupg-agent \
      8    software-properties-common
      9curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
      Make sure: 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
      1sudo apt-key fingerprint 0EBFCD88
      2sudo add-apt-repository \
      3  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
      4  $(lsb_release -cs) \
      5  stable"
      Install docker engine
      1sudo apt-get update
      2sudo apt-get install docker-ce docker-ce-cli containerd.io
      Check if everything is ok
      1sudo docker run hello-world
      Incase docker-compose isn’t installed
      1sudo 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.
    • 1sudo groupadd docker # create a docker group
      2sudo usermod -aG docker <user> # add <user> to group
      3newgrp docker # activate the changes
  • Configure docker start on boot (Ubuntu 15.04 or later)
    • 1sudo systemctl enable docker

MacOS

👉 Check this official doc.

Windows

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

With GPUs support?

👉 

Uninstall

Linux

1# from docker official
2sudo apt-get remove docker docker-engine docker.io containerd runc
1# identify what installed package you have
2dpkg -l | grep -i docker
3
4# uninstall
5sudo apt-get purge -y docker-engine docker docker.io docker-ce docker-ce-cli
6sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce
1# remove images containers
2sudo rm -rf /var/lib/docker /etc/docker
3sudo rm /etc/apparmor.d/docker
4sudo groupdel docker
5sudo rm -rf /var/run/docker.sock

Login & Download images

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

Check info

1# docker's version
2docker --version

Images

1# list images on the host
2docker images
1# check image's info
2docker inspec <image_id>
1# Where are images stored?
2docker info
3# normally, /var/lib/docker/

Containers

1# list running containers
2docker ps
3docker ps -a # all (including stopped)
1# only the ids
2docker ps -q
3docker ps -a -q
1# container's size
2docker ps -s
3docker ps -a -s
1# container's names only
2docker ps --format '{{.Names}}'
3docker ps -a --format '{{.Names}}'
1# Check the last command in container
2docker ps --format '{{.Command}}' --no-trunc
1# check log
2# useful if we wanna see the last running tasks's
3docker container logs <container_name>
1# get ip address
2docker inspect <container_name> | grep IPAddress
1# Attach to the running container
2docker attach <container_name>

Others

1# RAM & CPU usages
2docker stats
3docker stats <container_name>

Attach / Start / Stop

We can use sometimes interchangeable between <container_id> and <container_name>.
1# get info (container's id, image's id first)
2docker ps -a
1# start a stopped container
2docker start <container_id>
3
4# start and enter the container
5docker start -i <container>
1# stop a container
2docker stop <container_id>
1# Entering the running container (not attach)
2docker exec -it <container_name> bash
1# stop all running containers
2docker stop $(docker ps -a -q)
1# Attach to the running container
2docker attach <container_name>

Delete

Everything

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

Images

1# list all images
2docker images -a
1# remove a specific image
2docker image rm <IMAGE_ID>
Dangling images are layers that have no relationship to any tagged images.
1# list dangling images
2docker images -f dangling=true
1# remove dangling images
2docker images purge
If you use docker images -a and see a lot of <none>:<none> images. Don’t be worry to fast, if they’re dangling images, they take spaces, otherwise, they’re harmless to your drive! Check this SO question.

Containers

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

Zsh in a container

If you have already a container, enter that container and then,
1# Enter
2docker exec -it container_name bash
3
4# Install zsh
5apt-get update
6apt-get install zsh
7zsh
8
9# Install curl
10apt-get install curl
11
12# Install oh-my-zsh
13sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
If you want to integrate the zsh installation in the Dockerfile,
1RUN apt-get install zsh && apt-get install curl
2RUN PATH="$PATH:/usr/bin/zsh"
3RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
💡
Now, enter the container by docker exec -it zsh (instead of bash)!

Build an image

Create

1# build image with Dockerfile
2docker build -t <img_name> .
3
4# custom Dockerfile.abc
5docker build -t <img_name> . -f Dockerfile.abc
1# with docker-compose
2docker-compose up
3# with custom file
4docker-compose -f docker-compose.amin.yml up -d
1# if success
2# service name "docker_thi"
3docker run -it <service_name> bash
1# from current container
2docker ps -a # check all containers
3docker commit <container_id> <new_image_name>

Rename

1docker image tag old:latest myname/new:latest

Dockerfile

⚠️
You cannot use something like COPY .. . (parent of the folder containing the Dockerfile) in the Dockerfile because it uses the “build context” and it can access to files within that context. Ref.

Create a container

CLI

1# container test from an image
2docker create --name container_test -t -i <image_id> bash
3docker start container_test
4docker exec -it container_test bash
1docker run --name <container_name> -dp 3000:3000 -v todo-db:/etc/todos <docker_img>
1# run a command in a running docker without entering to that container
2# e.g. running "/usr/sbin/sshd -Ddp 22"
3docker exec -it -d docker_thi_dc /usr/sbin/sshd -Ddp 22
4# "-d" = Detached mode
1# want docker auto removes a container after exit
2docker run --rm ...

docker-compose.yml

Use to create various services with the same image.
1docker-compose up -d # up and detach
2docker-compose -f file_name.yml up -d # custom docker-compose.yml file name
3
4# if you run 2 container in the same folder name
5docker-compose -p "project_1" up -d
6docker-compose -p "project_2" up -d
To upgrade docker-compose file format from version 2.x to version 3.x, check this guide. Also check this: Compose file versions and upgrading.
🔅If there is no already built image, you can use a Dockerfile in the same place as docker-compose.yml. In docker-compose.yml, use
1services:
2	dataswati:
3		build: .
Then run docker-compose up --build.
🔅 Update to the newer version of docker-compose? Included in Docker Desktop (on Windows and MacOS), read this SO (Linux).

Errors

🐞 Docker can’t connect to docker daemon.
1# check if daemon is running?
2ps aux | grep docker
3
4# run
5sudo /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)
  1. Try running either sudo /etc/init.d/docker start or sudo service docker restart (twice if needed).
🐞 perl: warning: Please check that your locale settings: when using below in the Dockerfile,
1ENV LANG en_US.UTF-8ENV LANGUAGE en_US:enENV LC_ALL en_US.UTF-8# Replace them byRUN echo "LC_ALL=en_US.UTF-8" >> /etc/environmentRUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.genRUN echo "LANG=en_US.UTF-8" > /etc/locale.confRUN locale-gen en_US.UTF-8
🐞 On Windows + WSL2The process cannot access the file 'ext4.vhdx' because it is being used by another process.
  1. Quit docker.
  1. Open Task Manager, try to end the processes wsappx (all of them).
  1. Reopen docker.

Reference

Loading comments...