Home > Mobile >  Docker --> python Postgress running successfully in the Docekr images - but NOT ABLE to hit the u
Docker --> python Postgress running successfully in the Docekr images - but NOT ABLE to hit the u

Time:04-25

My Docker File is

# syntax=docker/dockerfile:1
FROM python:3.7.2
RUN mkdir my_app
COPY . my_app
WORKDIR /my_app
ARG DB_URL=postgresql://postgres:postgres@my_app_db:5432/appdb
ARG KEY=abcdfg1
ENV DATABASE_URL=$DB_URL
EXPOSE 8080:8080
EXPOSE 5432:5432
RUN pip install -r requirements.txt
WORKDIR /my_app/app
RUN python manage.py db init
CMD ["python", "my_app.py" ]

My Docker-composer.yml is

version: '3'
 services:
  postgres_db:
    image: postgres:11.1
    container_name: my_app_db
    ports:
     - 5432:5432
    restart: always
    environment:
     POSTGRES_USER: postgres
     POSTGRES_PASSWORD: postgres
     POSTGRES_DB: appdb
    volumes:
     - pgdata:/var/lib/postgresql/data/
  app_api:
   build:
    context: .
    dockerfile: Dockerfile
   container_name: my_app
   ports:
     - 8080:8080
   volumes:
     - ./:/app
   depends_on:
     - postgres_db
volumes:
  pgdata:

The Flask my_app.py:

if __name__ == '__main__':
create_app(app).run(host='0.0.0.0')

i Run the commands below:

  1. docker login
  2. docker-compose up -d

2 containers Running

CONTAINER ID   IMAGE                              COMMAND                  CREATED       STATUS       PORTS                              NAMES
f495fd1f7b38   my_project_my_app                  "python my_app.py…"      3 hours ago   Up 3 hours   5432/tcp, 0.0.0.0:8080->8080/tcp   my_app
2ae314034656   postgres:11.1                      "docker-entrypoint.s…"   3 hours ago   Up 3 hours   0.0.0.0:5432->5432/tcp             my_app_db

I can see the terminal of the bash of my_app running as below:

Running on http://0.0.0.0:5000/ (Press CTRL C to quit)
Restarting with stat
Debugger is active!
Debugger PIN: 602-498-436

I can run Curl in that terminal (via Docker exec -it my_app bash ) and succesfully call my apis in the app and get responses. THe app and the db container are communicating succesfully.

If try to run my app from postman on my pc and hit the below urls:

http://127.0.0.1:8080/myapp/api/v1/endpoint/1

http://localhost:8080/myapp/api/v1/endpoint/1

nothing happens!!!

OS: Windonws 10

1. How could i call my apis from my postman or local machine?

The problem seems to be from my pc to the my_app on local Docker

CodePudding user response:

well, the actual question is how do you run your docker engine. With LinuxVM inside Hyper-V?

Are you using Docker Desktop?

The answer to that would be, to find out on what IP is your VM running/listening on and then you can call your API with:

http://yourVMIP:8080/myapp/api/v1/endpoint/1

And it will probably work. I know I had to find out my Hyper-V Linux IP to link it up for a test drive.

CodePudding user response:

In the end - i did not know about the WSL2 features- once that was enabled i have re-instaleed Docker Desktop - configure WSL version 2 and install Ubuntu terminal for windowns.

DB
 ports:
     - 5433:5432

APP
  ports:
        - 5001:5000

I called the http://locahost:5001/myapp/api and it worked.

  • Related