0

I was trying to use docker for my flask backend along with postgres database. Here is my docker-compose.yml file

version: "2"

services:
  api:
    ports:
      - 5000:5000
    build:
      context: ./mymeds
      dockerfile: ./Dockerfile
    restart: unless-stopped
    networks:
      - mymeds-backend-tier
    depends_on:
      - db
    volumes:
      - ./mymeds/app/:/app/app:Z
    command: python manage.py run
    env_file: common.env

  db:
    image: library/postgres:11.1-alpine
    ports:
      - 5432:5432
    restart: unless-stopped
    networks:
      - mymeds-backend-tier
    volumes:
      - mymeds-db:/var/lib/postgresql
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=user123

volumes:
  mymeds-db:
    driver: local

networks:
  mymeds-backend-tier:
    driver: bridge

Here is my Dockerfile for flask backend

### Build and install packages
FROM python:3.8 as build-python

RUN apt-get -y update \
  && apt-get install -y gettext \
  # Cleanup apt cache
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt /app/
WORKDIR /app
RUN pip install -r requirements.txt

### Final image
FROM python:3.8-slim


RUN apt-get update \
  && apt-get install -y \
    libxml2 \
    libssl1.1 \
    libcairo2 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libgdk-pixbuf2.0-0 \
    shared-mime-info \
    mime-support \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

COPY . /app
COPY --from=build-python /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/
COPY --from=build-python /usr/local/bin/ /usr/local/bin/
WORKDIR /app

EXPOSE 5000
ENV PORT 5000

When I put command

sudo docker-compose run

It shows api_1 | * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) But I can't access this from my host It shows This site can’t be reached

enter image description here

Update

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

Solution from here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.