Running two Mysql docker containers - mysql

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.

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.

Connecting 2 conatiners with docker --network

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

Docker, Runned mysql container with port forwarding is stopped immediately as soon as it launched

I have got a problem with launching MySQL container.
I run MySQL container with below command:
$ sudo docker run -d --name stockdb -e MYSQL_ROOT_PASSWORD=yang1234 -e MYSQL_DATABASE=stkanalysis mysql:5.7 -p 3307:3306
and checked result using
$ sudo docker ps -a
This is the result.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34e98ad90f73 mysql:5.7 “docker-entrypoint…” 2 seconds ago Exited (1) 1 second ago stockdb
When I launched same MySQL container without option -p, it worked well like this:
$ sudo docker run -d --name stockdb -e MYSQL_ROOT_PASSWORD=yang1234 -e MYSQL_DATABASE=stkanalysis mysql:5.7
But, whenever I put the port forwarding option -p, running container is failed(technically, it is exited as soon as runed)
I hope to run MySQL container with port forwarding to connect its DBMS from outside host.
I’m using Ubuntu 16.04 and Docker version is 17.09.0-ce.
I solved my problem.
The cause was the position of option -p located at the end of commend.
I moved option -p statement forward, and it works well now.
$ sudo docker run --name stockdb -p 3307:3306 -p 3308:22 -e MYSQL_ROOT_PASSWORD=yang1234 -e MYSQL_DATABASE=stkanalysis mysql:5.7
thank you.

Why docker start is much faster than docker run

I use mysql image that start with this command
docker run --name test-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d -p 3306:3306 mysql
when docker run in background, It takes about a minute for another application can connect to port 3306.
After that I stop this container with docker stop test-mysql and then start it with docker start test-mysql. in the second case, with start command, the application can connect to port 3306, just after 5 seconds.
Now I take a snapshot from stopped container with docker commit test-mysql mysql2, and run it with docker run -d mysql2 but in this case, the application can connect to mysql2 after a minute!
So,
What's happen with stopped container, that can be start and responsible just in 5 seconds but mysql image can not do it?
Is there any way to take a snapshot after run container, that can be responsible in 10 seconds?
NOTE: Mysql image has an entrypoint that it takes above a minute to start.
Take a look here: https://stackoverflow.com/a/34783353/7719775 for the first Answer.
And for the second, you should take a look here https://docs.docker.com/engine/reference/commandline/commit/, but even in this case docker start will be faster than docker run command

mysql with Exited(1) from docker

Start learning docker and try to setup a mysql container. But it dies immediately with Exited(1).
Following is the command used
docker run mysql -e MYSQL_ROOT_PASSWORD=password1
Looking at docker ps, it does not show any running docker container
with docker ps -a returns the following :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e681f56c52e2 mysql "/entrypoint.sh -e MY" 3 seconds ago Exited(1) 3 seconds ago lonely_rosalind
Nothing shows up for docker logs lonley_rosalind either
Any idea how to determine why if failed ?
I am running
ubuntu 15.04
docker version 1.9.1 build a34a1d5
Try this
docker run -e MYSQL_ROOT_PASSWORD=password1 mysql
When you are writing something after docker image name docker accepts it as a command for execution in your created container. Pattern for docker run:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]