0

I’m trying to run a Flask application and mysql database by running docker-compose up on my computer. The flask is running on port 5000.

if __name__ == "__main__":
    app.run(port=5000, debug=True)

The docker container is responding properly when I use docker exec command. But I can't get any response from the host by using the url: http://localhost:5000.

The curl -X GET <url> command is giving the following output:

curl: (56) Recv failure: Connection reset by peer

The docker ps command is giving the following output:

CONTAINER ID   IMAGE                      COMMAND                  CREATED             STATUS             PORTS                                NAMES
bffa59c471f6   customer_transaction_app   "/bin/sh -c 'python …"   About an hour ago   Up About an hour   0.0.0.0:5000->5000/tcp               customer_transaction_app_1 
ad60c2830ac0   mysql                      "docker-entrypoint.s…"   About an hour ago   Up About an hour   33060/tcp, 0.0.0.0:32001->3306/tcp   customertransaction_db_host  


                                              

Here is the Dockerfile:

FROM python:3.8 
EXPOSE 5000 
COPY requirements.txt /app/requirements.txt
WORKDIR /app 
RUN pip install -r requirements.txt 
COPY . /app CMD 
python main.py

Here is the docker-compose.yml file:

version: "2"
services:
    app:
        build: ./
        depends_on:
            - db
        ports:
            - "5000:5000"
    db:
        container_name: customertransaction_db_host
        image: mysql
        restart: always
        ports:
            - "32001:3306"
        volumes:
            - customertransaction-db-vol:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: 123456
            MYSQL_DATABASE: customertransaction_db
            MYSQL_USER: user
            MYSQL_PASSWORD: 123456
    
volumes:
    customertransaction-db-vol: {}

Both the containers reside inside a docker network customer_transaction_default. The docker network inspect command creates the following output:

[
    {
        "Name": "customer_transaction_default",
        "Id": "4b5b20f503af0026a2f1ef185436c9a8e3d9c2ece690e93ece0e6b12f7821edb",
        "Created": "2021-06-20T17:52:15.603679073+05:30",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.24.0.0/16",
                    "Gateway": "172.24.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "ad60c2830ac0f7e270daf03334ea8a8170200e92c2bc43492c378bd1d89cd3ac": {
                "Name": "customertransaction_db_host",
                "EndpointID": "de4597a1f58d711640f71a6169111f9842c7c5d74320825657a2518d07f36504",
                "MacAddress": "02:42:ac:18:00:02",
                "IPv4Address": "172.24.0.2/16",
                "IPv6Address": ""
            },
            "bffa59c471f6762bb802fcee37db356cf2c7a59f4f88192e3546dd10ad9dbb2d": {
                "Name": "customer_transaction_app_1",
                "EndpointID": "a3ded03e28343921d799c0efc334034028821c231e1469d4359cd387c7f43f70",
                "MacAddress": "02:42:ac:18:00:03",
                "IPv4Address": "172.24.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
  • Can you connect to your Django app inside the container(by using the `docker exec`)? If it cannot, the problem is your Django app not started properly. – Yz. Jun 20 '21 at 22:08
  • The flask app is running properly. From the container using 'docker exec' command I can get proper response from the flask app with data returning from MySQL as I send a GET request through curl. But from the host machine I am not getting any response. – Sandeep K. Jun 20 '21 at 22:37
  • 1
    I guess you need to listen on `0.0.0.0`. See [https://flask.palletsprojects.com/en/2.0.x/api/#flask.Flask.run](https://flask.palletsprojects.com/en/2.0.x/api/#flask.Flask.run) – Yz. Jun 20 '21 at 23:10
  • Thanks. This solves the problem. – Sandeep K. Jun 21 '21 at 01:49

1 Answers1

0

Since you are trying to connect a server in your docker network, you should change the host in the connection string that you are using to connect Mysql to the name of the container that you are willing to connect.

For your case, you have to change localhost with "customertransaction_db_host".

Can Arda Aydin
  • 204
  • 2
  • 13
  • I am doing it already. This is the database connection env variable. `DEV_DATABASE_URI=mysql+mysqlconnector://user:123456@customertransaction_db_host:3306/customertransaction_db` The flask app is running properly and connect to the MySQL perfectly. From the container using 'docker exec' command I can get proper response from the flask app with data returning from MySQL as I send a GET request through curl. But from the host machine I am not getting any response. – Sandeep K. Jun 20 '21 at 22:37