I'm trying to create a GET API endpoint in python flask and run the flask in Docker container but I'm unable to access the API running inside the container. I tried looking on other answers but nothing worked.
server.py
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)
Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.10.4
WORKDIR /code
ENV FLASK_APP=server.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_DEBUG="true"
EXPOSE 5000
RUN pip install --upgrade pip
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
# CMD ["python","server.py"]
CMD ["flask", "run", "--host", "0.0.0.0"]
docker-compose.yml
version: "3.9"
services:
flask-server:
build: ./flask-server/
ports:
- "5000:5000"
restart: on-failure
Related
I'm learning how to run django rest with docker. I created an image, and I it's work when I use the command: docker run -p 8000:8000 docker_django_tutorial
But now I want to run this image through a docker-compose.yml file. Here is mine (it's based on a youtube vidéo, that why I don't understand why it doesn't work f
or me):
version: '3'
services:
monapp:
image: docker_django_tutorial
ports:
- 8000:8000
networks:
- monreaseau
networks:
monreseau:
When I run docker-compose up I've got the following error:
service "monapp" refers to undefined network monreaseau: invalid compose project
Just in case, here is my Dockerfile use for my image docker_django_tutorial:
#Use the Python3.7.2 container image
FROM python:3.7.2-stretch
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
#RUN python3 manage.py runserver
Thank you for you're anwsers.
Someone help me re-write my files like this:
Docker file:
#Use the Python3.7.2 container image
FROM python:latest
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
#CMD ["python3", "manage.py", "runserver", "0.0.0.0:90"]
#RUN python3 manage.py runserver
docker-compose.yml:
version: '3'
services:
monapp:
image: docker_django_tutorial
ports:
- 90:90
build: .
command: python3 manage.py runserver 0.0.0.0:90
This is my Dockerfile for a simple Django project:
FROM python:3.10.5-alpine
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
RUN adduser -D appuser
USER appuser
WORKDIR /home/appuser/
COPY requirements.txt .
RUN python -m pip install --user --no-cache-dir --disable-pip-version-check --requirement requirements.txt
COPY . .
ENTRYPOINT [ "./entrypoint.sh" ]
And Compose file:
version: "3.9"
services:
app:
build: ./app
ports:
- "8000:8000"
volumes:
- ./app:/app
restart: unless-stopped
And entrypoint.sh:
#!/bin/sh
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic --no-input
python manage.py runserver 0.0.0.0:8000
And the structure of the project:
app
|----project
|----venv
|----static
|----.dockerignore
|----Dockerfile
|----entrypoint.sh
|----manage.py
|----requirements.txt
docker-compose.yaml
Everything works except that when python manage.py migrate creates the db.sqlite3 file in the image, and python manage.py collectstatic copies static files to the static folder of the image (both in app folder), I don't see them in app folder of my host machine. Perhaps my understanding of how volumes work is not correct?
I have a docker project which uses docker-compose to stand up an API container and a database container. I also have it setup to create volumes for uploads and logs. When I build a new image, and then stand it up using docker-compose up, any data that was previously in the logs or uploads ceases to exist, but the data in our db-data volume persists. What do I need to change in my process and/or config to preserve the data in those other two volumes?
Here's how we stand up the docker:
docker-compose down
docker build -t api:latest .
docker save -o api.tar api:latest postgres:14
docker load -i api.tar
docker-compose up -d
Here's the Dockerfile:
# syntax=docker/dockerfile:1
FROM cupy/cupy AS deps
COPY nodesource_setup.sh .
RUN bash nodesource_setup.sh
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install libssl-dev ca-certificates cmake nodejs libgl-dev libglib2.0-0 -y
RUN pip3 install opencv-python python-dotenv
WORKDIR /app
COPY ./python ./ml_services/
COPY ./api/ .
RUN npm config set unsafe-perm true
RUN npm ci
# Rebuild the source only when needed
FROM deps AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app .
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build
FROM deps AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/ml_services ./ml_services
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/.env.production.local ./.env
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/.next ./.next
RUN mkdir -p /logs
RUN mkdir -p /uploads
RUN chown -R nextjs ./ /logs /uploads
# Cleanup
RUN apt-get clean && rm -rf /var/lib/apt
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["npm", "run", "deploy"]
And the docker-compose.yml
version: "3.7"
services:
database:
image: postgres:14
restart: always
env_file: .env.production.local
container_name: postgres
healthcheck:
test: "pg_isready --username=autocal && psql --username=autocal --list"
timeout: 10s
retries: 20
volumes:
- db-data:/var/lib/postgresql/data
api:
image: api:latest
ports:
- 3000:3000
depends_on: [database]
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
volumes:
- uploads:/uploads
- logs:/logs
volumes:
db-data:
uploads:
logs:
Make sure you writing logs and uploads in /logs and /uploads in your API code.
It might be the case it is being written it in ~/logs and ~/uploads (in the home directory of execution user).
Try to use absolute path not the relative path in your code.
Please go through node js docs around the path module https://nodejs.org/api/path.html
Absolutely ended up being a code issue in the API layer. Sigh. Thanks all.
I am unable to run a Docker container with a gunicorn entrypoint. I receive the error:
gunicorn_1 | /entrypoint.sh: 46: exec: gunicorn: not found
I'm building the docker images (gunicorn + nginx), hosting in a container registry and then pulling the image. I receive the error when I run docker-compose up on the server.
Dockerfile (gunicorn):
FROM python:3.9.5-slim-buster
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r /usr/src/app/requirements.txt
COPY . /usr/src/app/
EXPOSE 8000
Dockerfile (nginx):
FROM nginx:1.19-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
Docker-compose:
version: '2'
services:
gunicorn:
image: link_to_built_gunicorn_image
command: gunicorn --bind 0.0.0.0:8000 wsgi:app
ports:
- 8000:8000
nginx:
image: link_to_built_nginx_image
ports:
- 80:80
depends_on:
- gunicorn
Requirements.txt:
Flask
oauthlib
pyOpenSSL
gunicorn
I've looked at similar posts (1, 2 and 3) and checked the following:
gunicorn is included in the requirements.txt
Tried downgrading gunicorn version in requirements.txt
Additional information:
Ubuntu 20.04.3 LTS OS hosted with Digital Ocean
I'm running docker-compose to run my application which listen to REST api calls.
For some reason it is not accessible from outside.
I don't understand what am I doing wrong.
Here is the configuration:
version: '3.4'
services:
rabbitmq:
image: rabbitmq:3-management
ports:
- 5672:5672
- 15672:15672
my-server:
image: my-server
build:
context: .
dockerfile: ./apiserver/Dockerfile
ports:
- 5000:5000
restart: on-failure
depends_on:
- rabbitmq
and my Dockerfile is:
FROM ubuntu:16.04
RUN apt-get update -y && \
apt-get install -y python-pip python-dev
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 5000
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]