skip to Main Content

I am trying to containerize my company’s app. When I deploy that app in VM, I have to install MySQL in the same VM because the application has a hardcoding 127.0.0.1:3306 in the source code.
Now I am trying to separate the app into 2 containers: app-container and mysql-container.
I have published mysql-container port 3306 to the host machine port 3306:

 docker run --name mysql-container -p 3306:3306 -d mysql:latest

The next step is to forward app-container 127.0.0.1:3306 to mysql-container port 3306.

I have tried --network="host" to make the app-container use the host machine’s network so I can connect mysql-container’s port 3306 from app-container’s 127.0.0.1:3306 successfully. However, for some reason, the developer doesn’t like it so I have to find another way to achieve that.

I am very new to Docker and container, any help is appreciated!

2

Answers


  1. Usually you should create a network – there are two options basically user defined network and a default bridge network.

    When you create the network (this is an abstraction in docker, like you can docker network ls to see the available networks, for example) and run your two containers in the network – they’ll access each other.

    I’ve found a good tutorial about the implementation details that shows both cases Check here

    Having said that, when it comes to running multiple containers together, one of the good options especially for development on local machines is "docker compose" – you declare what containers should run and when you docker-compose up it will start them all with a network and everything so that you won’t need to delve into details of how to configure the docker containers properly (which flags to give, etc).
    Of course technically docker compose is only a kind of script that does all this for you but its really a convenient tool, worth to check

    Login or Signup to reply.
  2. Docker Compose is a great tool for running a multi-containers app with just a single command, what you need to do is create a file docker-compose.yml and define your app there, like below:

    version: '1.0'
    
    services:
      database:
        image: postgres
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=admin
          - POSTGRES_DB=postgres
          - PGDATA=/var/lib/postgresql/data/data/
        ports:
          - '5432:5432'
    
        volumes: 
          - ./db:/var/lib/postgresql/data
    
      python-container:
        build: .
        environment:
          - PP_PG_DB=postgres
          - PP_PG_USER=postgres
          - PP_PG_PASSWORD=admin
          - PP_PG_HOST=database
          - PP_PG_PORT=5432
        depends_on:
            - database
        ports:
          - 8000:8000
        volumes: 
          - ./uploads:/var/uploads
    

    Here you can see I define 2 services, one for the web app and one for the database, and each service will run in a separate container. I expose the port 5432 of the database container by using ports, then the api-server container can access it .
    If you are new to Docker you need to notice the volumes as well, volumes is a way to persist the database so that you will not lost all data when restart the container.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search