0

How to set to ENV content from file? Something like this:

COPY file_with_content.txt ./
ENV MODEL=file_with_content.txt
ocean
  • 27
  • 1
  • 6

2 Answers2

1

You can't directly use a Dockerfile ENV to set a variable to the content of a file.

If you are directly using the docker build command, you can pass the file contents as a build-time argument:

ARG MODEL
ENV MODEL=$MODEL # if needed at run time
docker build --build-arg MODEL=$(cat file-with-content.txt) .

This may or may not be supported by higher-level tooling. There's not a way to easily specify this in a docker-compose.yml, for example.

Otherwise, Docker has no built-in support for this, and you can use one of the techniques in Dockerfile - set ENV to result of command to set the environment variable to the file contents. For example, using an entrypoint wrapper:

#!/bin/sh
export MODEL=$(cat file-with-content.txt)
exec "$@"
ENTRYPOINT ["/entrypoint.sh"] # the script above, MUST be JSON-array syntax
CMD the main command as you had it before
David Maze
  • 130,717
  • 29
  • 175
  • 215
-1

Simply you can add inside any services in docker compose yaml file, for ex -

services:

  application:
    image: application
    command: /start.sh
    env_file:
      - .env
    ports:
    - 8000:8000
    build:
      context: ./
      dockerfile: compose/local/application/Dockerfile

For more you can refer from here https://docs.docker.com/compose/environment-variables/

Shubham Agrawal
  • 417
  • 2
  • 5