0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think my problem is not the code else the link of docker-compose. I Cannot connect with 'mysql' host because the container host is not accesible?. Also, i tried changing the code, and doesn't work :/
isn't that the purpose of depends_on or the link syntax for compose v1?docs.docker.com/compose/compose-file/compose-file-v1/#links ie it determines container startup based on that
in compose v1, the link directive hints docker-compose on the start order of containers, but won't ensure a service is ready to serve requests in a container before starting the next one. In compose v2, the depends_on syntax was introduced and, for the starting order, works the same way as link. Since compose v2.1 you can ensure a service is ready before starting the dependee container ; for that you have to use a combination of depends_on and healthcheck
0

Add the line container_name: mysql in the mysql section after image: mysql.

The following Bash code could be added to be sure that the 3306 port is open:

while ! ( exec 2>/dev/null ; echo > /dev/tcp/mysql/3306 ) ; do sleep 1 ; done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.