Connecting 2 conatiners with docker --network - mysql

I am trying to connect a phpmyadmin container to another mysql container. I have created a network with docker network create and added my two containers. But when I tried to login with phpmyadmin, the password is incorrect error displayed each time.
I checked that the two containers are available to my networks which I already created so I tried to ping mysql container to phpmyadmin container but unfortunately I had negative results.
I followed this tutorial:
https://tecadmin.net/tutorial/docker/docker-networking-example/
Can you explain to me how the commend docker run --network works exactly ?

If everything is ok, so you can ping one container from another using container name. When you create a network between 2 containers, it creates an alias with the container name and the container IP.
For instance
docker network create my-bridge-network
docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server --network my-bridge-network
docker run --name phpmyadmin -d -e PMA_HOST=mysql -p 8080:80 phpmyadmin/phpmyadmin --network my-bridge-network
In this case when you exec ping from phpmyadmin container you should be able to ping mysql container
docker exec phpmyadmin ping mysql # if in phpmyadmin `ping` program is available

Related

How can I to connect to a Docker running mysql?

The instructions here: https://hub.docker.com/_/mysql indicate to run
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql-container -uexample-user -p
But i have no idea what some-network is? So i run this instead and get and 'unknown MySQL host' error even though some-mysql-container is definitely the name of my container.
$ docker run -it--rm mysql mysql -hsome-mysql-container -uexample-user -p
What am I doing wrong here?
'some-network' refers to a docker-network. You need to create it first. I named it 'mysql-network' to make its purpose a bit more clear:
docker network create mysql-network
Then, start database container:
docker run --network mysql-network --name mysql-db -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql
Then, start a the client container to connect to the first one:
docker run -it --network mysql-network --rm mysql mysql -hmysql-db -uroot -p
By adding both containers to the same network, they are able to communicate with each other.

using docker images for mySQL and redmine, how do I resolve "Unknown MySQL server host"?

I am using the docker images supplied at https://hub.docker.com/_/redmine
I have chosen to use MySQL as my database backend. So I have 2 docker containers: MySQL and Redmine, as downloaded from dockerhub.
Following the instructions on the docker/redmine link above, I ran through the commands and found that the redmine docker would not start. Inspecting the docker logs, I see:
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host redmine (-5)
I thought the 2 dockers were having difficulty talking to each other, so I setup a new docker network for both containers to use:
docker network create --driver bridge redmine-net
Adapting the instructions, on the docker/redmine link above, I run
docker run -d name our-mysql --network redmine-net -e MYSQL_USER=redmine -e MYSQL_PASSWORD=todays-password -e MYSQL_DATABASE=redmine -e MYSQL_RANDOM_ROOT_PASSWORD=1 -p 3306:3306 mysql:5.7
docker run -d name our-redmine --network redmine-net -e REDMINE_DB_MYSQL=redmine -e REDMINE_DB_USERNAME=redmine -e REDMINE_DB_PASSWORD=todays-password redmine:latest
However, the redmine contain still falls over instantly, with the same error.
EDIT Using the *.yml file as provided in the dockerhub redmine instructions works pretty faultlessly.
So the question is: what is the docker-compose method doing that docker run isn't handling?
Thank you.
The REDMINE_DB_MYSQL arg of the redmine container do reference to the mysql container, so, if you define the database service like our-mysql, then set REDMINE_DB_MYSQL=our-mysql

Docker communication without using legacy links

I'm trying to create a mini-demo with docker using mysql and phpmyadmin and i'm trying to make the two docker containers communicate with each other without using the --link flag since this has been flagged as "legacy" by docker (https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/#/connect-with-the-linking-system)
I managed to do this using docker-compose using the network section, but I want to implement the same scenario using normal dockerfiles and running the two containers in command prompt.
Here are the two dockerfiles I created:
Dockerfile for mysql
FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD=12345678
ENV MYSQL_DATABASE=mysql
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=12345678
Dockerfile for pma
FROM phpmyadmin/phpmyadmin:4.6
ENV PMA_HOST=mysql
ENV MYSQL_ROOT_PASSWORD=12345678
Docker images are created correctly using docker build and these are the commands that i use to run the two containers:
mysql:
docker run -d --name mysql sebastian/db-mysql
pma:
docker run -d -p 7777:80 --name pma sebastian/db-pma
When i try to connecto to Pma using username root and password 12345678 i get the following error:
mysqli_real_connect(): (HY000/2005): Unknown MySQL server host 'mysql' (-2)
I'm sure I'm missing something when spinning the two containers and I cannot fully understand how the two containers are suppose to communicate and/or how pma will find host mysql (the name i defined when running the mysql container)
Is docker suppose to allow communication between the two containers?
How do containers should find each other by using names and not ip addresses?
P.S. i'm using dockertoolbox on windows 10 (maybe that is the real problem :D )
The problem:
You are not specifying any networks in your docker run so you will use default bridge, Default bridge will not give you internal DNS but containers on that network can communicate via IP Addresses.
Follow these steps:
First create a user-defined network:
docker network create <yournetworkname>
Now run containers using the network we just created:
docker run -d --name mysql --network <yournetworkname> sebastian/db-mysql
docker run -d -p 7777:80 --name --network <yournetworkname> pma sebastian/db-pma
User defined networks provide connectivity by default and internal dns to the containers on the same network. For example you can ping mysql from pma by:
ping mysql

Running two Mysql docker containers

I am trying to run two different mysql containers for master->slave replication. I start by building and running the master:
docker build --no-cache -t mysql-master .
docker run -it --name mysql-master -h mysql-master -p 3306:3309 mysql-master /bin/bash
Which works fine and runs the container correctly. I can get as far as getting the information to set up the second container, mysql-slave. When I run the following command:
docker build --no-cache -t mysql-slave .
docker run -it -p 3308:3309 mysql-slave --name mysql-slave --link mysql-master:mysql-slave /bin/bash
The mysql-master container disconnects. I am not sure why but I am sure that there is some kind of conflictions going on with the containers that I may not be aware of. Can anyone suggest what docker command I should be running so that both containers can run simultaneously?
I have a feeling this is because both containers are attempting to access the same port:
root#test2net:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1942b5e1f69 mysql-slave:latest "/tmp/makeSlaveSQL.s 40 seconds ago Up 40 seconds 3306/tcp, 0.0.0.0:32773->3307/tcp mysql-slave
c9a7632d9cae mysql-master:latest "/tmp/makeMasterSQL. 2 minutes ago Up 2 minutes 0.0.0.0:32769->3306/tcp mysql-master
Is there a way to explicitly cast each container to a specific port. I have tried using EXPOSE in the Dockerfile and the -p to designate different ports but as you can see from above, mysql-slave is still binding to port 3306.

Possible to run two instances of docker containers on one mysql database container?

There are three containers
Container A : web server
Container B : replicate web server of Container A
Container Z : mysql datastore container for Container A
Can I run Container A and B at the same time using Container Z as mysql datastore? will it corrupt mysql data store?
Runtime of the containers below:
Container Z :
docker run --name mysql_datastore -it busybox:mysql_datastore true
Container A:
docker run -it -p 80:80 --volumes-from mysql_datastore --name webservera -h webservera centos:webseverwithmysql /bin/bash
Container B :
docker run -it -p 81:81 --volumes-from mysql_datastore --name webserverb -h webserverb centos:webseverwithmysql /bin/bash
Hopefully one of these interpretations is correct.
Can I run multiple mysql daemons in different containers that all share a single data volume?
No, each daemon needs a separate data directory to avoid conflicts. You could put multiple data directories in the shared volume, but the result of that is multiple completely separate databases. - source
Can I run multiple containers that connect to a single mysql database container?
Yes it is possible to allow multiple containers to connect to a single database container, but not by sharing volumes. Container Z will run the mysql daemon and other containers can connect to it via tcp sockets. The official mysql repo readme has steps to get started:
First start Container Z.
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql
Then run other containers that you want to connect to the database with something like this:
docker run --name webservera --link some-mysql:mysql -d application-that-uses-mysql
Docs for the --link flag. Container linking adds a hostfile entry for the link alias so you don't have to find the address manually. Your webserver's database configuration would look something like this
jdbc:mysql://address=(protocol=tcp)(host=mysql)(port=3306)(user=root)(password=mysecretpassword)
I hope this helps.