Can't connect to MySQL server using docker-composer

I’m using the docker-toolbox for Windows 7. What I want to do is setup a container with MySQL and load the mysql dump that I have. To do this, I decided to use the docker composer. Here is the content of my Dockerfile and docker-compose.yml

Dockerfile

FROM mysql:5.5.55
RUN apt-get update
RUN mkdir /dumps
WORKDIR /dumps
ADD my_dump.sql /dumps/my_dump.sql
RUN mysql -u root -h 0.0.0.0 -e "create database legacy_datasite"
ADD . /dumps

docker-compose.yml

version: '3'
services:
    db:
        build: .
        volumes:
            - ~/data-site-mysql/db_data:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: your_password
            MYSQL_DATABASE: legacy_datasite
        ports:
            - 3306:3306

However, when I created the container using the command

docker-compose run --service-ports db mysql -h 0.0.0.0 -u root -p

It returns the following error:

ERROR 2003 (HY000): Can’t connect to MySQL server on ‘0.0.0.0’ (111)

I tried other host like 127.0.0.1, 192.168.99.100 (docker machine IP), 127.0.0.1, and even db (service name) because of this suggestion.

Unfortunately, none of them worked. Any suggestions on how to fix this? Thank you very much.

The commands in the Dockerfile are in the internal context of the container, they’re not run from outside. So I don’t think you need to specify -d in the command that you run in the Dockerfile. The image instructions has details on how to initialize the container with .sql file: https://hub.docker.com/_/mysql/

1 Like

Thanks. Just decided to follow the instructions in mysql hub page first before proceeding to setting up a composer file.