PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Docker communication without using legacy links

 January 23, 2022     docker, dockerfile, mysql, phpmyadmin     No comments   

Issue

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 )


Solution

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


Answered By - Farhad Farahi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing