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?

2
  • version: 3.1? Really?.. If you're writing new docker system, consider upgrading to latest (3.9 as of now). Commented Oct 10, 2022 at 15:45
  • Does this answer your question? How to define build-args in docker-compose? Commented Oct 10, 2022 at 15:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.