Docker Compose
April 24, 2020
I wanted to have three docker containers that were managed as one, being spun up and torn down together. These were a postgres database, pgsql postgres admin tool, and a spring boot back end (the one from https://drumcoder.co.uk/blog/2020/apr/24/docker-spring-boot/)
docker-compose.yml
Here's the docker-compose.yml file I used:
version: "3" services: todo-backend: image: drumcoder/todo-backend:latest container_name: todo-backend depends_on: - todo-db ports: - 5400:8080 db-admin: image: dpage/pgadmin4:latest container_name: todo-pgadmin environment: - PGADMIN_DEFAULT_EMAIL=email@here.com - PGADMIN_DEFAULT_PASSWORD=sekr1t depends_on: - todo-db ports: - 7777:80 todo-db: image: postgres:latest container_name: todo-db environment: - POSTGRES_DB=todo - POSTGRES_USER=todo - POSTGRES_PASSWORD=sekr1t ports: - 5432:5432
Once setup, all three the containers can be spun up using docker-compose up
.
The database connection inside the spring boot app was set to use:
spring.datasource.url = jdbc:postgresql://todo-db:5432/todo spring.datasource.username = todo spring.datasource.password = sekr1t
Note the use of the container name to be able to refer to the host for the database.
Docker compose will create a separate virtual network and run all these components inside of it. This can be viewed using docker network ls
NETWORK ID NAME DRIVER SCOPE 2ae18094fbe2 bridge bridge local 8045e0043cc0 host host local ea14b8e7f412 none null local ce389a7c2478 todo_default bridge local