Logo

dev-resources.site

for different kinds of informations.

docker-compose will always load .env

Published at
5/30/2024
Categories
docker
env
Author
byte4bike
Categories
2 categories in total
docker
open
env
open
Author
9 person written this
byte4bike
open
docker-compose will always load .env

Update 6th-Jun-2024

After deeper dive into the docs and practice. I realize I mixed up .env and file.env. Please refer to -> https://dev.to/tbroyer/comment/2fhdp, that's the right way of understanding this concept.

Screenshot of the issue I am presenting in one glance

As shown in the screenshot, I didn't specify .env under any env_file directives. But it choose to use the PORT value defined under .env instead of public.env or private.env.

Versions

  • docker: 24.0.0
  • docker-compose: 1.29.2

Possible Solution

  • Remove '.env'
  • Never use variable names that are already defined in .env

My Take

I think more people should be aware of this default behavior and I am interested to know how others deal with this issue.

Code Snippet

Checkout the code snippet below if the screenshot is not clear.

# docker-compose.yml
version: "3.8"

services:
  private:
    container_name: private_server
    build: ./backend
    command: uvicorn main:app --host=0.0.0.0 --port=7000
    ports:
      # - 127.0.0.1:${PUBLIC_PORT}:7000
      - 127.0.0.1:${PORT:-7001}:7000
    environment:
      - WORKSPACE=secret
    env_file:
      - private.env

  public:
    container_name: public_server
    build: ./backend
    command: uvicorn main:app --host=0.0.0.0 --port=7000
    ports:
      # - 127.0.0.1:${PRIVATE_PORT}:7000
      - 127.0.0.1:${PORT:-7000}:7000
    environment:
      - WORKSPACE=client
    env_file:
      - public.env
Enter fullscreen mode Exit fullscreen mode

Environment File

# .env
PORT=9879
Enter fullscreen mode Exit fullscreen mode
# public.env
PORT=7000
Enter fullscreen mode Exit fullscreen mode
# private.env
PORT=7001
Enter fullscreen mode Exit fullscreen mode

docker-compose config

services:
  private:
    build:
      context: /home/server/projects/server/backend
    command: uvicorn main:app --host=0.0.0.0 --port=7000
    container_name: private_server
    environment:
      PORT: '7001'
      WORKSPACE: secret
    ports:
    - 127.0.0.1:9879:7000/tcp
  public:
    build:
      context: /home/server/projects/server/backend
    command: uvicorn main:app --host=0.0.0.0 --port=7000
    container_name: public_server
    environment:
      PORT: '7000'
      WORKSPACE: client
    ports:
    - 127.0.0.1:9879:7000/tcp
version: '3.8'
Enter fullscreen mode Exit fullscreen mode
env Article's
30 articles in total
Favicon
How to Use Environment Variables in a React.js App with Vite
Favicon
next-runtime-env usage in Documenso source code
Favicon
How to Build an Elm Land Project for Production
Favicon
การใช้ GitLab สำหรับแชร์ Configuration ให้คนในทีม โดยไม่ใช้แชท
Favicon
How to create a fullstack application using Django and Python Part 5
Favicon
Exploring DotenvX
Favicon
Setting up a React environment
Favicon
Using environment variables in React and Vite
Favicon
Exploring Node.js 20.6: Built-in Support for .env Files
Favicon
docker-compose will always load .env
Favicon
How to fix 'process' is not defined (React+Vite)
Favicon
Virtual Environments in Python Applications
Favicon
Decoding the Matrix: The Evolution of Environment Variables in Software
Favicon
Use Doppler instead of traditional .env files 🍕
Favicon
Além do básico: Uso de Variáveis de Ambiente em Aplicações Node e Nest
Favicon
Running scripts in Production
Favicon
Better DX for .env
Favicon
Navigating Environment Variables in Flutter Projects: Strategies for Effective Integration
Favicon
TIL - Today I Learn 12-11 18-11
Favicon
Generate a Random JWT Secret Key
Favicon
Next JS might be exposing your backend environment variables
Favicon
Environment variables and configuration anti patterns in Node.js applications
Favicon
Node(20.6.0) now supports built-in .env files
Favicon
How to keep your tokens secret?
Favicon
Criando um comando Artisan personalizado para definir valores de variáveis no .env no Laravel
Favicon
How to Effectively Remove .env File from Git Repo/History
Favicon
Vite environment variable's type casting/transforming
Favicon
Did you know docker-compose only takes environment variables from`.env` only?
Favicon
[HUGO]: How to use variables from .env
Favicon
Python – AWS Secrets Manager: Remote env vars

Featured ones: