Skip to content

Instantly share code, notes, and snippets.

@Wapiti08
Last active June 12, 2020 02:34
Show Gist options
  • Save Wapiti08/43168ba00f6275deafbb7def4865b831 to your computer and use it in GitHub Desktop.
Save Wapiti08/43168ba00f6275deafbb7def4865b831 to your computer and use it in GitHub Desktop.
The trick to read env in docker image

it is possible to map the .env environment variables to ARGS to be used by the Dockerfile during build.

docker-compose.yml

version: "x"
services:

  xxx:
    build:
    # there is the space between context and .
      context: .
      dockerfile: ./Dockerfile
      args:
        - PORT=${PORT}
    volumes:
       ...

Dockerfile

FROM xx
ARG PORT

ENV PORT "$PORT"

EXPOSE ${PORT}
...

In the root folder, you can define the ARG in .env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment