Nam Hoai NguyenMy taking notes

Docker

Nam-Hoai Nguyen
added
updated

What & Why?

  • Docker is an open-source virtualization technology that makes it easy to build, test and deploy applications.
  • You can ship your applications in a container environment that houses everything your application need to run (tools, config, code,…)

Abbreviate

  • ps = process status: check running container (with a argument to show all)
  • i = interactive: used in docker exec or docker run
  • t = terminal: used in docker exec or docker run
  • m = memory
  • v = volume: mount volume in/out containers
  • -rm = remove: create temporary a container (will be removed after exits)

Installation

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

References

General

# docker's version
docker --version

# Remove any resources
docker system prune

# where're images stored?
docker info # normally: /var/lib/docker
# ram & cpu usages
docker stats
docker stats <container_name|container_id>

# get ip address
docker inspect <container_name|container_id> | grep IPAddress

# check image's info
docker inspect <image_id>

Images

# Build image with Dockerfile
docker buildx build -t v2daimg .
# docker build -t <img_name> .
# image name must be in lowercase
# run again to update the build

# Set target platform for build
docker buildx build -t v2daimg --platform=linux/amd64 .

# To know the host platform within a running container
docker run --rm <image_name> uname -m
# eg: aarch64

# don't use the cached layer from previous build
docker build --no-cache -t img_name .

# Custom Dockerfile.abc
docker build -t <img_name> . -f Dockerfile.abc
  • More about --platform
# list images on the host
docker images

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

# Remove all unused images
docker system prune -a
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
If you use docker image -a and see a lot of <none>:<none> images. Don’t worry to fast, if they’re dangling images, they take spaces. Otherwise , they’re harmless to you drive!. Check this StackOverflow 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
# Quickly create a testing container from an image
docker create --name container_test -t -i imageId bash
docker start container_test
docker exec -it container_test bash
docker 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+pctrl+q to detach from running container). Without -itctrl+pctrl+q may not work!
# run a container with docker-compose
docker compose up
# legacy: docker-compose up

# with custom docker-compose file
docker compose -f compose.admin.yaml up -d
# docker-compose -f docker-compose.admin.yml up -d
-d = detach. -it not availbale for docker compose!
# If you run 2 container in the same folder name
docker compose -p "project_1" up -d
docker compose -p "project_2" up -d
# docker-compose -p "project_1" up -d
# docker-compose -p "project_2" up -d