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

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Copying Laravel's .env File into a Docker Container: A Developer’s Guide

Working with containerized applications, especially those built on frameworks like Laravel, often introduces unique challenges related to configuration management. One of the most frequent hurdles developers face is managing sensitive environment variables stored in the .env file. For security reasons, these files are typically excluded from version control (like GitHub), meaning they must be injected into the container during deployment.

This post dives deep into a very specific Dockerfile issue encountered when trying to copy a local .env file into a Laravel container, and I will provide the correct solutions, along with the best practices for handling environment configuration in a production-ready setup.

The Problem: Why the COPY Command Fails

You are attempting to use the COPY instruction in your Dockerfile to move a local .env file into the image being built. When you encounter an error like lstat containers/.env: no such file or directory, it signals that Docker cannot find the source file at the specified path relative to the build context.

The issue is usually one of two things:

  1. Incorrect Relative Path: The path provided to COPY must be relative to where you execute the docker build command, not necessarily relative to the final container structure.
  2. Build Context Misunderstanding: Docker builds an image based on a context (the directory you run the command from). If your source file is outside that context, the COPY operation will fail.

In your specific scenario, where you have a host structure like:

root/
  containers/
    - docker-compose
    - .env

And you are running docker build from the root/ directory, the path /containers/.env inside the Dockerfile assumes a structure that doesn't match where the source file actually resides relative to the build context.

Solution 1: Correcting the Build-Time Copy (For Image Seeding)

If your goal is strictly to bake the environment variables into the image layer (which is generally discouraged for secrets, see Section 3), you need to ensure the path is correct relative to where you execute the build command.

Assuming your docker build context is the root directory containing containers/, and you want to copy .env from that location, you would adjust your Dockerfile as follows:

Revised Dockerfile Snippet:

FROM hitalos/laravel

RUN git config --system http.sslverify false

# Step 1: Clone the repository (assuming this is correct)
RUN git clone repo /var/www

# Step 2: Copy the .env file from the local build context into the image
COPY containers/.env .env 

# Run Composer Install and Artisan commands...
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

By changing the source path to COPY containers/.env .env, you are explicitly telling Docker to look for .env inside the containers directory relative to the build context, which resolves the "no such file or directory" error.

Solution 2: The Best Practice – Runtime Environment Injection (Volume Mounting)

While copying the file during the build works for initial setup, it is a major security risk as the secrets become baked into the image layer. The industry standard and most secure approach—especially when following Laravel conventions—is to handle environment variables at runtime using Docker Volumes. This allows you to keep your sensitive .env files securely on the host machine while injecting them only when the container starts.

This method aligns perfectly with modern deployment practices, which is crucial for maintaining security in any application, whether you are building a system like those promoted by Laravel resources.

Implementing Volume Mounting via docker-compose.yml:

Instead of using COPY in your Dockerfile, remove that line entirely and rely solely on mounting the configuration:

version: '3.7'
services:
  app:
    image: hitalos/laravel
    volumes:
      # Mount the local .env file directly into the container's environment path
      - ./containers/.env:/var/www/.env 

Why this is superior:

  1. Security: The .env file remains on your Ubuntu server, protected by standard file permissions, and is never baked into the immutable image layer.
  2. Flexibility: You can easily change environment variables without rebuilding the entire Docker image.
  3. Laravel Alignment: This approach respects the separation of configuration (the environment) and code (the application files), which is a core principle in managing Laravel projects effectively.

Conclusion

When dealing with containerization, especially involving sensitive data like .env files, always prioritize security and flexibility over simple file copying. While fixing the path for a COPY command solves the immediate Docker error during build time, using volume mounting via docker-compose is the robust, secure, and recommended method for injecting environment variables into your Laravel containers. Embrace these principles to build scalable and secure applications based on solid architectural foundations.