Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose + external container

I have started a docker container with the following command

docker run --name mysql --restart always -p 3306:3306 -v /var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:5.7.14

and then would like to connect a wordpress site with the following docker-compose.yml file

version: '2'

services:
    wordpress:
        image: wordpress
        external_links:
            - mysql:mysql
        ports:
            - 80:80
    environment:
        WORDPRESS_DB_USER: root
        WORDPRESS_DB_PASSWORD: password
    volumes:
        - /var/www/somesite.com:/var/www/html

But I keep getting the following error

Starting somesitecom_wordpress_1
Attaching to somesitecom_wordpress_1
wordpress_1  | 
wordpress_1  | Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 19
wordpress_1  | 
wordpress_1  | MySQL Connection Error: (2002) Connection refused

It seems like the external_links isn't working.

Any idea what I am doing wrong?

like image 480
sbarow Avatar asked Aug 21 '16 18:08

sbarow


1 Answers

Your link is working, but you're on separate networks inside of Docker. From the docker-compose.yml docs:

Note: If you’re using the version 2 file format, the externally-created containers must be connected to at least one of the same networks as the service which is linking to them.

To solve this, you can create your own network:

docker network create dbnet
docker network connect dbnet mysql

Then configure your docker-compose.yml with:

version: '2'

networks:
  dbnet:
    external:
      name: dbnet

services:
    wordpress:
        image: wordpress
        ports:
            - 80:80
        environment:
            WORDPRESS_DB_USER: root
            WORDPRESS_DB_PASSWORD: password
        volumes:
            - /var/www/somesite.com:/var/www/html
        networks:
          - dbnet

Note with recent versions of Docker, you shouldn't need to link the containers, the DNS service should do the name resolution for you.

like image 142
BMitch Avatar answered Sep 21 '22 09:09

BMitch