Quick References #
Docker Cleanup (Memory Management) #
#!/usr/bin/env bash
set -x
docker stop $(docker ps -a -q)
yes | docker system prune
yes | docker system prune --volumes
docker rm $(docker ps -a -q)
docker images -q --filter "dangling=true" | xargs docker rmi
docker rmi $(docker images -q)
Docker container and image cleanup #
docker images -f dangling=true -q | xargs docker rmi || true
Get IP Address of Docker image #
docker inspect --format '{{ .NetworkSettings.Networks.IPAddress }}' imagename
Docker Troubleshooting #
Docker logs command, as shown here:
docker logs --details containername
Delete all untagged images #
docker rmi $(docker images -q --filter "dangling=true") -f
Docker shell into alpine container #
docker run -it --rm alpine /bin/ash
Docker shell into debian container #
docker exec -it container_id /bin/bash
Remove all stopped containers #
docker container prune
Docker Compose #
Docker file example #
version: "3.9"
services:
redis:
image: redis:alpine
ports:
- "6379"
networks:
- frontend
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
max_replicas_per_node: 1
constraints:
- "node.role==manager"
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- "5000:80"
networks:
- frontend
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
restart_policy:
condition: on-failure
result:
image: dockersamples/examplevotingapp_result:before
ports:
- "5001:80"
networks:
- backend
depends_on:
- db
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 1
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
placement:
constraints:
- "node.role==manager"
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints:
- "node.role==manager"
networks:
frontend:
backend:
volumes:
db-data:
Build #
version: "3.9"
services:
webapp:
build: ./dir
version: "3.9"
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
Dockerfile #
build:
context: .
dockerfile: Dockerfile-alternate
ARGS #
ARG buildno
ARG gitcommithash
RUN echo "Build number: $buildno"
RUN echo "Based on commit: $gitcommithash"
build:
context: .
args:
buildno: 1
gitcommithash: cdc3b19
build:
context: .
args:
- buildno=1
- gitcommithash=cdc3b19
YAML boolean values (“true”, “false”, “yes”, “no”, “on”, “off”) must be enclosed in quotes, so that the parser interprets them as strings.
command #
Override the default command.
command: bundle exec thin -p 3000
command: ["bundle", "exec", "thin", "-p", "3000"]
credential_spec #
version: "3.9"
services:
myservice:
image: myimage:latest
credential_spec:
config: my_credential_spec
configs:
my_credentials_spec:
file: ./my-credential-spec.json|
depends_on #
version: "3.9"
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
LABELS #
version: "3.9"
services:
web:
image: web
deploy:
labels:
com.example.description: "This label will appear on the web service"
entrypoint #
entrypoint: /code/entrypoint.sh
entrypoint: ["php", "-d", "memory_limit=-1", "vendor/bin/phpunit"]
env_file #
env_file: .env
env_file:
- ./common.env
- ./apps/web.env
- /opt/runtime_opts.env
environment #
environment:
RACK_ENV: development
SHOW: 'true'
SESSION_SECRET:
environment:
- RACK_ENV=development
- SHOW=true
- SESSION_SECRET
external_links #
Link to containers started outside this docker-compose.yml or even outside of Compose, especially for containers that provide shared or common services. external_links follow semantics similar to the legacy option links when specifying both the container name and the link alias (CONTAINER:ALIAS).
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
extra_hosts #
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
restart #
restart: "no"
restart: always
restart: on-failure
restart: unless-stopped
Volume configuration reference #
version: "3.9"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
backup:
image: backup-service
volumes:
- data-volume:/var/lib/backup/data
volumes:
data-volume: