2
  1. How to using env variables in Dockerfile?
  2. not args

This is my Dockerfile now:

FROM node:12

I want Dockerfile:

FROM node:$version

How can I do it? If it is not possible, then I will create a feature request for Docker.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    That's exactly what `ARG` is designed for. There is no way to transparently get an environment variable from the host; you must explicitly pass it as a build argument. [Get environment variable value in Dockerfile](https://stackoverflow.com/questions/19537645/get-environment-variable-value-in-dockerfile) says more. – David Maze Aug 10 '20 at 23:17
  • @DavidMaze but I have a prod.env file, how can I pass all the variables from it to all stages of the Dockerfile build? help please = ( – Алексей Соснин Aug 10 '20 at 23:20
  • Usually you'd use an option like `docker run --env-file` to specify those settings at deploy time, and build your image against a fixed set of software. Ideally you run the exact same image in dev, QA, and production (and you wouldn't change the base version of Node between environments). – David Maze Aug 10 '20 at 23:59
  • @DavidMaze no, this is NOT runetimу variables, is variables like graphql endporint, rate limit, e.t.c. FOR WEBPACK. webpack is a ON OF multi part of stages build. This it NOT runtime variable =( its compile html files and script with ENV VALUES. its not runtime =( – Алексей Соснин Aug 11 '20 at 00:40

1 Answers1

0

You need to add as ARGs under a different place from the ENV variables. You can use them in the compose, just a little differently.

ARGs will be used during the build only, if you need them to available for the build & again while running the container for some reason you will have to set both an ARG as well as an ENV variable. {{{During the Build stage they will be called in the exact same manner}}}. ENV are set to replace defaults when running a container, so the build's ENV TZ=Europe/London will be replaced by the Compose's Environment: TZ=America/New_York

services:
  my_node_version:
    image: my/node
    build:
      context: .
      dockerfile: ../node.Dockerfile
      args:
        - VERSION=12
        - NEEDED_IN_BUILD_AND_RUN=SomeStrangeUseCase
    environment:
      - ONLY_USED_IN_IMAGE=MyImageWorks
      - NEEDED_IN_BUILD_AND_RUN=SomeStrangeUseCase
      - OVERRIDE_BUILD_ENV="Will replace the ENV from the Build"

I'm pretty sure that fulfills all the intent from the question (you asked to not use args I assume because you wanted them set in the docker-compose.yaml rather than the dockerfile). You can do this, you just have to set them in a different way.

ENV cannot be used in the build from the Compose (ENV variables set in the Dockerfile just set the defaults that the container will use if none are declared in the OS, the Compose, or anywhere Docker gets them from), so that simply isn't a possibility, so if you need it to be ENV specifically for some reason then the answer is simply "You can't" end of discussion. But I don't see anything that could possibly accomplish, & no reason using ARGs wouldn't accomplish exactly what you want. You just need to place them in a different section, but they should function as needed