-1

I am setting up an application with the Flask framework using MySQL as the database. This database is located locally on the machine. I manage to use the occifielle image of MySQL without problem. Only that I would rather use a local database that is on my computer. Here is my extract, please help me.

Dockerfile

FROM python:3.9-slim

RUN apt-get -y update 
RUN apt install python3-pip -y

WORKDIR /flask_docker_test

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

EXPOSE 80

CMD gunicorn --bind 0.0.0.0:5000 app:app

Docker-compose file

version: "3"
services:
  app:
    build: .
    container_name: app
    links:
      - db
    ports:
      - "5000:5000"
    depends_on:
      - db
    networks:
      - myapp


  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    container_name: mysql_db
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: flask_test_db
      MYSQL_USER: eric
      MYSQL_PASSWORD: 1234
    ports:
      - "3306:3306"
    networks:
      - myapp

  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      - 8080:80
    depends_on:
      - db
    environment:
      PMA_ARBITRARY: 1
      PMA_USER: serge
      PMA_HOST: db
      PMA_PASSWORD: 1234
    networks:
      - myapp


networks:
  myapp:

I would like to establish a connection with my local database rather than with the database provided by the MySQL container

1 Answers1

0

in order to connect to your local database, you should :

  • remove the db from your docker-compose.yaml
  • remove the network myapp
  • use network_mode host

But IMO you should keep your db in the docker-compose file, otherwise other developers won't be able to start the project on their machine

EDIT : code snippet for network_mode

services:
  app: 
    ...
    network_mode: host
GuiGav
  • 141
  • 8