8

Setup

I'm running Docker on my Ubuntu server and I'm trying create a Laravel container to run my website with artisan. The Laravel project is inside a GitHub Repository and I clone the project to the docker container with a dockerfile.

Problem

Laravel projects are dependent on the .env (environment files) which are not included in the repo project, for security reasons. So when I clone the repo to the docker container it doesn't include the .env file and thereby doesn't run the website properly. I have an .env file locally on my Ubuntu that I'm trying to COPY to the docker container Laravel project folder, ofcourse it doesn't work. This is because it's looking for the directory in the docker container's file structure.

Error

Step 6/11 : COPY /containers/.env .env

lstat containers/.env: no such file or directory

Question

How can I copy the .env file from the ubuntu server to the docker container with the COPY command?

file structure (Ubuntu) source from:

root/
  containers/
    - docker-compose
    - .env

file structure (docker container) source to:

root/
  var/www/

dockerfile

FROM hitalos/laravel

RUN git config --system http.sslverify false

RUN git clone repo /var/www

RUN git checkout test

COPY /containers/.env .env

# Run Compser Install
RUN composer install -d /var/www
RUN php /var/www/artisan key:generate

WORKDIR /var/www

CMD php /var/www/artisan serve --port=80 --host=0.0.0.0
EXPOSE 80
1
  • why don't you checkout the repo in the host machine, it is not necessary to have the repo in the image/container, you can copy it as well
    – bartimar
    Commented Jul 8, 2017 at 10:36

3 Answers 3

9

Simply copying the .env file is not going to work since you also have to run a source command on the file and then an export command to add each environment variable to your path.

Since you are using docker-compose then you can use the env_file like so in your docker-compose.yml:

env_file: -.env

This should automatically set the values required by Laravel from your .env file when you build your conainer.

1
  • 1
    Yup, I also think that COPY an .env file is not right, and feels dirty! But that if you need those variables at build stage specifically, can you help? stackoverflow.com/questions/56459243/… (sorry for the piggy back on an old question) :D Commented Jun 5, 2019 at 13:08
2

The path in COPY is relative to the Dockerfile

just change it to COPY containers/.env /var/www/.env

EDIT: seems like you don't have the .env file at build time (image), only at runtime (container). That means, you have to mount the file when running the container.

Remove the COPY ... command from Dockerfile and instead run the container with

-v /containers/.env:/var/www/.env

so something like this:

docker run... -v /containers/.env:/var/www/.env ...

or change it in the compose yml file

4
  • 1
    Didn't work still getting: lstat containers/.env: no such file or directory Commented Jul 8, 2017 at 10:46
  • I think I need to remotly access the Ubuntu server somehow to get access ti the directory containers. Commented Jul 8, 2017 at 10:48
  • Wait. You are building the images locally, but running the container on the ubuntu server where you have no access?
    – bartimar
    Commented Jul 9, 2017 at 12:39
  • If you need the .env file at runtime (container), but you don't have the file at build time (image), then simply build it without any COPY command and just simply mount the volume to the container at runtime (docker run... -v /containers/.env:/var/www/.env ...
    – bartimar
    Commented Jul 9, 2017 at 12:42
0
docker run -d --network=your-network --restart unless-stopped --name your-container-name --env-file /host/path/to/.env your-container/name
1
  • Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. Commented Jan 15 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.