Diego Orellana
Diego Orellana

Reputation: 1024

Error while executing mysql commands inside docker-compose

When I execute the xdcomp_my_sql_client command it pings the ip but then when it tries to reach the mysql server it fails. If I do the exact same command once the container is running it works. It seems that the mysql server is not running at the moment the command is executed. But I have used the "depends_on" command, so what I'm doing wrong?

Thank you.

version: '2'

services:
  xdcomp_my_sql_server:
    image: mysql/mysql-server:latest
    environment:
      MYSQL_ROOT_PASSWORD: diego
      MYSQL_USER: otro
      MYSQL_PASSWORD: otro
      MYSQL_ROOT_HOST: 172.28.0.101
    networks:
      SQLNetwork:
        ipv4_address: 172.28.0.102

  xdcomp_my_sql_client:
    build: .
    command: sh -c 'ping -c 5 172.28.0.102 && mysql -h 172.28.0.102 -u root -pdiego sys < /lafayette/forensic.sql && tail -f /etc/hostname'
    ports:
      - 83:80
    networks:
      SQLNetwork:
        ipv4_address: 172.28.0.101
    depends_on:
      - xdcomp_my_sql_server


networks:
  SQLNetwork:
    driver: "bridge"
    ipam:
      config:
        - subnet: 172.28.0.0/24
          gateway: 172.28.0.201

Client Dockerfile

FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y mysql-client
RUN apt-get install -y git
RUN git clone https://github.com/linkedin/lafayette
RUN apt-get update
RUN apt-get install -y python-dnspython
RUN apt-get install -y python-pip
RUN pip install Flask
RUN pip install python-dateutil
RUN apt-get install -y python-mysqldb
RUN pip install requests
RUN pip install multiprocessing
RUN pip install multiprocess
RUN apt-get install -y vim
RUN apt-get -y install iputils-ping

Upvotes: 1

Views: 377

Answers (2)

Diego Orellana
Diego Orellana

Reputation: 1024

The problem was the following: the mysql server was not running at the time the client command was executed. Even when the key "depends_on" was present (see:https://docs.docker.com/compose/startup-order/) For that reason the solution was to wait for the mysql server. This was done with the following command:

command: sh -c 'until nc -z -v -w20 172.28.0.102 3306; do sleep 1; echo "Waiting for mysqlserver to come up..."; done && ping -c 5 172.28.0.102 && mysql -h 172.28.0.102 -u root -pdiego sys < /lafayette/forensic.sql && tail -f /etc/hostname'

The wait for was made by the following line:

until nc -z -v -w20 172.28.0.102 3306; do sleep 1; echo "Waiting for mysqlserver to come up..."; done

nc= netcat command, it check the connection to a given ip and port

Upvotes: 0

Bukharov Sergey
Bukharov Sergey

Reputation: 10185

Working with docker includes 2 steps:

  • building a image
  • creating container based on image and running this container

The point is you mysql client can connect to server only on second step.

Dockerfile "executes" while you build image. In this time you don't have any containers, therefore you can`t connect to them. You need move command

RUN cd lafayette && mysql –h 172.25.0.102 –u root –ppass sys < forensic.sql

from Dockerfile to entrypoint.sh

Can you show me your Dockerfile? I will try to fix the issue

Upvotes: 1

Related Questions