Docker Compose
DevOps Docker Compose
Categories:
Common command
command | description |
---|---|
docker-compose build |
Build the images |
docker-compose up |
Start the container |
docker-compose up -d |
Start the container and run on the background |
docker-compose up --build |
Start the container and run rebuild the container |
docker-compose -f "./other-docker-compose.yml" up |
Specifiy different docker-compose.yml file |
docker-compose up {image name} |
Start the specific container from the docker-compose.yml |
docker-compose stop |
Stop the container |
docker-compose down |
Remove the container |
docker-compose rm |
Remove the container from memory |
docker-compose logs |
Get the logs |
docker-compose logs -f {CONTAINER_ID} |
Get the logs from the container |
docker-compose ls |
List running container |
docker-compose ps |
Show the active container |
docker compose --env-file ./config/.env.dev up |
Run with the different .env file |
docker-compose cp {CONTAINER_ID}:{SRC_PATH} {DEST_PATH} |
Copy files from the container |
docker-compose cp {SRC_PATH} {CONTAINER_ID}:{DEST_PATH} |
Copy files to the container |
Environment variable
db:
image: "postgres:${POSTGRES_VERSION}"
env variable | description |
---|---|
$VARIABLE or ${VARIABLE} |
Basic environment variable usage |
${VARIABLE:-default} |
if VARIABLE is unset or empty then use the default value |
${VARIABLE-default} |
if VARIABLE is unset then use the default value |
${VARIABLE:?err} |
if VARIABLE is unset or empty then exit with the err message |
${VARIABLE?err} |
if VARIABLE is unset then exit with the err message |
Container environment variable
Set environment variable
web:
environment:
- DEBUG=1
Pass environment variable
web:
environment:
- DEBUG
Specify the env file
web:
env_file:
- my-specific.env
Docker run env
docker compose run -e DEBUG=1 web python console.py
docker compose run -e DEBUG web python console.py
/etc/hosts
extra_hosts:
- "example.local:127.0.0.1"
Resource Limits
services:
redis:
image: redis:alpine
deploy:
resources:
# Can use up to
limits:
cpus: '0.50'
memory: 150M
# initial allocations
reservations:
cpus: '0.25'
memory: 20M
Restart policy
services:
app:
image: myapp
restart: always
depends_on:
- db
db:
image: postgres
restart: always
Restart policy | Description |
---|---|
no | Default restart policy. Does not restart a container |
always | Always restart the container until its removal |
on-failure | Restart a container if the exit code indicates an error |
unless-stopped | Restart a container irrespective of the exit code but will stop restarting when the service is stopped or removed |
Reference
- Overview | Docker Documentation
- Overview of docker compose CLI | Docker Documentation
- Environment variables in Compose | Docker Documentation
- dockerfile - Docker echo to /etc/hosts not working - Stack Overflow