Docker

Anh-Thi Dinh

What and Why?

Abbreviate

Installation/code

For Desktop Client, use OrbStack!
👉
For all platforms, check the 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

MacOS

Windows

With GPUs support?

Uninstall

Linux

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