Useful docker compose configs
Last updated on December 2021
Here are some useful snippets for quickly getting some services set up with docker.
I've left some commented out lines that you may want to add back in. There is no focus on security in these example configurations. Please read up the documentation to understand what you are running before you run these.
How to use a docker-compose.yml file
Just in case you haven't used docker-compose... this is how:
- Make sure you have installed docker
- And then run the Docker app (in Mac OS - just run it from the Applications folder)
- Create a file called
docker-compose.yml
in your project (often at the root of the project, same place you would put your.env
orpackage.json
. - in the command line, change to the directory with that file (
cd ~/your-dir/
), and rundocker-compose up
(ordocker compose up
) - Once finished, run
docker-compose up -d
to run it detached (in the background), anddocker-compose down
to stop running it.
PostgreSQL in docker-compose.yml
Use this to run Postgres in docker-compose.
This is a typical docker-compose.yml
config for running postgres.
version: "3"
services:
db:
image: postgres
# restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
# volumes:
# - 'postgres-data:/var/lib/postgresql/data'
Redis docker-compose.yml
version: "3"
services:
cache:
image: redis
restart: always
ports:
- "6379:6379"
volumes:
- redis-vol:/data
volumes:
redis-vol:
driver: local
Mysql docker-compose.yml
version: "3"
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=mysql
- MYSQL_USER=mysql
- MYSQL_PASSWORD=mysql
- MYSQL_ROOT_PASSWORD=mysql
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
MongoDB + Mongo Express
note: this sets username/password both to mongodb. You should update these.
mongo:
image: mongo
# restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: mongodb
MONGO_INITDB_ROOT_PASSWORD: mongodb
mongo-express:
image: mongo-express
# restart: always
depends_on:
- mongo
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: mongodb
ME_CONFIG_MONGODB_ADMINPASSWORD: mongodb
ME_CONFIG_MONGODB_URL: mongodb://mongodb:mongodb@mongo:27017/
Port mapping in docker-compose.yml
In most of the examples above I've mapped the ports from the container to the host with the same ports. For example:
// ...
ports:
- "3306:3306"
// ...
The syntax is [host-port]:[container-port]
.
So if you were running a web server in the docker container (on port 80), you might want to access it on localhost:3001
, so you would set:
ports:
- "3001:80"
You can also define multiple port mappings, for example the following to expose port 80 and 443
ports:
- "80:80"
- "443:443"
How to use a different docker-compose.yml config
Use the -f
(file) flag to set what file to use.
You can use this to use alternative docker-compose files (such as one for a dev or testing environment)
docker-compose -f ./docker-compose.dev.yaml up
# or:
# docker compose -f ./docker-compose.dev.yaml up