“The ability to observe without evaluating is the highest form of intelligence.”

— Jiddu Krishnamurti

Prologue

This is a collection of useful Docker commands that is used but not remembered. 😎

This article will be updated from time to time.

You may also like: Linux Cheat Sheet
You may also like: Git Cheat Sheet


Basic Use

Build a container

1
docker build -t <image_name> .

Note that the . here is the build context, it means the build process can only access files under this directory. If you want to copy files from other directory, you may want to specify a common parent directory.

Run a container

1
2
3
4
5
docker run -it --rm -d --name <container_name>  \
-e ENV=value \
-v /host/path:/container/path \
--gpus all \
<image_name>

There are many options available for the docker run command. You can customize the container’s behavior by using various flags and parameters.

  • -it: run interactively with a terminal
  • --rm: automatically remove the container when it exits
  • -d: run the container in detached mode (in the background)
  • --name <container_name>: assign a name to the container
  • -e ENV=value: set environment variables, you can set multiple variables
  • -v /host/path:/container/path: mount a host directory as a data volume, can mount multiple volumes
  • --gpus all: use all available GPUs

Here, a name is recommended for easier management, so that you won’t bother searching for container IDs.

Attach to a running container

1
docker exec -it <container_name_or_id> /bin/bash

Copy files

Copy from container to host.

1
docker cp <container_name_or_id>:/path/to/file /host/path

Copy from host to container.

1
docker cp /host/path <container_name_or_id>:/path/to/file

Tricks

Remove all <none> images

1
docker rmi $(sudo docker images -f "dangling=true" -q)

Epilogue

To be continued…