evertramos / docker-wordpress

Wordpress Docker container using SSL Certificates with LetsEncrypt
https://evertramos.github.io/docker-wordpress/
MIT License
484 stars 156 forks source link

Split DB and WP service declarations into a separate docker-compose file #41

Closed Darksoulsong closed 4 years ago

Darksoulsong commented 5 years ago

This is not a bug, and I know this is not quite the place to ask for it, but I'd like to know your thoughts about something:

I have your build up and running, thanks to your help the other day.

I got this idea to create a separate directory for each of the WP websites and database, because it makes sense to have a separate container for each website. And I'd like to share the same db service between them.

But I'm not sure about how do I'd handle this, or how can I reference a db service that is declared in the db dir into "blog1" and "blog2"directories, for example. Can that be done? If so, how? And does this makes sense, that is, to share the same db service between websites, if the target is to be more efficient on the server resources (my VPS is limited to 2gb ram)?

Any help would be awesome!

This is what I have ATM:

    .
    ├── nginx-proxy
    │   └── docker-compose.yml
    └── blogs
        └── docker-compose.yml

docker-compose.yml

    version: "3"

    services:
      db:
        container_name: ${CONTAINER_DB_NAME}
        image: mariadb:latest
        restart: unless-stopped
        volumes:
          - ${DB_PATH}:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
          MYSQL_DATABASE: ${MYSQL_DATABASE}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}

      wordpress1:
        depends_on:
          - db
        container_name: ${CONTAINER_WP_NAME1}
        image: wordpress:latest
        restart: unless-stopped
        volumes:
          - ${WP_CORE}:/var/www/html
          - ${WP_CONTENT}:/var/www/html/wp-content
          - ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
        environment:
          WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME1}:3306
          WORDPRESS_DB_NAME: ${MYSQL_DATABASE1}
          WORDPRESS_DB_USER: ${MYSQL_USER1}
          WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD1}
          WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX1}
          VIRTUAL_HOST: ${DOMAINS1}
          LETSENCRYPT_HOST: ${DOMAINS1}
          LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL1}
        logging:
          options:
            max-size: ${LOGGING_OPTIONS_MAX_SIZE1:-200k}

      wordpress2:
        depends_on:
          - db
        container_name: ${CONTAINER_WP_NAME2}
        image: wordpress:latest
        restart: unless-stopped
        volumes:
          - ${WP_CORE}:/var/www/html
          - ${WP_CONTENT}:/var/www/html/wp-content
          - ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
        environment:
          WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME2}:3306
          WORDPRESS_DB_NAME: ${MYSQL_DATABASE2}
          WORDPRESS_DB_USER: ${MYSQL_USER2}
          WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD2}
          WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX2}
          VIRTUAL_HOST: ${DOMAINS2}
          LETSENCRYPT_HOST: ${DOMAINS2}
          LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL2}
        logging:
          options:
            max-size: ${LOGGING_OPTIONS_MAX_SIZE2:-200k}

    networks:
      default:
        external:
          name: ${NETWORK}

And this is where I want to be:

    .
    ├── db
    │   └── docker-compose.yml
    ├── blog1
    │   └── docker-compose.yml
    ├── blog2
    │   └── docker-compose.yml
    └── blog3
        └── docker-compose.yml

db container

    version: "3"

    services:
      db:
        container_name: ${CONTAINER_DB_NAME}
        image: mariadb:latest
        restart: unless-stopped
        volumes:
          - ${DB_PATH}:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
          MYSQL_DATABASE: ${MYSQL_DATABASE}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    networks:
      default:
        external:
          name: ${NETWORK}

blog1 container (blog2, blog3 and so on)

    version: "3"

    services:
      wordpress1:
        depends_on:
          - db // Does Docker knows this is db service I created outside it?
        container_name: ${CONTAINER_WP_NAME1}
        image: wordpress:latest
        restart: unless-stopped
        volumes:
            - ${WP_CORE}:/var/www/html
            - ${WP_CONTENT}:/var/www/html/wp-content
            - ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
        environment:
            WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME1}:3306
            WORDPRESS_DB_NAME: ${MYSQL_DATABASE1}
            WORDPRESS_DB_USER: ${MYSQL_USER1}
            WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD1}
            WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX1}
            VIRTUAL_HOST: ${DOMAINS1}
            LETSENCRYPT_HOST: ${DOMAINS1}
            LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL1}
        logging:
            options:
            max-size: ${LOGGING_OPTIONS_MAX_SIZE1:-200k}
    networks:
      default:
        external:
          name: ${NETWORK}
evertramos commented 4 years ago

Hello @Darksoulsong!

You should remove the depend_on instruction in your compose file... and only set the environment WORDPRESS_DB_HOST to your container name (${CONTAINER_DB_NAME}).

I think you will be fine with it.

Please let me know if you made through it.

cocapasteque commented 4 years ago

Highly interested in this also, trying to acheive the exact same thing.