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.
Related
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"]
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?
I am trying to create 2x Docker containers:
For my WEB API
For PostgreSQL DB
I am using docker-compose in order to build these containers. Even though I can successfully build them using docker-compose build command, whenever I go to inspect the logs using docker-compose logs -f command, I am getting the following error message:
...
db_1 | 2020-08-19 12:39:07.681 UTC [45] LOG: database system was shut down at 2020-08-19 12:39:07 UTC
db_1 | 2020-08-19 12:39:07.686 UTC [1] LOG: database system is ready to accept connections
web_1 | python: can't open file 'manage.py': [Errno 2] No such file or directory
nlp-influencertextanalysis_web_1 exited with code 2
Everything seems fine with db container, but for some reason inside web container Python cannot locate manage.py file. Here is my file structure:
And here is code for my docker-compose.yml:
version: '3.7'
services:
web:
build: ./services/web
command: python manage.py run -h 0.0.0.0
volumes:
- ./services/web/:/usr/src/app/
ports:
- 5000:5000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=user1
- POSTGRES_PASSWORD=test123
- POSTGRES_DB=influencer_analysis
volumes:
postgres_data:
And here is my code for Dockerfile:
FROM python:3.8.1-slim-buster AS training
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install system dependencies
RUN apt-get update && apt-get install -y netcat
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
# install NLTK dependencies
RUN python -c "import nltk; nltk.download('punkt')"
# copy project
COPY . /usr/src/app/
WORKDIR /usr/src/app/experiments
RUN python train.py --data data/HaInstagramPostDetails.xlsx --update 1
I should note that I've printed out all fines that are located in /usr/src/app when train.py is executed with RUN command from Docker file, and manage.py is there.
I believe the problem is that you have changed the working directory at the end of your Docker file.
You can try to give an exact path to your manage.py file or.
Change the working directory in the Docker file at the end that directs to the app directory.
I think there is problem while changing the Working directory. It should have been
WORKDIR /usr/src/app/web/experiments based on the folder structure.
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
I am using Docker Toolbox to run a Python API. My Docker-Compose for the Python API can be seen below:
flask-api:
container_name: flask_api
restart: always
build:
context: ./api/
dockerfile: Dockerfile
ports:
- "5000:80"
volumes:
- ./api:/usr/src/app
The Dockerfile for this flask-api is found in the ./api folder. The Dockerfile is:
FROM python:3-onbuild
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY requirements.txt /usr/src/app
RUN pip install -r requirements.txt
COPY . /usr/src/app
CMD ["python", "app.py"]
The app.py file can be found in the ./api folder. However, given the Dockerfile and Docker-compose setup, running docker-compose finishes with the flask-api container crashing, claiming the "app.py is not found".
Somethings I have tried:
I converted the Python image to an Ubuntu image with the same Dockerfile, ran it in interactive mode, and found that the files were indeed copied over.
I ran the Python image in interactive mode as well, used os.lisrdir() to list files at the current directory, and once again found that the files were indeed copied over.
Any ideas on the cause of this issue? Please let me know if there is any other information I can report.