Skip to content

How to set and use variable in docker compose

Jack edited this page Mar 21, 2022 · 4 revisions

Docker-compose with .env (i.e. Linux shell mechanism)

It follows the Linux shell mechanism, like that export variable

A. How to import/load variables via file

1. docker-compose offer a option to import environment file

read a manual

# docker-compose --help
--env-file string            Specify an alternate environment file.

2. docker-compose read .env as default

Load .env file, if it is on directory that you command docker-compose`s CLI

3. Linux shell mechanism export

export VARIABLE=VALUE

B. How to write, .env file

VARIABLE=VALUE

C. How to call variables from .env file

Is is shell mechanism for using variable

#docker-compose.yaml
version: '3'
services:
  your-service:
    image: your-imange:${VARIABLE}