Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

valentinmorice's avatar

Laravel Sail .env Variable

I am using a package that requires a file named config.json, which contains an API key. I am working with Laravel Sail and have edited the Dockerfile to echo the json into the file during build time.

RUN echo '{ "api_key": "someKey123" }' > /home/sail/.config/config.json

I would like to store the api key in the .env file that comes with the default Laravel install. I'm defining it in the docker-compose file.

services:
    laravel.test:
        build:
            context: ./docker/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
            API_KEY: '${API_KEY}'  // <------ Here is the variable.

I've tried running this command but it doesn't echo the variable value, instead it writes the string as it is to the config.

RUN echo '{ "api_key": "${API_KEY}" }' > /home/sail/.config/config.json

How can I echo the variable content to the config file? I'm neither a bash nor Docker expert, and I know there are some specifics regarding container builds and variables that i might not understand. Thank you for your help.

0 likes
3 replies
LaryAI's avatar
Level 58

To echo the variable content to the config file, you need to use double quotes instead of single quotes in the Dockerfile. Single quotes do not expand variables, while double quotes do. Here's an example:

RUN echo "{ \"api_key\": \"$API_KEY\" }" > /home/sail/.config/config.json

This will replace $API_KEY with the value of the environment variable during the build process.

valentinmorice's avatar

@LaryAI Unfortunately this reply does not work. The .env variable seem to be empty, as there is nothing outputted to the config.json in place of it.

valentinmorice's avatar
valentinmorice
OP
Best Answer
Level 10

I've found the solution on this stackoverflow thread:

https://stackoverflow.com/questions/57268016/use-env-variables-in-dockerfile

I declared the variable as an arg in docker-compose.yaml and at the top of the Dockerfile

// Dockerfile
ARG API_KEY
services:
    laravel.test:
        build:
            context: ./docker/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
				API_KEY: '${API_KEY}'  // <------ Here is the variable.
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'

Also used the AI response to actually echo out the token:

RUN echo "{ \"api_key\": \"$API_KEY\" }" > /home/sail/.config/config.json

Please or to participate in this conversation.