I no longer have the error, but apparently this did not work since the command create database lba did not even work.
Indeed, in phpmyadmin, I do not find my database lba.
Strange, right?
PS: sorry for my english, i’m french
By changing with:
RUN /etc/init.d/mysql start
RUN mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE lba"
RUN mysql -u root -p${MYSQL_ROOT_PASSWORD} -D lba < /usr/sql/sources.sql
I get an error with socket :
Step 8/9 : RUN mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE lba"
---> Running in e870dc8837f2
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
ERROR: Service 'mysql' failed to build: The command '/bin/sh -c mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE lba"' returned a non-zero code: 1
your first RUN statements startsthe database in background and creates an image based on this
your second RUN starts the image. the previously started background does not run. So any attempt to access the database fails then.
if you want to start the database and import something, then you need to do it in one RUN statement. Probably also start database and wait a fair amount of time, before try to access teh database.
tried that already, didnt work out.
Mysql start, mysql user create and import in a script, copied it to container, changed permissions to execute. but didnt help.
Check “Initializing a fresh instance” in https://hub.docker.com/_/mysql/ . You just need to mount *.sh or *.sql in /docker-entrypoint-initdb.d then the files are treated on launching, respectively.
Thanks @piglovesyou, you saved me! I’ve tried and tried for ages to figure out how to do that, finally!
I think it’s a bummer that they didn’t document that on their Docker Hub image page / github.
Now all set with this line in my Dockerfile:
can you please share your docker file.
I am facing same problem. I tried many thing but unable to create and import data base using Dockerfile so can you please help me.
Dockerfile:
FROM mysql
COPY wpt-db.sql /docker-entrypoint-initdb.d/wpt-db.sql
RUN /bin/bash -c “/usr/bin/mysqld_safe --skip-grant-tables &” &&
sleep 5 &&
mysql -u root -e “CREATE DATABASE mydb” &&
mysql -u root mydb </docker-entrypoint-initdb.d/wpt-db.sql
RUN pwd
CMD [“mysqld”]
It is better to put init.sql into a local directory and mount to directory /docker-entrypoint-initdb.d/ during the stage of creating container.
like below. $home is my local directory, I have put ddl.sql in it.
CREATE DATABASE IF NOT EXISTS billsDB;
USE billsDB;
CREATE TABLE bills (
bill_name varchar(255) NOT NULL,
amount int NOT NULL,
dateDue int NOT NULL,
source varchar(255) NOT NULL,
isPaid int NOT NULL
);
SET GLOBAL local_infile=1;
LOAD DATA LOCAL INFILE '/bills.csv'
INTO TABLE bills
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(bill_name,amount,dateDue,source,isPaid)