0

How to pass environment variables to RUN command in Dockerfile? In my scenario, I want to pass env variables to RUN command which runs a script & uses these variables?

.env

NAME=John

script.sh

#!/bin/sh

echo $NAME

Dockerfile

FROM alpine:3.14

COPY . .

RUN chmod +x script.sh

RUN ./script.sh

docker-compose.yml

version: "3.1"

services:
  foo:
    container_name: foo
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped

How can I pass the NAME env variable to the last RUN command in Dockerfile (to be used by the script executable)?

I am aware of --build-arg but it is inconvenient when there are 100s of env variables. Even then how can I format the docker compose command to read all arguments from an env file & pass them as build arguments?

Aditya Mishra
  • 1,687
  • 2
  • 15
  • 24
  • version: 3.1? Really?.. If you're writing new docker system, consider upgrading to latest (3.9 as of now). – STerliakov Oct 10 '22 at 15:45
  • Does this answer your question? [How to define build-args in docker-compose?](https://stackoverflow.com/questions/50734271/how-to-define-build-args-in-docker-compose) – STerliakov Oct 10 '22 at 15:46

0 Answers0