Docker Snippets
Image Management
Section titled “Image Management”# Create a docker setup with the current project# Go through guide based on project configurationdocker init
# Get an image, the latest versiondocker pull redis
# List images on localdocker images
# Compare imagesdocker scout compare ...
# Scan image for vulnerabilitiesdocker scout cves image-name:tag
# Build image, name with my-app and tag 1.0# Use Dockerfile in current directory .docker build -t my-app:1.0 .
# Tag an image with repository for pushed laterdocker tag my-app long-repository-name/my-app:1.0
# Push the imagedocker push long-repository-name/my-app:1.0
# Push an image to Docker Hub, assume logged in alreadydocker push myusername/myapp:latest
# Delete an imagedocker rmi image_id
# Create volumedocker volume create --name app-data1
# List volumesdocker volume ls
# See volume details, like file locationdocker inspect <volume-name>
# See history of, layers of an imagedocker history <image-name>Container, Run Management, Debugging and Logs
Section titled “Container, Run Management, Debugging and Logs”# Run Container in attached mode with terminal output# Command can also pull image if not present# Ctrl + c to terminatedocker run redis
# Run container in detached modedocker run -d redis
# Run a specific image with environment variablesdocker run -e POSTGRES_PASSWORD=mysecretpassword postgres:14.7
# Run redis with -p to find host port 6000 to container port 6379# Host ports must be available and not bound alreadydocker run -p 6000:6379 redis
# Run detached mode with port mapping and with specified namedocker run -d -p 6000:6379 --name redis-old redis
# Run with a named volume -v and path of data in container, requires volume already createddocker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3
# Check running containers and manage themdocker ps# Start, stop containersdocker stop id_of_containerdocker start id_of_container
# List running or not running containersdocker ps -a
# Check logsdocker logs container_id# Check logs with name, names are randomizeddocker logs container_name
# Run commands in container with exec# -it - interactive terminal# /bin/bash - which shell to usedocker exec -it container_id /bin/bash# Use shell if bash not availabledocker exec -it container_id /bin/sh# Enter container as a user, 0 for rootdocker exec -u 0 -it container_id bash
# Delete a containerdocker rm container_id
# List networkdocker network ls
# Use docker compose to start containersdocker-compose -f mongo-docker-compose.yaml up# Notice default network is created# and logs of both containers are shown
# Stop composed containers, remove networkdocker-compose -f mongo-docker-compose.yaml down
# Run specific commands from different imagesdocker compose run
# Watch changes and reload changes per compose.yml settings# Good for code changes and testingdocker compose watch
# Scan directory for vulnerabilitiesdocker scout quickview fs://Repositories
Section titled “Repositories”# Log into repositorydocker login ip_address:portdocker login 1.1.1.1:8083
# Log into Docker Hubdocker loginDockerfile and .dockerignore
Section titled “Dockerfile and .dockerignore”Dockerfile / Containerfile Good Practices
Section titled “Dockerfile / Containerfile Good Practices”Practices from Reddit discussion “A Containerfile that works isn’t necessarily a good Containerfile”
- WORKDIR - set working directory once
- Combine run instructions - reduce image layers
- Use smaller base image - use minimal base images when possible
- Use ENV / ARG - separate configuration from code
- Add USER - run the container as non-root
- Add LABEL - Add metadata about image
- Use multi-stage builds - build in one stage, copy only what is needed to run
There can be exceptions to above depending on context or making the container simple.
Optimizing Dockerfile, Containerfile
Section titled “Optimizing Dockerfile, Containerfile”Set of optimizations for Dockerfile [fn:1] and Dockerfile linter
[fn:2].
Prior to the last
COPY . . = command, make sure to ignore unneeded files in the =.dockerignore
file
node_modules/.git/*.log.envdist/coverage/Code is copied last with =COPY . . = so it is last in container layer.
FROM node:slimWORKDIR /appCOPY package.json package-lock.json ./RUN npm ciCOPY . .CMD ["npm", "start"]Dockerfile with builder image first to output binary and run binary on a minimal image (example alpine or scratch).
FROM golang:alpine as builderWORKDIR /buildCOPY . .RUN go build -o /app
FROM scratchCOPY --from=builder /app /build/appCMD ["/bin/app"]Minimal Python image requiring nginx so nginx doesn’t need to be run in a separate container or Kubernetes (K8s).
fROM python:slimRUN apt-get update \ && apt-get install -y --no-install-recommends nginx supervisor \ && rm -rf /var/lib/apt/lists/*
WORKDIR /appCOPY server.py /app/server.pyCOPY nginx.conf /etc/nginx/sites-enables/defaultCOPY supervisord.conf /etc/supervisor/conf.d/supervisord.confCMD ["supervisord", "-n", "-c", "/etc/supervisor/config.d/supervisord.conf"]