What I want to do:
- Create a container that runs a mariadb server
- Run some custom query before the container is built. (Not at the start up)
What I've done:
- I've created a
docker-compose.yml
file where I describe my service and for each component, I've created a dockerfile inserting the command to run. I obtain the shown error. - I've added some sleep second. Nothing changed
- I've tried to create a second service depending on the database and run from there the queries. Nothing changed, still the same error.
Where is the problem?
I cannot execute any command until every service is built and running. In the following dockerfile:
FROM mariadb:latest
ARG MYSQL_ROOT_PASSWORD
RUN mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE test"
I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Here is the docker-compose.yml
file:
version: '3.1'
services:
db:
image: mariadb:latest
build:
context: ./db
args:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
container_name: IUB_sql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
MYSQL_DATABASE: "useless_test_db"
MYSQL_USER: "${MYSQL_USER}"
MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
ports:
- ${MYSQL_PORT}:3306
Note:
I do not want to create only a database, I want to run some arbitrary queries.
Note 2:
If I delete the RUN mysql ...
line the service goes up. Then if I try to run the following command I'm able to run successfully the query:
docker exec -umysql -it IUB_sql mysql -uroot -p -e "CREATE DATABASE test_cmdline"