Reputation: 323
I'm using docker-compose
to set up a Mariadb
database in a container and run a migration to get it ready for use. The issue is I'm getting an error of the following when doing so:
Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
This makes me think the database isn't set up at this point in the Dockerfile
but I could be wrong. Any ideas?
Dockerfile
contents:
FROM mariadb:10.0.37
ADD db /db
WORKDIR /db
RUN mysql --user userExample --password passwordExample < migrate.sql
docker-compose.yaml
contents:
version: '3'
services:
db:
build: .
environment:
- MYSQL_USER=userExample
- MYSQL_PASSWORD=passwordExample
networks:
example_network:
ipv4_address: 172.28.1.1
networks:
example_network:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
Upvotes: 0
Views: 940
Reputation: 3498
You are using the Dockerfile in the wrong way, confusing images with containers.
The RUN
command will fail because the container doesn't even exist at that point so there is nothing to connect to, thus you don't need to setup a Dockerfile at all.
If the migrate.sql
only needs to be read once with a clean database then you can mount the file on /docker-entrypoint-initdb.d/migrate.sql
. You can read about this directory on the docker-mysql documentation.
If you need to import it from an already existing database then just run the import command from the host or another container.
I don't recommend creating a mysql image with an embedded database because any changes will be lost when the container is restarted.
Upvotes: 1
Reputation: 8636
If you need to connect to Db from host, simplest thing ever you can do is to start this container in the network=host
mode. Thus container wont get it's own networking but instead will use host's one.
First update your docker-compose.yml
:
version: '3'
services:
db:
build: .
environment:
- MYSQL_USER=userExample
- MYSQL_PASSWORD=passwordExample
network_mode: "host"
Second launch it: docker-compose up
Third, connect from host using localhost
as database host name.
Upvotes: 1