R
R
Roman Dubinin2018-10-16 08:00:42
Docker
Roman Dubinin, 2018-10-16 08:00:42

How to use one database for multiple sites running on Docker?

Several websites are running on the server. Autodeploy via gitlab is configured as follows:
1. After the build, the image is uploaded to the server
2. Docker-compose.yml is loaded into the project folder (which contains the .env file, mount folders, etc.) something like this (deleted what off topic):

version: '3.2'
services:
  web:
    image: project-name
    environment:
    - DB_HOST=${DB_HOST:-db}
    - DB_DATABASE=${DB_DATABASE:-project-name}
    - DB_USER=${DB_USER:-project-name}
    - DB_PASSWORD=${DB_PASSWORD:-password}
  db:
    image: mariadb:10.3.8
    deploy:
      resources:
        limits:
          memory: ${LIMIT_DB_MEM:-80M}
    environment:
    - MYSQL_RANDOM_ROOT_PASSWORD=yes
    - MYSQL_DATABASE=${DB_DATABASE:-project-name}
    - MYSQL_USER=${DB_USER:-project-name}
    - MYSQL_PASSWORD=${DB_PASSWORD:-password}

3. To load environment variables from .env:
docker-compose config > docker-stack.yml
4. And:
docker stack deploy -c docker-stack.yml project-name

I don’t have a cluster as such, there is only one server and the docker stack is used exclusively for deploy-resources-limits, without which my small vps sometimes chokes.
Nevertheless, for each of the three projects, as a result, three separate DB instances were launched, each of which I can allocate on sale in the region of 100-150 MB, which is very unpleasant.
Therefore, I want to run one database (preferably also in docker) and connect all sites to it, while not launching their databases specified in docker-compose.yml.
But for developers, everything should remain as it was: docker-compose up --buildand everything rises by itself, no fuss with the database, network, etc.
My question is: can the current configuration be reworked so that when certain variables in .env (for example, when DB_HOST is not equal to db), the db service does not start? And how, in this case, to give web services access to an external database using the docker-compose file and environment variables (I am very weak in docker networks)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qq1, 2018-10-16
@romash

My question is: can the current configuration be reworked so that when certain variables in .env (for example, when DB_HOST is not equal to db), the db service does not start?

It would be more correct to make a separate docker-compose.prod.yml in which there will be no db service.
The database will run in a separate stack, with its own docker-compose.yml and its own network, which must be declared in docker-compose.prod.yml as external:
https://docs.docker.com/compose/networking/#use -a-...
networks:
  default:
    external:
      name: db_default
...
services:
  web:
    image: project-name
    networks:
      - default
      - db_default

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question