Docker communication without using legacy links - mysql

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

Related

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

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

Connect docker container to local workbench MySQL DB

HI I have my web app running on my local machine and connected to Mysql workbench, I am now trying to dockerize the webapp. I can't seem to get it to connect to the DB on my local dev machine (I am running Docker Desktop for Windows), can anyone tell me how I would go about this? Here is what I have so far.
`docker run -it -e "CATALINA_OPTS=-Dspring.profiles.active=dev -DPARAM1=DEV" -p 8080:8080 -p 8005:8005 -p 8009:8009 -p 3306:3306 --add-host=docker:192.168.1.7 -v C:\myapp\trunk\target\myapp.war:/usr/local/tomcat/webapps/myapp.war --name waitapp tomcat:8.0.38-jre8`
after a few second, I run docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a1764dd9640 tomcat:8.0.38-jre8 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 0.0.0.0:8005->8005/tcp, 0.0.0.0:8009->8009/tcp, 0.0.0.0:8080->8080/tcp waitapp
The container seems to be running, but I get a 404 Not found when I try the rest request, this is the same as I do when running from inside spring tool suite using built in tomcat server.
NOTE
I don't want to run a separate mysql container and link the two over a network, I just want to try get my newly created docker app to connect to my local DB MySQL.
As mentioned on this post, you can try 2 things:
Add gateway and use it from containers, like
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
In addition to your app container, run proxy (ngnix?) container, which will rout the calls to DB when required
This answer also show how can you obtain the host IP inside the docker container.

Access to mysql container from other container

I have setup docker container with mysql that expose 3306.
I've specified database user, database password and create a test db and give the privileges to new user.
In another container i want to accesso to this db.
So i set up new container with a simply php script that create new table in this db.
I know that mysql container's ip is 172.17.0.2 so :
$mysqli = new mysqli("172.17.0.2", "mattia", "prova", "prova");
Than using mysqli i create new table and all works fine.
But i think that connect to container using his ip address is not good.
Is there another way to specify db host? I tryed with the hostname of the mysql container but it doens't work.
The --link flag is considered a legacy feature, you should use user-defined networks.
You can run both containers on the same network:
docker run -d --name php_container --network my_network my_php_image
docker run -d --name mysql_container --network my_network my_mysql_image
Every container on that network will be able to communicate with each other using the container name as hostname.
You need to link your docker containers together with --link flag in docker run command or using link feature in docker-compose. For instance:
docker run -d -name app-container-name --link mysql-container-name app-image-name
In this way docker will add the IP address of the mysql container into /etc/hosts file of your application container.
For a complete document refer to:
MySQL Docker Containers: Understanding the basics
In your docker-compose.yml file add a link property to your webserver service:
https://docs.docker.com/compose/networking/#links
Then in your query string, the host parameter's value is your database service name:
$mysqli = new mysqli("database", "mattia", "prova", "prova");
If you are using docker-compose, than the database will be accessible under the service name.
version: "3.9"
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
ports:
- "8001:5432"
Then the database is accessible using: postgres://db:5432.
Here the service name is at the same time the hostname in the internal network.
Quote from docker docs:
When you run docker-compose up, the following happens:
A network called myapp_default is created.
A container is created using web’s configuration. It joins the network myapp_default under the name web.
A container is created using db’s configuration. It joins the network myapp_default under the name db.
Source:
https://docs.docker.com/compose/networking/

docker.io - Docker linking between application & database containers

I am trying to use my Scala-Akka application with my MySQL database on two separate Docker containers. I found out that Docker allows developers to link their application to their databases with the flag named --link. In my Dockerfiles in which I've used to create my images, I have add in EXPOSE 3306 8080 to it.
And this is how I run the containers:
docker run -d -p 3306:3306 --name mysql centos6mysql
docker run -d -p 8080:8080 --name scalaapp --link mysql:db centos6scala
After running the containers, I used docker ps and I am able to see the active containers. However, It seems like the application container is not using the database from the MySQL container. Anyone know what's wrong?
Linking in Docker allows network connections to be made between containers. Docker will define environmental variables to your linked containers for the URL, IP, port, and protocol. The names of these will be based on the name of your container. For instance:
DB_NAME=/web2/db
DB_PORT=tcp://172.17.0.5:5432
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
DB_PORT_5432_TCP_PROTO=tcp
DB_PORT_5432_TCP_PORT=5432
DB_PORT_5432_TCP_ADDR=172.17.0.5
You can use these environmental variables to set up your Akka app container to connect to your DB container. However, you must manually configure the app container to do so. Docker will not make the connection for you automatically.
So, somewhere in your app, you will need to pass these values to your startup script, something that might look like:
./restcore --Ddb.default.db="jdbc:mysql//${DB_PORT_3306_TCP_ADDR}:${DB_PORT_3306_TCP_PORT"