Accessing local MySQL server from my docker container - mysql

I have a mysql server and a docker container running on my machine. In the docker container, I run a django website. I want to connect to the local mysql server from the docker container. How can I do that?

I usually do ( for testing purposes ) :
docker network create -d my-bridge
docker run --network my-bridge --name app-db -e MYSQL_ROOT_PASSWORD=secret -e
MYSQL_DATABASE=myapp -e MYSQL_USER=myapp_user -e MYSQL_PASSWORD=myapp_secret
mysql:latest
docker run --network my-bridge --name app -p 80:80 -e DB_HOST=app-db -e DB_USER=myapp_user -e DB_PASS=myapp_secret -e DB_NAME=myapp myapp:latest
In my app Dockefile I am using in entrypoint something like envsubst, or if code language can read from environment variables I dont need to setup this.
Forget about --link docker parameter -> its obsolete

Related

run a docker mysql container and open it in one command

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/

"docker container run" requires at least 1 argument

I'm trying to create a container using a volume that I have already created, but my console shows the error
docker container run" requires at least 1 argument
This is the command I'm trying to run:
docker container run --name db -v volume-dados-do-banco:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=Mypass
I have also tried this one, wih more arguments, but the same error persists:
docker container run -d --name db -p 3306:3306 -e 'ACCEPT_EULA=Y' -e MYSQL_ROOT_PASSWORD=Mypass -v volume-dados-do-banco:/var/lib/mysql
Any thoughts on the reason why this is happening?
Problem is not with docker, you just didn't specify which image to run. Your command should include Docker image as per documentation.
docker run [OPTIONS] IMAGE[:TAG|#DIGEST] [COMMAND] [ARG...]
Example would be:
docker run -d --name db -v volume-dados-do-banco:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=Mypass mysql:latest
i just had the same problem with psql my password simply contained & and i needed to escape it with / before &
try the below command.. it seems a syntax error on your command..
docker container run -d --name db -v volume-dados-do-banco:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=Mypass
i have the same problem when i use this:
docker run -d -p 3306:3306 -v /Volumes/wd4black/mysql -e MYSQL_ROOT_PASSWORD=root mysql
but when i try below, the problem is disappear:
docker run --name my-s -d -p 3306:3306 -v /Volumes/wd4black/mysql -e MYSQL_ROOT_PASSWORD=root mysql
so i think the --name is key, but the doc didn/t write it.
I just restarted docker and ran:
docker run --name torgmysqldb --volumes-from volume-dados-banco-mysql -e MYSQL_ROOT_PASSWORD=Mypass -p 3307:3306 mysql
I found out a known issue about this:
https://github.com/docker/for-win/issues/2722
After you have extracted the image from the Docker repository, you can move on to deploying the new Container with the following code snippet:
sudo docker run --name=[container_name] -d [image_tag_name]

Docker, unable to connect WordPress to MySQL

I'm trying to link two containers together, I was able to connect a PhpMyAdmin container with a MySQL container, but nothing seem to work when I'm using a WordPress container.
I tried different things, actually I'm using this command to run a MySQL container:
sudo docker run --name sql -e MYSQL_ROOT_PASSWORD=pass mysql
and this one to set up the WordPress container:
sudo docker run --name wpress -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=pass -e WORDPRESS_DB_HOST=172.17.0.2 -p 8085:80 --link sql:mysql wordpress
MySQL container work fine, but I have this output from wpress:
MySQL Connection Error: (2054) The server requested authentication method unknown to the client
Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22
What I am doing wrong?
Edit:
I was able to connect the wpress container to sql container a couple of time during some test by adding a port to WORDPRESS_DB_HOST, which will give:
sudo docker run --name wpress -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=pass -e WORDPRESS_DB_HOST=172.17.0.2:3306 -p 8085:80 wordpress
(I also remove the --link option, it worked without it).
So it work 2-3 times, but it doesn't work anymore.
It seem to be a version error. Use an image with a different version of mysql, mysql:5.7 for example, and it should work.
I had same problem/error.
This is what I had to do for mysql and wordpress:
docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -d mysql
docker exec -it wordpressdb bash
#inside run:
mysql -uroot -ppassword
#paste
ALTER USER 'root'#'%' IDENTIFIED WITH mysql_native_password BY 'password';
exit
exit
docker run --name wordpress -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=password -p 8080:80 --link wordpressdb:mysql -d wordpress

Avoid hard coding the mysql container ip in my apache container script

I have a mysql container which runs fine. I can start it and see it up and running in the docker ps list.
I then try to run another learnitouch container in which an engine-db-seed.sh shell script tries to connect to the mysql container server.
The learnitouch container Dockerfile contains:
ENTRYPOINT ["/bin/bash", "/usr/bin/learnintouch/engine-db-seed.sh"]
The engine-db-seed.sh file contains:
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h 172.17.0.2 -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
The db_engine-db.sql is being seeded all right in the mysql database.
But I had to hard code the mysql container IP as you can see in the -h option. I got the 172.17.0.2 IP address from a docker inspect on the mysql container. Not the most automated way...
How can I do without such hard coding ?
Running the mysql container:
docker run -d -p 3306:3306 -v /home/stephane/dev/php/learnintouch/docker/mysql/data:/usr/bin/mysql/install/data --name mysql stephane/mysql:5.6.30
Running the learnintouch container:
docker run -p 127.0.0.1:80:80 --link mysql:mysql --name learnintouch stephane/learnintouch
I'm using Docker version 1.12.1, build 23cf638
Just use the service name and make sure that both services are running on the same network (bridge0 by default).
So if you create your mysql service like this
docker run -d --name foo mysql-image
your engine-db-seed.sh could then be
/usr/bin/mysql/install/bin/mysql --protocol=tcp -h foo -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql
mysql will make a dns request for foo which will be resolved by Docker to the ip of your foo service.

How to attach a volume to a Bluemix container

I’m setting up a container on Bluemix using ice from the command line, but every time I try to attach a volume to a container it simply doesn’t work. The mounted folder isn't created in the root directory.
My command is:
ice create -p 80 -p 22 --name test --memory 1024 --volume notebooks:/notebooks registry.ng.bluemix.net/repository/app:latest
Docker gives you the option to create the volume yourself or allow Docker to create it for you. Either one of these will work:
docker run --name mysql_test -v /etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d jw_mysql:latest
OR
docker run --name mysql_test -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d jw_mysql:latest
For IBM Containers, the situation is different: you need to create a volume before you can use it. So only this will work:
cf ic volume create dbstorage
cf ic run -p 3306 --name cf_mysql_test -v dbstorage:/etc/mysql/config/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d registry.ng.bluemix.net/jw_image_reg/jw_mysql:latest
(Assuming you want to use port 3306.)