0

Creating two containers, one contains the flask web app and the other has MySql installed. The web app in the first server needs to access the database in the other server.

docker-compose.yml file

web and db containers are linked in the compose file.

version: '3'

services:
  web:
    container_name: "web"
    build:
      context: .
      dockerfile: ./build/web_build
    links: 
      - db
    ports:
     - "5000:5000"
  db:
    container_name: "mysql"
    build:
      context: .
      dockerfile: ./build/db_build
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 'root'
      MYSQL_DATABASE: 'password'
      MYSQL_USER: 'root'
    volumes:
      - ./db:/docker-entrypoint-initdb.d/:ro
      - ./data:/data

My running containers

enter image description here

This is how web container's hosts file look like

root@9ee63128f6db:/code# cat /etc/hosts 
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
**172.31.0.3      9ee63128f6db**

And the hosts file on db container

root@9acbf1743dca:/# cat /etc/hosts 
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.31.0.2      9acbf1743dca

File /etc/mysql/my.cnf

enter image description here

Installed Maria db on web container just to check connectivity.

apt install mariadb-server

DB container's IP is 172.31.0.2. When I try to connect I get the following error

root@9ee63128f6db:/code# mysql -u root -p'root' 172.31.0.2
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I have gone through this Access to mysql container from other container but it didn't help.

mysqld service is also not available.

  • MySql latest version is pulled from MySQL docker image.
Ali
  • 7,810
  • 12
  • 42
  • 65

0 Answers0