Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

557
Views
Docker - MySQL commands within Dockerfile using RUN (ERROR 2002)

I am using Docker to create a dockerfile with mysql as the base image:

FROM mysql
#set root pass
ENV MYSQL_ROOT_PASSWORD password
#update linux
RUN apt-get update
#create database
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
#install vim
RUN apt-get install vim -y

The dockerfile fails on the step where I try to create a database, it doesn't finish building and i receive this error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

When I remove the #create database run command the dockerfile will build and I am able to run a container from that image. I know that it isn't a problem with the mysql server as I can enter the container and run the mysql command manually with success and the service status is running.

Using an environment variable i.e MYSQL_ROOT_PASSWORD within the file also allows me to create a database successfully but this will only work with a single database, I need to be able to use the mysql command to make queries, such as creating additional databases / assigning users etc.

This may be because I need to specify the host and port of the docker container but this still does not allow me to connect

RUN mysql -u root -ppassword -h 127.0.0.1 -P 3308 -e "CREATE DATABASE dbname"

Strangely, doing this also often crashes the container and puts it in a state where it will crash again on start-up every time that I try to restart it again.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I think the issue might be that in the service hasn't started within the container used to build your Dockerfile.

  • Try starting and configuring MySQL server within a single step. As a reference please check this file: https://github.com/dockerfile/mysql/blob/master/Dockerfile
over 4 years ago · Santiago Trujillo Report

0

Use below-given commands in your Dockerfile:

RUN service mysql restart && echo 'CREATE DATABASE db_name;' | mysql -uroot -
pYOUR_ROOT_PASSWORD
over 4 years ago · Santiago Trujillo Report

0

Had the very same problem: When starting the container and running a set of RUN instructions, or .sh or .sql scripts in /docker-entrypoint-initdb.d/ no connection to the database server could be established.

I found the solution by a comment of @wpalmer on the mysql-image:

The init scripts run by the entrypoint, internally, use the variable "${mysql[@]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).

So what this means for you, instead of providing the plain mysql command with user, pass etc. as in

RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"

use the placeholder instead:

RUN "${mysql[@]}" -e "CREATE DATABASE dbname"
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!