Docker
4 min read
Docker is a utility to pack, ship and run any application as a lightweight container. Another container engine is Podman

Usage
Running containers
Docker runs processes in isolated containers. A container is a process which runs on a host. The host may be local or remote. When an operator executes docker run, the container process that runs is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

Options
OptionDescription
-d, --detachRun container in background
-i, --interactiveAttach stdin, stdout, stderr
-t, --ttyAllocate a pseudo-TTY
--rmAutomatically remove the container when it exits
-p portExpose a port from container
-v src:targetBind a volume to the container

docker attach
Use docker attach to attach your terminal's standard input, output, and error (or any combination of the three) to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.

shell
docker attach [--no-stdin] container

docker cp
The docker cp utility copies the contents of SRC_PATH to the DEST_PATH. You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container. If - is specified for either the SRC_PATH or DEST_PATH, you can also stream a tar archive from STDIN or to STDOUT. The CONTAINER can be a running or stopped container. The SRC_PATH or DEST_PATH can be a file or directory.

shell
docker cp ./some_file CONTAINER:/work
docker cp CONTAINER:/var/logs/ /tmp/app_logs
docker cp CONTAINER:/var/logs/app.log - | tar x -O | grep "ERROR"

docker exec
The docker exec command runs a new command in a running container.

shell
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
docker exec -it mycontainer sh

Options
OptionDescription
-d, --detachDetached mode: run command in the background
-e, --env KEY=VALUESet environment variables
--env-file fileRead in a file of environment variables
-i, --interactiveKeep STDIN open even if not attached
-t, --ttyAllocate a pseudo-TTY
-u, --user uid:guidUsername or UID
-w, --workdirWorking directory inside the container

docker kill
The docker kill subcommand kills one or more containers. The main process inside the container is sent SIGKILL signal (default), or the signal that is specified with the --signal option. You can reference a container by its ID, ID-prefix, or name.

shell
docker kill [-s, --signal SIGNAL] CONTAINER [CONTAINER...]

docker logs
The docker logs command batch-retrieves logs present at the time of execution.

shell
docker logs [OPTIONS] CONTAINER

Options
OptionDescription
-f, --followFollow log output
--since tsShow logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail numNumber of lines to show from the end of the logs
--untilShow logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

docker ps
This command shows running containers.

shell
docker ps

Container Managment
You can use these commands to manage containers:

shell
docker restart container
docker rm container
docker start container
docker stop container

Building images
The docker build command builds Docker images from a Dockerfile and a "context". A build's context is the set of files located in the specified PATH or URL.
shell
docker build .

# Tag the image
docker build -t me/myimage:latest .

# Custom Dockerfile
docker build -f custom/Dockerfile .

Working with Images
shell
# List images
docker images

# Remove image
docker rmi image

# Tag image
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Load/Save images
You can save and load container images to/from tar archives.
shell
# Save
docker save [-o, --output outfile] image

# Load
docker load [-i, --input infile]

Sign into container registry
You can authenticate yourself with any container registry.
shell
docker login [-u, --username username] [--password pw] http://localhost:8080
docker logout http://localhost:8080

Push and Pull Images
You can push and pull container images to and from a registry.
shell
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
docker push [OPTIONS] NAME[:TAG]

Dockerfile
A Dockerfile is used for building container images. See Dockerfile.

Docker Compose
Docker Compose helps you manage multiple containers which work together. It can spin up a whole fleet of containers in one go. See Docker Compose.

Docker Swarm
Docker Swarm lets you group multiple hosts into a cluster for distributed container workloads. See Docker Swarm.