My dockerfile looks like this
FROM python:3.8.10
COPY ./requirements.txt /code/requirements.txt
WORKDIR /code
RUN pip install -r requirements.txt
COPY ./app /code/app
EXPOSE 8000
RUN ['uvicorn', 'app:app']
My questions are:
In second COPY command is /code/app an absolute path and will copy app into code/app or relative path and will copy app into /code/code/app?
If this path is relative how do I make it absolute?
How do I change working directory to root folder?
Related
Failed to compute cache key: "/films" not found: not found ?
My app structure:
My Dockerfile :
FROM python:3.7
RUN useradd --create-home userapi
WORKDIR /films
COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY films/ .
RUN crown -R userapi:userapi ./
USER userapi
EXPOSE 5000
CMD ["python", "./wsgi.py"]
I got error:
=> ERROR [7/8] COPY films/ .
failed to compute cache key: "/films" not found: not found
why can't find films ??
The Dockerfile for your app is within the ~/Documents/films local directory. When you build your Docker image from the ~/Documents/films, this folder is the build context that is referred to by relative path . (e.g. ./data) so it is not found by its full name (e.g. films)
You should use the COPY instruction to copy the files from . to their location in the image filesystem. If you want all the files in ~/Documents/films to be in a root directory of the image with the name "/films", change the instruction to:
COPY . /films
Otherwise, you can specify which files/folders you want to copy within the build context as you have done with the COPY requirements.txt . instruction.
e.g.:
COPY ./data /films/data
COPY ./config.py /films/config.py
Ive tried all solutions to this issue I could find and no luck.
My directory looks like this:
-automation
--app
---requirements.txt
--archive
--Dockerfile
How can I get the Dockerfile to recognize the requirements.txt?
FROM *secret*/python:3.8
WORKDIR /automation
RUN pwd
COPY requirements.txt ./app/requirements.txt
RUN pip install -r requirements.txt
ADD automation automation/
RUN python3 ./app/main.py
your problem related to your work directory.
Your Dockerfile must be like taht:
FROM *secret*/python:3.8
WORKDIR /app # change this from automation to app
RUN pwd
COPY app/requirements.txt ./app/requirements.txt # this line change because your requirement.txt is inside the folder app
RUN pip install -r requirements.txt
Copy . . # This mean copy all you files to /app
# ADD automation automation/ # delete this line
RUN python3 ./app/app/main.py # update also this line main.py will be inside the folder app inside the working directory app
I hope that can help you to resolve your issue.
Have this Dockerfile
Dockerfile
FROM python:3.8
ADD Pipfile.lock /app/Pipfile.lock
ADD Pipfile /app/Pipfile
WORKDIR /app
COPY . /app
RUN pip install pipenv
RUN pipenv install --system --deploy --ignore-pipfile
ENV FLASK_APP=app/http/api/endpoints.py
ENV FLASK_RUN_PORT=4433
ENV FLASK_ENV=development
ENTRYPOINT ["python"]
CMD ["-m", "flask", "run"]
Why in the Docker container my app lands in
/app/app/http/api
App gets duplicated
How to copy it to:
/app/http/api
How can I fix it?
Update 1:
My docker-compose and Dockerfile and ls output.
https://0bin.net/paste/ePRws72M#IV8H6iRJ+UMJFr8lB7CRQYKwsnGYflsmOlyvFxZA7zE
You got two problems here
copy . /app copies the whole working directory from the host machine into the /app directory in the image. If you just want the ./app subdirectory change that to copy ./app /app
Your docker-compose mounts the current working directory of the host machine into the container under the mount point /app . This hides the existing directory /app of the image. So you will either need to copy your app to a different directory or use a different mount point for your volume
I have a Dockerfile that explicitly defines which directores and files from the context directory are copied to the app directory. But regardless of this Docker tries to copy all files in the context directory.
The Dockerfile is in the context directory.
My test code and data files are in directories directly below the context directory. It attempts to copy everything in the context directory, not just the directories and files specified by my COPY commands. So I get a few hundred of these following ERROR messages, except specifying each and every file in every directory and sub directory:
ERRO[0043] Can't add file /home/david/gitlab/etl/testdata/test_s3_fetched.csv to tar: archive/tar: missed writing 12029507 bytes
...
ERRO[0043] Can't close tar writer: archive/tar: missed writing 12029507 bytes
Sending build context to Docker daemon 1.164GB
Error response from daemon: Error processing tar file(exit status 1): unexpected EOF
My reading of the reference is that it only copies all files and directories if there are no ADD or COPY directives.
I have tried with the following COPY patterns
COPY ./name/ /app/name
COPY name/ /app/name
COPY name /app/name
WORKDIR /app
COPY ./name/ /name
WORKDIR /app
COPY name/ /name
WORKDIR /app
COPY name /name
My Dockerfile:
FROM python3.7.3-alpine3.9
RUN apk update && apk upgrade && apk add bash
# Copy app
WORKDIR /app
COPY app /app
COPY configfiles /configfiles
COPY logs /logs/
COPY errorfiles /errorfiles
COPY shell /shell
COPY ./*.py .
WORKDIR ../
COPY requirements.txt /tmp/
RUN pip install -U pip && pip install -U sphinx && pip install -r /tmp/requirements.txt
EXPOSE 22 80 8887
I expect it to only copy my files without the errors associated with trying to copy files I have not specified in COPY commands. Because the Docker output scrolls off my terminal window due to aqll thew error messages I cannot see if it succeeded with my COPY commands.
All files at and below the build directory are coppied into the initial layer of the docker build context.
Consider using a .dockerignore file to exclude files and directories from the build.
Try to copy the files in the following manner-
# set working directory
WORKDIR /usr/src/app
# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
# add app
COPY ./errorfiles /usr/src/app
Also, you will have to make sure that your docker-compose.yml file is correctly built-
version: "3.6"
services:
users:
build:
context: ./app
dockerfile: Dockerfile
volumes:
- "./app:/usr/src/app"
Here, I'm assuming that your docker-compose.yml file is inside the parent directory of your app.
See if this works. :)
I understand that COPY can copy files from source folder to destination, but what is purpose of COPY . .?
For example:
FROM ruby:2.5.1
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .
CMD ["./your-daemon-or-script.rb"]
That will copy the current working directory of your local machine into the current working directory of the container.
See the WORKDIR
This command will copy all the content of the parent directory in which your dockerfile resides(contents of your machine) to the location of the WORKDIR in your container.