depends_on

Using the depends_on option, you can specify the order in which the service should start. One service can have dependencies on more than one service, if that's needed.

Let's review the following docker-compose.yaml file, which uses both of these options:

version: '3.1'
services:
database:
image: mysql:5
ports:
- "3306:3306"
volumes:
# Use this option to persist the MySQL data in a shared
volume.
- db-data:/host/absolute/path/.mysql
environment:
- MYSQL_ROOT_PASSWORD=example
- MYSQL_DATABASE=demo

application:
image: enriquezrene/docker-compose-banking-app:1.0
ports:
- "8081:8080"
depends_on:
- database
environment:
- spring.datasource.url=jdbc:mysql://database:3306/demo
- spring.datasource.password=example
links:
- database

volumes:
db-data:

The depends_on and links options in the preceding code are highlighted in bold. It's pretty easy to understand from this that the application connects to the database server once the database is up.

The enriquezrene/docker-compose-banking-app: 1.0 image has a Spring Boot application running inside of it. As part of this application, we have the configuration file named application.properties with the following content:

spring.thymeleaf.cache=false
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.password=root

You may notice that the password and data source URL parameters are already provided. However, Spring offers the ability to override these configurations by using environment variables, as we did in the docker-compose.yaml file.

Docker Compose is easy to use, and it has the same options as Docker. Let's quickly review some commands that will allow us to start using it.

This command allows us to start all the containers listed in the configuration file:

docker-compose up

The up command also allows the -d flag to run all processes as a daemon. If you want to, you can start only one service from the docker-compose.yaml file specifying the service name. Let's say we only want to run the database server. The command that allows you to perform this action is the following:

$ docker-compose up database

In this way, you can specify the service name for the other commands available in Docker Compose.

Once the services are up, you can list all the containers that are running using the following command:

$ docker-compose ps

If you want to stop all the commands that were started, you will need to use the following command:

$ docker-compose stop

Docker Compose is comprised of a large set of commands. For a complete reference, you can visit https://docs.docker.com/compose/reference/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.86.18