Docker Compose Basics
Running multiple containers using docker-compose
Create my-docker-compose.yml file with the following contents:
################
version: '2'
networks:
main:
services:
my-postgres:
container_name: my-postgres
image: "postgres:9.4.8"
ports:
- "5432:5432"
environment:
- "POSTGRES_USER=myuser"
- "POSTGRES_PASSWORD=superpassword"
networks:
main:
aliases:
- my-postgres
my-mongo:
container_name: my-mongo
image: "mongo:3.2"
ports:
- "27017:27017"
networks:
main:
aliases:
- my-mongo
################
Pull the images
docker-compose -f ./my-docker-compose.yml -p mydockerproject pull
Start the containers
docker-compose -f ./my-docker-compose.yml -p mydockerproject up --build -d
Test Mongo DB container
docker run --network=mydockerproject_main -it --rm mongo:3.2 sh -c 'exec mongo "my-mongo:27017/local"'
Type 'exit' to quit
Test Postgres DB container
docker run --network=mydockerproject_main -it --rm postgres:9.4.8 psql --host my-postgres --username myuser --dbname myuser --port 5432
Password for user myuser: superpassword
Type '\q' to quit the terminal and container
Stop the containers
docker-compose -f ./my-docker-compose.yml -p mydockerproject down -v
Inspect Networks and Containers
docker network ls -q | xargs docker network inspect
Comments
Post a Comment