Docker

Anh-Thi Dinh
☝️
Use Ask AI feature on the official docs.

What and Why?

Abbreviate

This list isn’t complete.
  • 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

  • Client: OrbStack, official apps.
    • ☝️
      OrbStack supports containerName.orb.local, which points to the local server. It also supports logs, debugging, and opening a terminal inside the container.

References

General

1# docker's version
2docker --version
1# RAM & CPU usages
2docker stats
3docker stats <container_name>
1# Remove any resources
2docker system prune

Images

1# Build image with Dockerfile
2docker buildx build -t v2daimg .
3# docker build -t <img_name> .
4# image name must be in lowercase
5# run again to update the build
6
7# Set target platform for build
8docker buildx build -t v2daimg --platform=linux/amd64 .
9
10# To know the host platform within a running container
11docker run --rm <image_name> uname -m
12# eg: aarch64
13
14# don't use the cached layer from previous build
15docker build --no-cache -t img_name .
16
17# Custom Dockerfile.abc
18docker build -t <img_name> . -f Dockerfile.abc
1# Create an image from current container
2docker ps -a # Check all containers
3docker commit <container_id> <new_image_name>
1# list images on the host
2docker images
3
4# list all
5docker images -a
1# Where are images stored?
2docker info
3# normally, /var/lib/docker/
1# Rename an image
2docker image tag old:latest myname/new:latest
1# Remove a specific image
2docker image rm <IMAGE_ID>
1# Remove all unused images
2docker system prune -a
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

☝️
In Obstack, check Logs tab to see the logs inside a container! For example, when a container fails to start.
Creating a new container,
1# Quickly create a testing container from an image
2docker create --name container_test -t -i imageId bash
3docker start container_test
4docker exec -it container_test bash
1docker run --name <container_name> -dp 3000:3000 -v todo-db:/etc/todos imageId
-d = --detach (run in background), -p = --publish (map ports). 3000 (left) is the host port, 3000 (right) is the container host. -it to enable interactive mode (then use ctrl+p, ctrl+q to detach from running container). Without -it, ctrl+p, ctrl+q may not work!
1# run a container with docker-compose
2docker compose up
3# legacy: docker-compose up
4
5# with custom docker-compose file
6docker compose -f compose.admin.yaml up -d
7# docker-compose -f docker-compose.admin.yml up -d
-d = detach. -it not availbale for docker compose!
1# If you run 2 container in the same folder name
2docker compose -p "project_1" up -d
3docker compose -p "project_2" up -d
4# docker-compose -p "project_1" up -d
5# docker-compose -p "project_2" up -d
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
-d = Detached mode
1# want docker auto removes a container after exit
2docker run --rm ...
Working with created containers,
☝️
OrbStack supports containerName.orb.local, which points to the local server. It also supports logs, debugging, and opening a terminal inside the container.
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}}'
☝️
We can use sometimes interchangeable between <container_id> and <container_name>.
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>
Detach by: ctrl+p then ctrl+q. If it’s not working (iTerms), try ctrl+c.
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>
ctrl+p then ctrl+q to detach
1# Go inside a container
2docker exec -it containerId /bin/bash
Combine -it for interactive sessions, like running a shell.
Remove a container,
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)!

Troubleshooting