Search code examples
dockerboot2dockerdockerfiledocker-compose

Linking container with docker-compose


I try to run an instance with 2 containers, 1 container with mysql and other with node.

In docker-compose.yml file:

api:
  build: ./server
  ports:
    - 8001:8001
  links:
    - mysql:mysql

mysql:
  image: mysql
  environment:
    MYSQL_DATABASE: ghostDB
    MYSQL_ROOT_PASSWORD: root
  volumes:
    - /data/mysql:/var/lib/mysql

Dockerfile of server/:

FROM node:0.12

ENV PORT 8001
ENV MYSQL_DATABASE ghostDB
ENV MYSQL_USER root
ENV MYSQL_PASSWORD root
ENV MYSQL_HOST mysql
ENV MYSQL_PORT 3306

ENV API_DIR /usr/src/server-celerative

COPY . \${API_DIR}

WORKDIR \${API_DIR}

RUN npm install

RUN node index.js

index.js

var db = mysql.createConnection({
    host: 'mysql',
    port: '3306',
    user: 'root',
    password: 'root',
    database: 'ghostDB'
});

But i have got output:

Error: getaddrinfo ENOTFOUND mysql
    at errnoException (dns.js:44:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)
    --------------------

I don't understand why not work.

Anybody help?

NOTE: I use boot2docker.


Solution

  • docker-compose v1 cannot ensure that the mysql process is fully started and initialized before starting the node container. As a result, your node container is responsible for testing if mysql is yet available and for waiting/retrying until it is available.

    You either do this with node in your application (pooling-connections might be the way) ; or provide a boostrap shell script that will test the mysql connection and once available will start node.