Hi there,
I'm trying to create a docker container that will run a mysql script which generates a database and then table. My Dockerfile looks like this:
FROM mysql:latest
WORKDIR /
ADD . /
EXPOSE 3306
CMD mysql -u "root" -proot < "schema.sql"
I create the image through this:
docker build -t database .
And then I run it through this:
docker run -d -p 3306:3306 database
At this point the script should be run- however instead I just get this random line in the terminal:
0b2503b42482a4fa840351925845392e1abdf6022b23447187ff49ed4f0fa05b
Grateful for your help!
you are getting 0b2503b42482a4fa840351925845392e1abdf6022b23447187ff49ed4f0fa05b because of the option "-d" in docker run -d -p 3306:3306 database which indicates that the container will be running in the background. You can remove it if you dont want that.
For more info about this please check this link: Docker run command
The container should be running though.
Check the command Docker ps that will show you infos about running containers.
Hope this helps!
don't worry, this is 0b2503b42482a4fa840351925845392e1abdf6022b23447187ff49ed4f0fa05b ID from container.
You can use id to stop or check status or collect information of container. if you want check your command is work or not, you can run with this command docker run -it database bash or docker run -it database sh
Hope this helps.
Best Regards
as you are overriding CMD entry command in docker file, you would need to start mysqld service explicitly
CMD [mysqld, mysql -u "root" -proot < "schema.sql"]
Related
I am very sorry guys, I found several topics on stackoverflow but none of them solved my issue. I am a docker noob, but all I want to do is connect to my docker mysql database in a docker container created via docker-desktop on windows.
docker run -p 3306:3306 --name blaaa -e MYSQL_ROOT_PASSWORD=password -d mysql
I set up everything with the suggested port mapping, but I cannot connect to the docker container from the host (windows 10) via mysql-workbench or other programs. I read that there are issues and you often cannot connect to the docker host via localhost, but I cannot even figure out what the freakin ip of docker0 or other adapters is (ipconfig does not show anything). docker inspect <id> shows a lot of information, but besides mapping to 0.0.0.0:3306 and other ips that don't work, I cannot really figure out what to do here. the container itself is running fine and I can access the database from inside the container without any issues.
thanks for your help!
/SOLVED
I am sorry for the confusion; I think it was due to the Windows clients (I tried several) that things didn't work out. I finally got it working with HeidiSQL. Don't ask me how or why HeidiSQL finally works; but mysql-workbench generally showed strange behavior on my system, it crashed several times out of the blue. Thanks for your help.
Hey when you don't specify the database name, the container will stop as soon as it created, so specify the database name as environment variable
this is the docker command :
docker run -p <host_machine_port>:3306 --name <container_name> -e MYSQL_ROOT_PASSWORD=<root_password> -e MYSQL_DATABASE=<db_name> -d mysql
in your case :docker run -p 3306:3306 --name blaaa -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE= testDB -d MySQL
And next verify if the container running or not: docker ps
if it shows the container name you specified when you created it, it's running.
next connect to your container : mysql --host=127.0.0.1 --port=3306 -u root -p password
this works
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d -p 3307:3306 mysql
reason explained here -p is an argument
https://github.com/docker-library/mysql/issues/504
I want to run and open a mysql Cli in docker just with one command . Something like this is not working:
docker run --rm -it -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql mysql -p
I know I can connect to mysql after running my container this way
docker -it docker exec -it mydb mysql -p
but i want to do it in one liner.
Thanks
(Updated)*****
Seems that you can do it in version 8 calling MySQLsh at the end of the command. But unable to do it for previous versions
docker run --name=mk-mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -it mysql/mysql-server:8.0.20 mysqlsh
The database server and client are two separate programs. A container only runs one program, so you can't run both the server and the client in the same container, both as the main process. You could write a script that starts the container and then runs mysql to connect to it, but that's about the best you can do.
#!/bin/sh
docker run -d -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql
exec mysql --host=127.0.0.1 --port=33060 --connect-timeout=60 --wait --password
If you're trying to do this to create a database or do other first-time initialization, you can bind-mount an initialization script into /docker-entrypoint-initdb.d and it will run as part of the database setup (only the very first time the database is started).
# Create the storage for the database
# (delete and recreate to rerun the init script)
docker volume create mysql-data
docker run \
-v mysql-data:/var/lib/mysql \
-v $PWD/init.sql:/docker-entrypoint-initdb.d/init.sql \
... \
mysql
If you're just trying to experiment with SQL commands, a serverless database like SQLite might fit your needs better.
the -p parameter is for the ports to be published and should not be part of the -it interactive, that should be your error,
Have a read of the docker run command, in the docker documentation,
https://docs.docker.com/engine/reference/run/
I have from issue running mysql using Dockerfile
FROM mysql:latest
# Add a database
ENV MYSQL_DATABASE some_table_name
ENV MYSQL_ROOT_PASSWORD some_password
ADD some_table.sql /docker-entrypoint-initdb.d
ENTRYPOINT /bin/bash
Building an image works for this code but mysql service is not found in the container when I try to run service mysql start. mysqld command would not run due to some root security issues. Is anyone able to help? Thank you.
Remove the ENTRYPOINT
build and run the container.
Run another process to check inside the container name like:
docker exec -ti {containername} mysql -u root -p
This will check the password is set right. Then SHOW CREATE TABLE {db.tablename}
I am building a docker machine using the image "mysql". I have some setup script to run for the first time the machine is built. This setup script will create some database and dabase users with specified permmissions.
Following are the minimized version of my files..
pcdb1.entrypoint.sh
#!/bin/sh
mysql -uroot -p'pass123' -e 'show databases MYENTRYDB;'
Dockerfile
FROM mysql:5.7
COPY ./pcdb1.entrypoint.sh /
ENTRYPOINT ["/pcdb1.entrypoint.sh"];
I am getting the following error in the log
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
pcdb1 exited with code 1
What I understood is, my script is trying to run before mysql is started. But I am not sure how to do it properly. Can I get a suggestion?
EDIT: 20181007
I have found the way you mentioned in question--initialize DB while building docker image. But with a little difference, the way I found seems like initializing DB while running a container from image, although the initializing script was specified while building the image.
According to the official information about mysql:5.7, there is paragraph named "Initializing a fresh instance". We could just add a initializing script into the directory /docker-entrypoint-initdb.d, the default ENTRYPOINT and CMD of image mysql:5.7 would execute it after database start-up.
For example:
FROM mysql:5.7
COPY init-database.sql /docker-entrypoint-initdb.d/
content of init-database.sql:
create database light;
create user 'light'#'%' identified by 'abc123';
grant all privileges on light.* to 'light'#'%' identified by 'abc123';
grant all privileges on light.* to 'light'#'localhost' identified by 'abc123';
Build new image:
docker build -t light/mysql:5.7 .
Run a container:
docker run -tid --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD='abc123' light/mysql:5.7
Examine initialization:
docker run -ti mysql /bin/bash
root#25e73d40c4ff:/# mysql -uroot -p
Enter password: (abc123)
Welcome to the MySQL monitor.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
All work well.
Former answer below.
How to start mysql damon in container?
First of all, you are right on "trying to run before mysql is started" part. But still, there is a missing on "how MySQL starts exactly". If you execute docker history mysql:5.7 --no-trunc, you could see three important records among output like below:
/bin/sh -c #(nop) CMD ["mysqld"]
/bin/sh -c #(nop) ENTRYPOINT ["docker-entrypoint.sh"]
/bin/sh -c ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh
So far we should know when we start a mysql container with command below, the exact initial command in container is docker-entrypoint.sh mysqld.
docker run -tid -e MYSQL_ROOT_PASSWORD='abc123' mysql:5.7
How to initiate mysql in container?
Secondly, let's now have a check on docker-entrypoint.sh script.
There is a specific line like below, just at roughly middle position of this script, which means to start mysql daemon.
mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
After starting mysql, we could see lots of initiating statements in docker-entrypoint.sh script. Such as creating root user with password or not, granting privileges to root, creating database declared by users with MYSQL_DATABASE env and so on.
Now here are solutions offered for you.
Self-defining the docker-entrypoint.sh script.
In this way, you could whatever you want which is legal in mysql.
Get the whole entrypoint.sh script on your host.
Add your self-definition of mysql in the script, make your
self-defining content at the bottom of this script. I assume you
don't want to mess it with original content.
Build a new mysql image for your own with command and Dockerfile below.
command: docker build -t mysql:self .
Dockerfile:
FROM mysql:5.7
COPY /path/to/your-entrypoint.sh /
ENTRYPOINT ["/your-entrypoint.sh"]
CMD ["mysqld"]
If your don't want a new image, there is another way to change
ENTRYPOINT when you run a container. But still, you should make your own script available in container.
docker run -tid -v /path/to/your-entrypoint.sh:/entrypoint.sh -p 3306:3306 -e MYSQL_ROOT_PASSWORD='abc123' mysql:5.7
Using default ENVs provided by mysql:5.7
In this way, there is a limit, especially on "specified permmissions" you mentioned.
The ENVs you need are: MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD.
The command should like this:
docker run -tid -e MYSQL_ROOT_PASSWORD='abc123' -e MYSQL_DATABASE='apps' -e MYSQL_USER='light' -e MYSQL_PASSWORD='abc123' mysql:5.7
This means that the database apps and user light will be created automatically, and the user light will be granted superuser permissions for the database apps.
More reference here on hub.docker.com.
I have a usecase where i need to run mysql on a container and link it to another container. I also have my db files data in my host location which is mounted as a volume on to the database container... The condition is to run the container not as root but as a different user with all privileges.
The db is there is in the mounted volume.
I ran the following command:
docker run -d -v ~/testdata:/var/lib/mysql -e MYSQL_DATABASE=Testdata_DB -e MYSQL_USER=testdata -e MYSQL_PASSWORD=mypasswordhere -p 3306:3306 --name=testdata_db mysql
The above command will start the container but i am not able to see the user with the password when i bash into the running container. Only the mysql is running
docker exec -it testdata_db bash
Kindly let me know where i am going wrong. I followed the documentation under the docker official repo link.
I solved it by creating a init.sql with the required sql commands to create user , tables which it loaded from my host to the container under docker-entrypoint-initdb.d/ and set the env varibles as required. This made mysql instance load as a fresh instance and loaded all the required tables and data.
The final command is:
docker run --name testdata_db -p 3306:3306 -e "MYSQL_ROOT_PASSWORD= " -e "MYSQL_USER=test" -e "MYSQL_PASSWORD=mypass" -e "MYSQL_DATABASE=mysql" -v ~/mysql/db/:/var/lib/mysql/ -v ~/mysql/init/:/docker-entrypoint-initdb.d/ -d mysql