Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a Laravel's .env file into a Docker container

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
like image 233
melkawakibi Avatar asked Jul 08 '17 10:07

melkawakibi


People also ask

Does Docker use .env file?

The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy . Both $VARIABLE and ${VARIABLE} syntax are supported.

How do I pass an environment variable in Dockerfile?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

How do I set an environment variable on an existing container?

Use the -e , --env , and --env-file flags to set simple (non-array) environment variables in the container you're running, or overwrite variables that are defined in the Dockerfile of the image you're running.


1 Answers

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.

like image 141
yahyazini Avatar answered Nov 15 '22 03:11

yahyazini