Genarito
Genarito

Reputation: 3433

Make Docker Swarm use same volumes from Docker-Compose

I have a docker-compose.yml file with some services:

version: '3'

services:
    # PostgreSQL
    db:
        image: postgres:12
        volumes:
            - postgres_data:/var/lib/postgresql/data/

    # MongoDB Server
    mongo:
        image: mongo:4.2
        volumes:
            - mongo_data:/data/db
            - mongo_data:/data/configdb

volumes:
    mongo_data:
    mongo_config:
    postgres_data:

I've been working in production starting the services with docker-compose up -d. But now I want to use docker stack deploy --compose-file docker-compose.yml mystack for future scalability.

The problem is that the services are now using new volumes. So I can't use all the DB data. How could I make Swarm to use the existing docker-compose volumes?

Any kind of help would be really appreciated. Thanks in advance

Upvotes: 0

Views: 2877

Answers (2)

Genarito
Genarito

Reputation: 3433

As Kiryl pointed out, the answer was the external volumes but there were several steps to follow, which I list below:

  1. Inspect the containers to get the volumes they're using and get their names. This answer indicates how to do that.
  2. Edit the docker-compose.yml file to add the external key with the name of the existing volumes. The result was as follows:
volumes:
    mongo_data:
        external:
            name: existing name...
    mongo_config:
        external:
            name: existing name...
    postgres_data:
        external:
            name: existing name...
  1. Start with docker-compose up -d to check that all the volumes are OK. Then shutdown with docker-compose down
  2. Deploy the stack with docker stack deploy --compose-file docker-compose.yml mystack

Also, there's the same question in the official Docker Forum where there are other valuable solutions and opinions.

Upvotes: 4

Kiryl
Kiryl

Reputation: 1511

Consider using external volumes

Upvotes: 2

Related Questions