5

I would like to run my Wordpress site on Docker, and I want to connect the Wordpress database to another container which have only the databases of all my sites.

For doing so, I've created a LAMP container using the following docker-compose.yml:

version: "3"

services:
  web: 
    image: webdevops/php-apache:alpine-php7
    ports:
      - "4500:80"
    volumes: 
      - ./www:/app
      - ./uploads.ini:/opt/docker/etc/php/php.ini

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "8088:80"

as you can see I've installed Apache as service using the webdevops image, this return the following:

enter image description here

Then, I've created a new container which have the Wordpress instance:

version: '3'

    services:
      wordpress:
       depends_on:
         - db
       image: wordpress:latest
       volumes:
         - ./wp/wp-content:/var/www/html/wp-content
       ports:
         - "8000:80"
       restart: always
       environment:
         WORDPRESS_DB_HOST: lamp_db_1:3306
         WORDPRESS_DB_USER: root
         WORDPRESS_DB_PASSWORD: root
    
    volumes:
      dbdata:
      wp-content:

as you can see I mount the wp-content folder since I already have a Wordpress installation with plugins and media... then I tried to connect this container to lamp_db_1 container but when I run this using:

docker-compose up --build

I get:

MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known

what I did wrong?

How can I connect the wordpress container to the LAMP container?

sfarzoso
  • 1,356
  • 2
  • 24
  • 65

2 Answers2

2

You can specify a custom network in your LAMP stack and have your Wordpress stack and other containers defined in other Compose files use this network by using network.external parameter in Compose.

Being on the same network, you'll be able to join your lamp_db container using it's container name as hostname.


A complete example:

docker-compose.yml for LAMP stack:

version: '3.5'
services:
  db:
    # This will be the hostname for your DB container on the network
    container_name: lamp_db
    image: mysql
    ports:
    - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: root
    # Have db container join our db network
    networks:
    - db_network

# Ensure our custom network is managed by this stack
networks:
  db_network:
    name: db_network

docker-compose.yml for Wordpress stack:

version: '3.5'

services:
  wordpress:
    image: wordpress:latest
    ports:
    - "8000:80"
    environment:
      # Name of the db container
      WORDPRESS_DB_HOST: lamp_db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: root
    # Have wordpress container join our db network
    networks:
    - db_network

# Declare db_network as external with it's name
# Network won't be created by this stack but must exists before running
# See https://docs.docker.com/compose/compose-file/#external-1
networks:
  db_network:
    external: true
    name: db_network

And a generic example any service that would need to use our database:

version: '3.5'

services:
  some_container:
    image: some-image:latest
    # Have this container join our db network
    networks:
    - db_network
    # Note: make sure to reference db container by it's hostname lamp_db

networks:
  db_network:
    external: true
    name: db_network

Notes:

  • db_network is managed via LAMP stack. When uping the LAMP stack, Docker Compose will ensure this network is created. Other stacks using this network should declare it as external: true with it's name
  • You'll need version: '3.5' or more to be able to use network.name config
Pierre B.
  • 11,612
  • 1
  • 37
  • 58
1

It seems the reason you are running into this issue is due to the fact that you have two separate services. One contains your LAMP stack and the other contains your wordpress image.

When you run docker-compose it sets up a single network for your app. Thus your wordpress image isn't on the same network as your LAMP stack.

You can add your wordpress container to your LAMP docker-compose.yml file such as

version: "3"

services:
  web: 
    image: webdevops/php-apache:alpine-php7
    ports:
      - "4500:80"
    volumes: 
      - ./www:/app
      - ./uploads.ini:/opt/docker/etc/php/php.ini

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "8088:80"

  wordpress:
   depends_on:
     - db
   image: wordpress:latest
   volumes:
     - ./wp/wp-content:/var/www/html/wp-content
   ports:
     - "8000:80"
   restart: always
   environment:
     WORDPRESS_DB_HOST: db:3306
     WORDPRESS_DB_USER: root
     WORDPRESS_DB_PASSWORD: root
    
volumes:
  dbdata:
  wp-content:

And unless you are doing something specific with your php-apache image you can actually remove it at this point.

Another approach you could take if you do not want everything in the same docker-compose file is that you can manually define the networks and get your containers to join them. The answers on this question show how to do that fairly easily.

DCCoder
  • 1,587
  • 4
  • 16
  • 29
  • Hi, thanks for the answer! I understood that I've two different networks, but what about if I have multiple wordpress instances? Should I attach them in the same `docker-compose.yml`? It's a good practice? – sfarzoso Sep 25 '20 at 08:23
  • That really all depends on if you would consider them one application. Docker-compose provides a way of basically grouping together several containers as a single application (ex. Wordpress, database, and phpmyadmin). I personally would ensure each wordpress instance had its own database and if the db is going to be in a container then a docker-compose file containing two containers (db and wordpress) for each site. – DCCoder Sep 25 '20 at 08:27