Why can't docker compose find uvicorn module - python

I am new to docker and was trying to dockerize my fastapi application.
I built a Dockerfile shown below
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN apt-get update
RUN apt-get -y install libpq-dev gcc
RUN apt-get -y install libnss3-tools
RUN apt-get -y install curl
RUN curl -LJO https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64
RUN mv mkcert-v1.4.4-linux-amd64 mkcert
RUN chmod +x mkcert
RUN ./mkcert -install
RUN ./mkcert -cert-file cert.pem -key-file key.pem 0.0.0.0 localhost 127.0.0.1 ::1
RUN pip3 install -r requirements.txt
COPY . .
CMD ["python3.8", "-m", "uvicorn", "main:app", "--host=0.0.0.0", "--ssl-keyfile=./key.pem", "--ssl-certfile=./cert.pem"]
and ran the containers and they all worked. But when I try to combine the containers with docker compose its tells me can't find uvicorn module even when it's in the requirements.txt file .
Here is a snippet of my docker compose file containing the server service.
services:
server:
container_name: server
image: python:3.8-slim-buster
command: ["python3.8", "-m", "uvicorn", "main:app", "--host=0.0.0.0", "--ssl-keyfile=./key.pem", "--ssl-certfile=./cert.pem"]
ports:
- 8000:8000
working_dir: /app
I have tried using changing the command part of the server service in docker compose to
command: bash "python3.8 -m uvicorn main:app --host=0.0.0.0 --ssl-keyfile=./key.pem --ssl-certfile=./cert.pem"
didn't work.
changed it to
command: sh -c "python3.8 -m uvicorn main:app --host=0.0.0.0 --ssl-keyfile=./key.pem --ssl-certfile=./cert.pem"
didn't work.
I removed the command totally it still didn't work, keeps showing
server | /usr/local/bin/python3.8: No module named uvicorn
server exited with code 1

The image you use in the docker compose is not the one previously built in the Dockerfile but a basic Python image.
You could build the image from your Dockerfile
docker build . -t fastapi
then modify your docker-compose.yml file with something like this
services:
api:
image: fastapi
ports:
- "8000:8000"
then run docker compose
docker-compose -f docker-compose.yml up

Related

Install python packages from containerized pypiserver into containerized flask app

I have two docker containers. First one is pypiserver, that contains a package I've created. Second one is my flask app that will install that package from pypiserver. I build those containers with docker-compose, and after that I go into the app container and install that package. It works fine. However, when I tried to install that package in Dockerfile, while building the app, it does not work.
This is my docker-compose.yaml file:
version: '3.9'
services:
test-pypiserver:
image: pypiserver/pypiserver:latest
ports:
- 8090:8080
volumes:
- ./pypiserver/packages:/data/packages
networks:
- test-version-2-network
test-flask:
build: ./dashboard/.
container_name: test-flask
ports:
- 5000:5000
volumes:
- ./dashboard:/code
depends_on:
- test-pypiserver
networks:
- test-version-2-network
This is my Dockerfile for my flask app:
FROM python
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_RUN_PORT=5000
ENV FLASK_DEBUG=1
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN pip install --trusted-host test-pypiserver --extra-index-url http://test-pypiserver:8080 osh
COPY . .
EXPOSE 5000
CMD [ "flask", "run" ]
When I command out this line from Dockerfile
pip install --trusted-host test-pypiserver --extra-index-url http://test-pypiserver:8080 osh
and use it in app container, it works properly
Is there any way to do that? Or is it the proper way to install my packages?
docker-compose up command first build containers and then (after that all containers are builded) make them running. When he try to build your flask application the pypiserver is not running already, so the package installation fails.
You can try to install package when the container is starting.
CMD [ "/bin/sh", "-c", "pip install --trusted-host test-pypiserver --extra-index-url http://test-pypiserver:8080 osh; flask run" ]

Can't open localhost in the browser on port given in docker-compose

I am trying to build and run django application with docker and docker-compose.
docker-compose build example_app and docker-compose run example_app run without errors, but when I go to http://127.0.0.1:8000/ page doesn't open, I'm just getting "page is unavailable" error in the browser.
Here is my Dockeffile, docker-compose.yml and project structure
Dockerfile
FROM python:3.9-buster
RUN mkdir app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
COPY ./requirements_dev.txt /app/requirements_dev.txt
RUN pip install --upgrade pip
RUN pip install -r /app/requirements.txt
docker-compose.yml
version: '3'
services:
example_app:
image: example_app
build:
context: ../
dockerfile: ./docker/Dockerfile
command: bash -c "cd app_examples/drf_example && python manage.py runserver"
volumes:
- ..:/app
ports:
- 8000:8000
project structure:
──app
──app_examples/drf_example/
────manage.py
────api
────drf_example
──requirements.txt
──requirements_dev.txt
──docker/
────docker-compose.yml
────Dockerfile
By default, Django apps bind to 127.0.0.1 meaning that they'll only accept connections from the local machine. In a container context, the local machine is the container, so your app won't accept connections from outside the container.
To get it to accept connections from anywhere, you add the bind address to the runserver command. In your case, you'd change the command in your docker-compose.yml file to
command: bash -c "cd app_examples/drf_example && python manage.py runserver 0.0.0.0:8000"
you need to expose port 8000 in your Docker file
FROM python:3.9-buster
EXPOSE 8000
RUN mkdir app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
COPY ./requirements_dev.txt /app/requirements_dev.txt
RUN pip install --upgrade pip
RUN pip install -r /app/requirements.txt

Failed to run Python3 http.server on Docker's container

I'm trying to create a Docker container which runs a Python http.server listening on port 8000. This is my Dockerfile:
FROM ubuntu:20.04 AS focal
WORKDIR /usr/src/server
COPY . .
RUN apt update && apt-get install -y build-essential python python3 zip net-tools iptables sudo curl
CMD ["python3", "-m http.server", "8000"]
First, I successfully built the image: docker build -t py_server .
Then I tried to run the image as a container: docker run --rm -p 8000:8000 py_server
But the following error was thrown:
/usr/bin/python3: Error while finding module specification for ' http.server' (ModuleNotFoundError: No module named ' http')
Not sure why python3 wasn't able to find the http module when specified with CMD in the Dockerfile. I tested whether python3 in the container has http.server by directly executing the command using bash on the py_server image, and it worked:
$ docker run -it --rm -p 8000:8000 py_server bash
root#d3426b37cf2e:/usr/src/cs435_mp1_server# python3 -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
I'm very confused about this.
I just noticed that there was a space in front of "http.server" which made python3 unable to find the module.
I edited the CMD line on my Dockerfile and it is working now:
CMD ["python3", "-m", "http.server", "8000"]
Still not very sure why a space was previously added in front of "http.server" though.

not able to access Django project through Docker Compose

I have created a simple Docker file for DJango project and when I issue docker run, I am able to access through browser.
docker run -p 8000:8000 s3bucket-ms:1
Here is the Docker File:
FROM python:3.6.7-alpine
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install --upgrade pip
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev
RUN pip install -r requirements.txt
RUN mkdir /app
WORKDIR /app
ADD ./s3bucket /app/
EXPOSE 8000
CMD ["python", "./manage.py", "runserver", "0.0.0.0:8000"]
However, when I am using Docker Compose , i can't access the project through the browser.
Here is the docker-compose.yml
version: '3'
services:
web:
build: .
command: python ./manage.py runserver 8000
ports:
- "8000:8000"
With Docker Compose , I also commented CMD within Docker File.
Output from Docker Compose UP
web_1 | Run 'python manage.py migrate' to apply them.
web_1 | February 17, 2020 - 14:29:22
web_1 | Django version 3.0.3, using settings 's3bucket.settings'
web_1 | Starting development server at http://127.0.0.1:8000/
web_1 | Quit the server with CONTROL-C.
Am I missing something? Any help is appreciated.
Thanks,
Add EXPOSE 8000 on your Dockerfile
Update1:
With docker run
docker run \
-v /path/to/s3bucket:/app \ # absolute path
-p 8000:8000 \
s3bucket-ms:1
With docker-compose
version: '3'
services:
web:
build: .
command: python ./manage.py runserver 8000
volumes:
- /path/to/s3bucket:/app # absolute path
ports:
- "8000:8000"
More infos on https://docs.docker.com/storage/volumes/

pserve not working inside docker container

I am trying to develop pyramid application with using docker-container.I build a docker images with below docker file.
FROM ubuntu
RUN apt-get -y update
RUN apt-get -y install python3.6 python3.6-dev libssl-dev wget git python3-pip libmysqlclient-dev
WORKDIR /application
COPY . /application
RUN pip3 install -e .
EXPOSE 6543
This is my docker-compose file
version: '3'
services:
webserver:
ports:
- 6543:6543
build:
context: .
dockerfile: Dockerfile-development
volumes:
- .:/application
command: pserve development.ini --reload
The docker image is created successfully. But when i run the docker-compose up and browse the url localhost:6543 it is showing The site can't be reached now. But when i run it locally with pserve development.ini it is working fine. I tried to connect to the docker interactively and run the command pserve develpment.ini, It is showing as
Starting server in PID 18.
Serving on http://localhost:6543
But when i browse the url from chrome it is not working.
You need to listen in all network interfaces. In your development.ini file, use:
listen = *:6543
You should get a log which says:
Serving on http://0.0.0.0:6543
Then try to access it from your host machine using localhost:6543.

Categories

Resources