0

Hi I am using flask and MySQL it works on my local machine perfectly but I want to put it in docker container I get the following error: MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)") Can someone please help me, where I am doing wrong? my dockderfile:

FROM python:3.9

EXPOSE 5000 

WORKDIR /virtual-staging
RUN apt-get update
RUN apt-get -y install python3 python3-pip vim iputils-ping python3-mysqldb
# COPY requirements.txt app.py config.py /virtual-staging/
COPY . /virtual-staging

RUN pip install -r requirements.txt


CMD [ "flask", "run", "--host=0.0.0.0"]

and my docker-compose.yml file:

version: "3.7"

services:
  app:
    build: .
    restart: always
    links:
      - "db"
    ports:
      - "5000:5000"
    environment:
      - FLASK_ENV= production
  db:
    image: mysql:8.0
    ports:
      - "32000:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root

    volumes:
      - ./db:/docker-entrypoint-initdb.d/:ro

any my config.py file:

class ProductionConfig(Config):
    MYSQl_USER = 'root'
    MYSQL_PASSWORD = 'root'
    MYSQL_HOST = "mysql"
    MYSQL_PORT = "3306"
    MYSQL_DB = "virtualstaging"

thank you in advance

1 Answer 1

2

Try setting MYSQL_HOST in your configuration to db.

Docker-compose creates host definitions for services defined in a compose file available through docker network created by docker-compose for the application stack.

Also the links is really not necessary. I would advise to use depends_on instead in order to create the db service before the app one.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.