skip to Main Content

For some reason I cant copy this .env file with docker compose.

I get an error:

failed to solve: failed to compute cache key: failed to calculate checksum of ref P7HG:437G:LTTO:46LJ:7DOL:LGFQ:P7GQ:YW5M:RZV2:E4OL:HEQW:3GNM::pgh0i83f6t5m5ko3ng9naj74n: "/.env": not found

Here is my project structure:

tree -a  
├── docker-compose.yml  
├── .env  
├── readme.md  
├── src  
│   ├── app.py  
│   ├── authenticate.py  
│   ├── dockerfile  
│   └── requirements.txt  
└── test.txt  

This is my dockerfile:

FROM python:3.10.11
RUN apt-get update
RUN apt-get install nano
RUN mkdir /usr/src/app/

COPY ./requirements.txt ./
RUN pip install -r requirements.txt
COPY ../.env /usr/src/app/
COPY . /usr/src/app/
WORKDIR /usr/src/app/
EXPOSE 5000
CMD ["python", "app.py"]

Even if I put the absolute path, it still does not work:

FROM python:3.10.11
RUN apt-get update
RUN apt-get install nano
RUN mkdir /usr/src/app/

COPY ./requirements.txt ./
RUN pip install -r requirements.txt
COPY /media/frank/T721/dev/app/.env /usr/src/app/
COPY . /usr/src/app/
WORKDIR /usr/src/app/
EXPOSE 5000
CMD ["python", "app.py"]
failed to solve: failed to compute cache key: failed to calculate checksum of ref P7HG:437G:LTTO:46LJ:7DOL:LGFQ:P7GQ:YW5M:RZV2:E4OL:HEQW:3GNM::zvn3xxsp7fh929ia473mpmtvc: "/media/frank/T721/dev/app/.env": not found

But the file is definitly there:

cat /media/frank/T721/dev/app/.env
FLASK_PORT=5000
AUTH_DB_NAME=mydb
AUTH_DB_PORT=1234
AUTH_DB_HOST=10.0.0.28
AUTH_DB_PASSWORD=mypass
AUTH_DB_USER=root

Does it need an escape character or something? I believe I have done this in the past many times and it has worked without issue. I do not have a dockerignore file.

It only works if I move the .env file to the same directory, then it copies everything at once with the command COPY . /usr/src/app/. It doesn’t work if I manually specify the .env file.

2

Answers


  1. COPY ../.env
    

    It is not possible to access anything outside docker context. I will gues that the context is set to src from docker-compose.yml. So it is not possible to access any files outside src, as all files inside src are copied to docker context. In short, it is not possible to copy anything from upper directories.

    Set the context to root and fix relative paths in your dockerfile. Research docker build context.

    Login or Signup to reply.
  2. The issue here is that the Docker build content needs to contain all of the files which you transfer to the image. So if the .env file is not in the build context then you will get those errors.

    There are a couple of options.

    1. Dockerfile in src/

    You can leave your Dockerfile in the src/ directory. You’d need to make a few tweaks to paths though.

    FROM python:3.10.11
    
    RUN apt-get update
    RUN apt-get install nano
    
    RUN mkdir /usr/src/app/
    
    COPY src/requirements.txt ./
    RUN pip install -r requirements.txt
    
    COPY .env /usr/src/app/
    COPY src/* /usr/src/app/
    
    WORKDIR /usr/src/app/
    
    EXPOSE 5000
    
    CMD ["python", "app.py"]
    

    Then from the root directory of the project build your image as follows:

    docker build -f src/Dockerfile .
    

    The . at the end indicates that the current directory should be used as the build context.

    If you really wanted to build from the src/ directory then you could also do something like this:

    docker build -f ../src/Dockerfile ..        # 😱 Don't like this!
    

    The .. at the end boost the build context up one level in the directory hierarchy.

    2. Dockerfile in root

    The other option is to move the Dockerfile up to the root directory of your project. Personally this is the approach that I would take. This would not require any changes to its contents, but would mean that the build command is slightly simpler.

    
    docker build .                              # ⭐ This seems the best option to me!
    

    If you wanted to build from src/ you’d need to do this:

    docker build -f ../Dockerfile ..
    

    Whichever approach you take, if you want to include files from the root directory (like .env) then everything below the root directory must be included in your Docker build context. If there are large files then this will impact your build time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search