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

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.

Related

Why can't docker compose find uvicorn module

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

Docker Max retries exceeded with url: /assets

I'm a bit new to docker and I'm messing around with it. I currently have a server being ran on port 5000 in another container. This server is being ran with express and uses JavaScript. I'm trying to send requests to that server with python. I tried using both localhost:5000 and 127.0.0.1:5000 but neither of these seems to work. What can I do? I noticed if I run the python code without docker it works perfectly fine.
Python Docker File:
FROM ubuntu:latest
RUN apt update
RUN apt install python3 -y
RUN apt-get install -y python3-pip
WORKDIR /usr/app/src
COPY . .
RUN pip install discum
RUN pip install python-dotenv
EXPOSE 5000
CMD ["python3", "./src/index.py"]
JavaScript Docker File:
FROM node:latest
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
You could create a network between the to containers using --net look at this answer How to get Docker containers to talk to each other while running on my local host?
Another way, and my preferred way, is to use docker-compose and create networks between your containers.
Use Service Name and Port is always the best.
So if you have a docker file like the below you could use the URL http://client:5000
version: 3.8
services:
client:
image: blah
ports:
- 5000:5000

Python docker image fails because of missing shell / bash in the image

I have the following Dockerfile:
FROM python:3-alpine
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install -qr requirements.txt
COPY target-watch.py .
CMD ["python3", "./target-watch.py"]
If I deploy this to a Kubernetes cluster the build went fine, but I got an error from the Kubernetes logs. To verfiy my image I run the following command:
docker run --rm -it --entrypoint /bin/bash docker-conveyor.xxx.com/myorg/my_cron_jobs:2021.12.08_03.51_abcdef
Which gives me this response:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown.
How can this be fixed? I assume a shell is missing in my image. How do i have to change my Dockerfile to make it work without any errors?
Your container image doesn't have bash so you should use /bin/sh instead of /bin/bash.
docker run --rm -it --entrypoint /bin/sh docker-conveyor.xxx.com/myorg/my_cron_jobs:2021.12.08_03.51_abcdef
Alpine docker image doesn't have bash installed by default. You will need to add the following commands to get bash:
RUN apk update && apk add bash
If you're using Alpine 3.3+ then you can just do
RUN apk add --no-cache bash

I cannot containerise my python code in a docker container

I am learning docker. I want to practice how to see logs inside of a docker container once I run a python image.
This is the python code I want to execute:
#loop.py
#loop.py
import time
while True:
print('test')
time.sleep(3)
This is the docker file:
FROM python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
CMD ["python", "loop.py"]
I build the image like this:
docker build -t image_test .
Once it is created ( I can see it doing docker image ls) I want to run it, I have different ways but the container is created and is terminated instantly:
docker run <imageID>.
I have tried too:
docker docker run <imageID> sleep infinity. Then I can do: docker exec -it <containerID> bash and I can run inside the container python loop.py but it automatically ends.
I modified the CMD like this as well:
CMD ["python","-u", "loop.py"]
I have tried:
docker run -it --entrypoint=/bin/bash image_test and I directly go to the terminal inside of the container, but if I execute the python code as before, it automatically ends, instead of run the infinite loop.
why?
You don't need bash to run a python program. You can use it to exec into a running container. But the container will be running as long as the python is connected to the stdout.
I ran these commands and they all work on my machine.
sudo docker run image_test
sudo docker run -d image_test
sudo docker ps
sudo docker logs -f 041
sudo docker exec -ti 041 bash
sudo docker stop 041
sudo docker rm 041

socket.error: [Errno 99] Address not available..Get this error while trying to link a container with another container of CouchDB in Docker

I am trying to make a Docker Image of a Web Application which is based on python. As I am trying to link the official CouchDB container with the Docker Container that I have created I am getting the "Address not available error". docker ps -a shows me that the couchdb is working in a perfect way.
The command that I have used in the terminal is:
docker run -p 5984:5984 --name my-couchdb -d couchdb
docker run --name my-couchdb-app --link my-couchdb:couchdb webpage:test
My Docker File for webpage:test is given below:
FROM python:2.7.15-alpine3.6
RUN apk update && \^M
apk upgrade && \^M
apk add bash vim sudo
RUN mkdir /home/WebDocker/
ADD ./Webpage1 /home/WebDocker/Webpage1
ADD ./requirements.txt /home/WebDocker/requirements.txt
WORKDIR /home/WebDocker
RUN pip install -r /home/WebDocker/requirements.txt
RUN chmod +x /home/WebDocker/Webpage1/main.py
EXPOSE 8080
ENTRYPOINT ["python","/home/WebDocker/Webpage1/main.py"]

Categories

Resources