SnackK
SnackK

Reputation: 23

Docker compose: scale both services

I have the following docker-compose:

version: '2'
services:
    cryptohds:
        image: cryptohds:cryptohds-2.0.0-SNAPSHOT
        environment:
            - APP_SLEEP=10 # gives time for the database to boot before the application
            - SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
            - SPRING_DATASOURCE_URL=jdbc:mysql://cryptohds-mysql:3306/cryptohds?useUnicode=true&characterEncoding=utf8&useSSL=false
            - SPRING_DATASOURCE_USERNAME=root
            - SPRING_DATASOURCE_PASSWORD=cryptohds
    cryptohds-mysql:
        extends:
            file: mysql.yml
            service: cryptohds-mysql

As for the mysql.yml:

version: '2'
services:
    cryptohds-mysql:
        image: mysql:5.7.20
        environment:
            - MYSQL_USER=root
            - MYSQL_ROOT_PASSWORD=cryptohds
            - MYSQL_ALLOW_EMPTY_PASSWORD=yes
            - MYSQL_DATABASE=cryptohds
        ports:
            - 3306
        command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8 --explicit_defaults_for_timestamp

When scaling cryptohds they scale fine, but they all use the same cryptohds-mysql. I want to scale cryptohds and each instance have their own cryptohds-mysql.

How can i achieve this? Should they be two services that share a network between them and then scale them individually? But in that case how can i use the SPRING_DATASOURCE_URL?

UPDATE

I've actually built a docker-compose that does what i need, but it's not dynamic. It specifically creates 2 instances of each, and i want a random number. Is there any other way of doing this dynamically? Or only by a script?

version: '2'
services:
    cryptohds:
        image: cryptohds:cryptohds-2.0.0-SNAPSHOT
        environment:
            - APP_SLEEP=10 # gives time for the database to boot before the application
            - SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
            - SPRING_DATASOURCE_URL=jdbc:mysql://cryptohds-mysql:3306/cryptohds?useUnicode=true&characterEncoding=utf8&useSSL=false
            - SPRING_DATASOURCE_USERNAME=root
            - SPRING_DATASOURCE_PASSWORD=cryptohds
    cryptohds-mysql:
        extends:
            file: mysql.yml
            service: cryptohds-mysql
    cryptohds_1:
        image: cryptohds:cryptohds-2.0.0-SNAPSHOT
        environment:
            - APP_SLEEP=10 # gives time for the database to boot before the application
            - SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
            - SPRING_DATASOURCE_URL=jdbc:mysql://cryptohds-mysql_1:3306/cryptohds?useUnicode=true&characterEncoding=utf8&useSSL=false
            - SPRING_DATASOURCE_USERNAME=root
            - SPRING_DATASOURCE_PASSWORD=cryptohds
    cryptohds-mysql_1:
        extends:
            file: mysql.yml
            service: cryptohds-mysql

Upvotes: 0

Views: 466

Answers (1)

BMitch
BMitch

Reputation: 263587

To run the containers as completely separate groups, I'd recommending defining them as separate "projects" in docker-compose. In swarm mode, the equivalent would be deploying separate "stacks". The project name defaults to your current directory name. You can override that with the -p option, e.g.:

docker-compose -p cryptohds2 up

Upvotes: 1

Related Questions