Docker and docker-compose
table of contents
Helpful services
Docker commands
Reclaim disk space
Clean up disk space by removing all the docker containers older than 14 days.
docker image prune -a --filter "until=336h"
See Prune unused Docker objects
Run a container and attach current directory
Example for a specific version of Python.
docker run -v $(pwd):/app --rm -it python:3.10 bash
Docker-compose Snippets
PostgreSQL
Sample docker-compose file to run a PostgreSQL server.
# The storage can be cleaned up by running the command:
# docker-compose down -v
#
# Django-environ settings:
# DATABASE_URL=psql://myproject:password@127.0.0.1:5499/myproject
version: '3.3'
services:
db:
image: postgres:14
environment:
POSTGRES_USER: myproject
POSTGRES_PASSWORD: password
POSTGRES_DB: myproject
ports:
- '127.0.0.1:5499:5432'
volumes:
- 'postgres-data:/var/lib/postgresql/data'
volumes:
postgres-data: {}
MongoDB
Sample docker-compose file to run a MongoDB server.
# Sample docker-compose file to run a MongoDB server and
# store data in a persistent storage.
#
# The storage can be cleaned up by running the command:
# docker-compose down -v
#
# Connect to this instance from the command-line:
# mongo --port 27019
#
# Connect to this instance from Python:
#
# MONGO_URL = "mongodb://localhost:27019"
# from pymongo import MongoClient
# client = MongoClient(MONGO_URL)
version: '3.3'
services:
mongo:
image: mongo:latest
ports:
- '127.0.0.1:27019:27017'
volumes:
- 'mongo-data:/data'
volumes:
mongo-data: {}
Redis
Sample docker-compose file to run a Redis server.
# Sample docker-compose file to run a Redis server
# store data in a persistent storage.
#
# The storage can be cleaned up by running the command:
# docker-compose down -v
#
# Connect to this instance from the command-line:
# redis-cli -p 6314
#
# Connect to this instance from Python:
#
# REDIS_URL = "redis://localhost:6399"
# redis = Redis.from_url(REDIS_URL)
version: '3.3'
services:
redis:
image: redis:alpine
ports:
- '127.0.0.1:6399:6379'
volumes:
- redis-data:/data
volumes:
redis-data: {}
Keycloak
Keycloak with the standalone database.
version: '3.3'
services:
keycloak:
image: quay.io/keycloak/keycloak:latest
ports:
- '127.0.0.1:8080:8080'
volumes:
- keycloak-data:/opt/jboss/keycloak/standalone/data/
environment:
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: password
volumes:
keycloak-data: {}
See Get started with Keycloak on Docker.
Fake email with Inbucket
With Inbucket.
version: '3.3'
services:
# SMTP server is available at localhost:2500 or inbucket:2500.
# The web UI is available at http://localhost:9000.
# POP3 interface is at localhost:1100
inbucket:
image: inbucket/inbucket
ports:
- '9000:9000'
- '2500:2500'
- '1100:1100'
Configuration with django-environ .
EMAIL_CONFIG = env.email("EMAIL_URL", default="smtp://localhost:2500")
vars().update(EMAIL_CONFIG)
Fake email with MailHog
With MailHog.
version: '3.3'
services:
# SMTP server is available at localhost:1025 or mailhog:1025.
# The web UI is available at http://localhost:8025.
mailhog:
image: mailhog/mailhog
ports:
- '1025:1025'
- '8025:8025'
Configuration with django-environ .
EMAIL_CONFIG = env.email("EMAIL_URL", default="smtp://localhost:1025")
vars().update(EMAIL_CONFIG)
Sending an email from Django to see if it works:
from django.core.mail import send_mail
send_mail("subject", "message", "noreply@example.com", ["admin@example.com"])