Environment variables don't appear to work in ENTRYPOINT
. It is my understanding that the shell form of ENTRYPOINT
will expand ENV
variables at run time, but this doesn't to appear to work for ENV_CONFIG_INT
in the example below. What have I done wrong in the following example?
Dockerfile
ENTRYPOINT [ "yarn", "run", "app-${ENV_CONFIG_INT}" ]
Compose yaml
test:
image: testimage/test:v1.0
build:
context: .
dockerfile: Dockerfile
env_file:
- ./docker.env
environment:
- ENV_CONFIG_INT=1
Error:
error Command "app-${ENV_CONFIG_INT}" not found.
Replacing the value with a static int of say 1 fixes the issue, however I want the value to be dynamic at runtime.
Thanks in advance.
I wouldn't try to use an environment variable to specify the command to run. Remove the line you show from the Dockerfile, and instead specify the command:
in your docker-compose.yml
file:
test:
image: testimage/test:v1.0
build: .
env_file:
- ./docker.env
command: yarn run app-1 # <--
As you note, the shell form of ENTRYPOINT
(and CMD
and RUN
) will expand environment variables, but you're not using the shell form: you're using the exec form , which doesn't expand variables or handle any other shell constructs. If you remove the JSON-array layout and just specify a flat command, the environment variable will be expanded the way you expect.
# Without JSON layout
CMD yarn run "app-${ENV_CONFIG_INT:-0}"
(I tend to prefer specifying CMD
to ENTRYPOINT
for the main application command. There are two reasons for this: it's easier to override CMD
in a plain docker run
invocation, and there's a useful pattern of using ENTRYPOINT
as a wrapper script that does some initial setup and then runs the CMD
.)
The usual way to ensure that environment variable expansion works in the entrypoint or command of an image is to utilize bash - or sh - as the entrypoint.
version: "3.8"
services:
test:
image: alpine
environment:
FOO: "bar"
entrypoint: ["/bin/sh", "-c", "echo $${FOO}"]
$ docker-compose run test
Creating so_test_run ... done
bar
The other thing you need to do is properly escape the environment variable, so its not expanded on the host system.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.