Docker Cheatsheet¶
Useful Links¶
Concepts¶
A Docker image is a read-only template. For example, an image could contain an Ubuntu operating system with Apache and your web application installed. Images are used to create Docker containers. Docker provides a simple way to build new images or update existing images, or you can download Docker images that other people have already created. Docker images are the buildcomponent of Docker.
Docker registries hold images. For orchestration, see Kubernetes Concepts.
Cheatsheet¶
To show only running containers, use the following command.
To show all containers, use the following command.
To show the last started container, use the following command.
To download an image from Docker Hub, use the docker pull command.
Create then start a container: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
* Docker run reference
Run with interactive terminal (i = interactive t = terminal):
Start then detach the container (daemonize):
If you want a transient container, docker run --rm will remove the container after it stops.
To look inside the container, use the logs command. You can also use -f to act like tail -f.
Stop container:
Delete container:
To check the environment:
Docker version / info:
Port Mapping¶
-p 80:5000 would map port 80 on our local host to port 5000 inside our container.
Full format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
Both hostPort and containerPort can be specified as a range of ports. When specifying ranges for both, the number of container ports in the range must match the number of host ports in the range, for example: -p1234-1236:1234-1236/tcp
The -P flag tells Docker to map any required network ports inside our container to our host (using random ports).
Linking¶
--link <name or id>:alias where name is the name of the container we’re linking to and alias is an alias for the link name.
The --link flag also takes the form: --link <name or id>
docker run -d --name myES -p 9200:9200 -p 9300:9300 elasticsearch
docker run --name myK --link myES:elasticsearch -p 5601:5601 -d docker-kibana-sense
Networks¶
Find out the container’s IP address:
Data Volumes¶
Create a new volume inside a container at /webapp:
You can also use the VOLUME instruction in a Dockerfile to add one or more new volumes to any container created from that image.
Mount the host directory, /src/webapp, into the container at /opt/webapp.
On Windows, use: docker run -v /c/Users/<path>:/<container path> ...
Example Dockerfile¶
FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay
Multi-stage Build Example¶
The following is a Dockerfile example showing a multi-stage build. Multi-stage builds are useful to optimize image sizes, separating the build environment from the runtime environment.
# Build stage.
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o myapp .
# Final stage.
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/myapp .
CMD ["./myapp"]
Docker Compose Example¶
compose.yaml (or compose.yaml) to define and run multi-container applications:
version: "3.8"
services:
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- api
api:
build: ./api
environment:
- DB_HOST=db
- DB_USER=postgres
ports:
- "5000:5000"
db:
image: postgres:16-alpine
environment:
- POSTGRES_PASSWORD=secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Run compose:
Docker Cleanup Commands¶
To remove all unused or dangling images, containers, volumes, and networks:
Multi-architecture Builds¶
Use buildx to build for multiple architectures (e.g., AMD64 and ARM64):