How do I help Docker find Python 3.9 on my system? - python

I'm trying to build a rudimentary API and deploy it to a cloud environment with Docker. My API can be found in this directory here: https://github.com/n8feldman/cashman-flask-project. Basically, I've built a Flask API that can add "income" or "expense" items to an array through RESTful endpoints.
I downloaded Docker today and built the following Dockerfile in the root directory:
# Using lightweight alpine image
FROM python:3.6-alpine
# Installing packages
RUN apk update
RUN pip install --no-cache-dir pipenv
# Defining working directory and adding source code
WORKDIR /usr/src/app
COPY Pipfile Pipfile.lock bootstrap.sh ./
COPY cashman ./cashman
# Install API dependencies
RUN pipenv install
# Start app
EXPOSE 5000
ENTRYPOINT ["/usr/src/app/bootstrap.sh"]
I then run the command docker build -t cashman . from the root directory of my project and get the following output:
[+] Building 1.1s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.6-alpine 0.3s
=> [1/7] FROM docker.io/library/python:3.6-alpine#sha256:579978dec4602646fe1262f02b96371779bfb0294e92c91392707fa999c0c989 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.01kB 0.0s
=> CACHED [2/7] RUN apk update 0.0s
=> CACHED [3/7] RUN pip install --no-cache-dir pipenv 0.0s
=> CACHED [4/7] WORKDIR /usr/src/app 0.0s
=> CACHED [5/7] COPY Pipfile Pipfile.lock bootstrap.sh ./ 0.0s
=> CACHED [6/7] COPY cashman ./cashman 0.0s
=> ERROR [7/7] RUN pipenv install 0.7s
------
> [7/7] RUN pipenv install:
#11 0.607 Warning: Python 3.9 was not found on your system...
#11 0.608 Neither 'pyenv' nor 'asdf' could be found to install Python.
#11 0.608 You can specify specific versions of Python with:
#11 0.608 $ pipenv --python path/to/python
------
executor failed running [/bin/sh -c pipenv install]: exit code: 1
How can I decipher the error message at the bottom and fix the issue?

Related

RUN apk --no-cache add shadow rsync && mkdir /app

I'm going to run a project which runs with docker and when I run the following command: docker-compose -f local.yml up --build . I am canceling a process and I am presented with an error as seen below from this point I am presented with the error
I am working with windows 10, in a project that contains python, Django, Vue, docker and I already installed the requirements and still this error appears.
`
` => CANCELED [ares_local_django 2/11] RUN apt-get update && apt-get install -y build-essential && apt-get install -y libpq-dev && apt-get install -y gett 18.6s
=> ERROR [ares_local_vue 2/6] RUN apk --no-cache add shadow rsync && mkdir /app 16.6s
=> CACHED [ares_production_postgres 2/4] COPY ./compose/production/postgres/maintenance /usr/local/bin/maintenance 0.0s
=> CACHED [ares_production_postgres 3/4] RUN chmod +x /usr/local/bin/maintenance/* 0.0s
=> CACHED [ares_production_postgres 4/4] RUN mv /usr/local/bin/maintenance/* /usr/local/bin && rmdir /usr/local/bin/maintenance 0.0s
=> [ares_production_postgres] exporting to image 1.5s
=> => exporting layers 0.0s
=> => writing image sha256:0fc325a12edf89cce8cfd203af7b9ac57125b703a0a48661c6a6cd1808370474 0.3s
=> => naming to docker.io/library/ares_production_postgres 0.0s
------
> [ares_local_vue 2/6] RUN apk --no-cache add shadow rsync && mkdir /app:
#0 6.000 fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
#0 11.04 fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
#0 11.04 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.15/main: temporary error (try again later)
#0 16.04 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.15/community: temporary error (try again later)
#0 16.04 ERROR: unable to select packages: 0.0s
#0 16.06 rsync (no such package):
#0 16.06 required by: world[rsync]
#0 16.06 shadow (no such package):
#0 16.06 required by: world[shadow]
------
failed to solve: executor failed running [/bin/sh -c apk --no-cache add shadow rsync && mkdir /app]: exit code: 2`

Failed to solve with frontend Dockerfile Error - Robotframework

I am working on robotframework and learning how to integrate docker with this.
I am trying to create image of docker file
My docker file is stored here :
C:\Docker
Filename : Dockerfile (File Type : FILE)
Content in the file -
FROM Python
RUN apt-get update
RUN apt install python3.7
I have docker app installed in my windows machine.
When I run this command : docker build . or docker build -f Dockerfile or docker build -f C:\Docker\Dockerfile
I am getting this error -
[+] Building 0.0s (2/2) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 2B 0.0s
=> CANCELED [internal] load .dockerignore 0.0s
=> => transferring context: 0.0s
failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount4275530885/Dockerfile: no such file or directory
I tried running different commands
docker build .
docker build -f Dockerfile
docker build -f C:\Docker\Dockerfile
I am just expecting that the image file to be created.

How to stop uvicorn ASGI web server running when building Dockerfile?

Docker build running endlessly
I'm trying to build a docker image, but i'm running into an issue with uvicorn server running while building which causes it to never build.
so i'm looking for an alternative way of building/running the docker image.
Required the docker image should run the uvicorn server on startup
Haven't found any real solution from browsing SOF/Google
Code
main.py file
from fastapi import FastAPI, Request, Response
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from src.scraper import Scraper
app = FastAPI()
templates = Jinja2Templates(directory="src/templates")
app.mount("/static", StaticFiles(directory="src/static"), name="static")
scraper = Scraper()
scraper.scrapedata()
#app.get("/")
async def home(request: Request):
return templates.TemplateResponse("index.html", {"request": request, "items": scraper.scrapedata()})
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=8000)
Dockerfile
FROM python:3.9.2-buster
ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"
COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt
WORKDIR /website
COPY . /website
RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
USER webuser
EXPOSE 8000
#this is the cause for endless running. Any other way to do this?
RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000
Console output
[+] Building 15.2s (11/12)
[+] Building 974.8s (11/12)
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 512B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 34B 0.0s
=> [internal] load metadata for docker.io/library/python:3.9.2-buster 2.4s
=> [1/8] FROM docker.io/library/python:3.9.2-buster#sha256:56f1b4dbdebb3b6ec31126e256c0852d18e79909ed1df8b594e56 0.0s
=> [internal] load build context 0.2s
=> => transferring context: 135.50kB 0.2s
=> CACHED [2/8] COPY requirements.txt . 0.0s
=> CACHED [3/8] RUN python -m pip install --upgrade pip 0.0s
=> CACHED [4/8] RUN python -m pip install -r requirements.txt 0.0s
=> CACHED [5/8] WORKDIR /website 0.0s
=> [6/8] COPY . /website 0.3s
=> [7/8] RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website 5.5s
=> [8/8] RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000 966.2s
=> => # 200
=> => # INFO: Started server process [7]
=> => # INFO: Waiting for application startup.
=> => # INFO: Application startup complete.
=> => # INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Use CMD instead of RUN to launch the uvicorn server in the Dockerfile. It will retard the execution of the command to when the container is launched.
RUN runs commands during the image building.
CMD runs commands during container launching.
Your Dockerfile would be rewritten as follows:
FROM python:3.9.2-buster
ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"
COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt
WORKDIR /website
COPY . /website
RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
USER webuser
EXPOSE 8000
CMD ["python", "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]

Dockerfile build image error: Distribution contains no modules or packages for namespace package

I'm trying to build a docker image of a python project. My project has one dependency package, "my_package" which needs to be install first. It's a namespace package.
I'm having this error when building docker image of this python project. I can pretty much install everything with RUN pip install XYZ inside Dockerfile but I'm not sure what's the proper way to install local python namespace packages, here, my_package, properly.
I followed this exact same steps on a normal python package that is NOT A NAMESPACE PACKAGE and it works absolutely fine. Any pointers to install python namespace packages in Docker?
Dockerfile:
ARG BASE_IMAGE=ubuntu:18.04
FROM ${BASE_IMAGE} AS compile-image
ARG BASE_IMAGE=ubuntu:18.04
ENV PYTHONUNBUFFERED TRUE
RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
ca-certificates \
g++ \
python3.8-dev \
python3.8-distutils \
python3.8-venv \
openjdk-11-jre-headless \
curl \
&& rm -rf /var/lib/apt/lists/* \
&& cd /tmp \
&& curl -O https://bootstrap.pypa.io/get-pip.py \
&& python3.8 get-pip.py
RUN python3.8 -m venv /home/venv
ENV PATH="/home/venv/bin:$PATH"
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
RUN update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1
COPY my_package/* /tmp/my_package/
WORKDIR /tmp/my_package/
RUN python setup.py install
Docker build command:
$ DOCKER_BUILDKIT=1 docker build --file Dockerfile .
Error:
[+] Building 0.6s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 907B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:18.04 0.1s
=> [internal] load build context 0.0s
=> => transferring context: 3.11kB 0.0s
=> [compile-image 1/8] FROM docker.io/library/ubuntu:18.04#sha256:4bc3ae6596938cb0d9e5ac51a1152ec9dcac2a1c50829c74abd9c4361e321b26 0.0s
=> CACHED [compile-image 2/8] RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install 0.0s
=> CACHED [compile-image 3/8] RUN python3.8 -m venv /home/venv 0.0s
=> CACHED [compile-image 4/8] RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 0.0s
=> CACHED [compile-image 5/8] RUN update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1 0.0s
=> [compile-image 6/8] COPY my_package/* /tmp/my_package/ 0.0s
=> [compile-image 7/8] WORKDIR /tmp/my_package/ 0.0s
=> ERROR [compile-image 8/8] RUN python setup.py install 0.4s
------
> [compile-image 8/8] RUN python setup.py install:
#12 0.370 error in my_package setup command: Distribution contains no modules or packages for namespace package 'my_package'
------
executor failed running [/bin/sh -c python setup.py install]: exit code: 1
my_package structure:
my_package
├── [4.0K] my_package
│   └── [ 56] __init__.py
└── [ 162] setup.py
setup.py:
from setuptools import setup, find_packages
setup(
name='my_package',
version='0.0.1',
namespace_packages=['my_package'],
packages=find_packages()
)
__init__.py file:
__import__('pkg_resources').declare_namespace(__name__)
In your Dockerfile, you need to put :
COPY my_package/. /tmp/my_package/

Docker experimental build still uses a system cache after mounting a different cach for pip installs

I was following this StackOverflow answer (https://stackoverflow.com/a/58021389/11268971) to get my Docker build image process to use a cache directory with package wheels for pip install. However, when I switched from (suggested in the SO answer)
# syntax = docker/dockerfile:experimental
FROM python:3.7
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
to
# syntax = docker/dockerfile:experimental
FROM python:3.7
RUN --mount=type=cache,target=/home/my_username/new_dir_i_created_just_for_docker_builds pip install --no-index --find-links=/home/my_username/new_dir_i_created_just_for_docker_builds -r requirements.txt
Docker buildkit started failing to mount the new directory, it still looks for packages in the old directory I mounted /root/.cache/pip.
I tried clearing the cache with docker builder prune and its different flags. To no avail.
Any ideas how I can get this to work?
P. S. I decided to create my own directory under my user, since installing /root/.cache/pip would regularly stop (docker build would download packages from the Net, instead of installing cached versions) after the first 4 or so builds or 1 day after initial cache mount. My hypothesis is that /root/.cache/pip was overwritten or invalidated.
EDIT:
My Dockerfile
# syntax = docker/dockerfile:experimental
FROM python:3.7
ADD . /app
WORKDIR /app
RUN --mount=type=cache,target=/home/my_username/new_dir_i_created_just_for_docker_builds pip install --no-index --find-links=/home/my_username/new_dir_i_created_just_for_docker_builds -r requirements.txt
ENV CONTAINER_ID=""
ENV HOST=""
ENV PORT=""
ENV MODELPATH=""
CMD "python" "/app/model.py" $CONTAINER_ID $HOST $PORT $MODELPATH
My Docker build logs:
#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 468B done
#2 DONE 0.1s
#3 resolve image config for docker.io/docker/dockerfile:experimental
#3 DONE 3.0s
#4 docker-image://docker.io/docker/dockerfile:experimental#sha256:de85b2f3a...
#4 CACHED
#5 [internal] load metadata for docker.io/library/python:3.7
#5 DONE 0.0s
#7 [internal] load build context
#7 transferring context: 1.00kB done
#7 DONE 0.0s
#6 [stage-0 1/4] FROM docker.io/library/python:3.7
#6 CACHED
#8 [stage-0 2/4] ADD . /app
#8 DONE 1.2s
#9 [stage-0 3/4] WORKDIR /app
#9 DONE 0.2s
#10 [stage-0 4/4] RUN --mount=type=cache,target=/home/my_username/new_dir_i_created_just_for_docker_builds ...
#10 3.110 Looking in links: /home/my_username/new_dir_i_created_just_for_docker_builds/
#10 3.119 ERROR: Could not find a version that satisfies the requirement absl-py==0.9.0 (from -r /app/requirements.txt (line 1)) (from versions: none)
#10 3.119 ERROR: No matching distribution found for absl-py==0.9.0 (from -r /app/requirements.txt (line 1))
#10 ERROR: executor failed running [/bin/sh -c pip install --no-index --find-links=/home/my_username/new_dir_i_created_just_for_docker_builds -r /app/requirements.txt]: runc did not terminate sucessfully
------
> [stage-0 4/4] RUN --mount=type=cache,target=/home/my_username/new_dir_i_created_just_for_docker_builds pip install --no-index --find-links=/home/rytis/test-cont-cache/ -r /app/requirements.txt:
------
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = failed to build LLB: executor failed running [/bin/sh -c pip install --no-index --find-links=/home/my_username/new_dir_i_created_just_for_docker_builds/ -r /app/requirements.txt]: runc did not terminate sucessfully

Categories

Resources