'How to set WORKDIR in dockerfile from environment ARG?
I want to replace WORKDIR /opt/app with a global ARG, but it does not work:
ARG BUILD_IMAGE=maven:3.8.4-eclipse-temurin-11
ARG APP=/opt/app
FROM $BUILD_IMAGE as dependencies
#WORKDIR /opt/app #this would pass!
WORKDIR ${APP}
Result:
Error response from daemon: cannot normalize nothing
Failed to deploy Dockerfile': Can't retrieve image ID from build stream
Why?
Solution 1:[1]
ARGs are scoped to the stage, or the from lines when defined at the top of the file. So define your ARG after the FROM line, preferably just before you use it (for cache efficiency).
# define top level args used in FROM lines here
ARG BUILD_IMAGE=maven:3.8.4-eclipse-temurin-11
FROM $BUILD_IMAGE as dependencies
# define your args scoped within the stage after the FROM for that stage
ARG APP=/opt/app
WORKDIR ${APP}
If you want to set the arg value once and reuse it, that looks like:
# define top level args used in FROM lines here
ARG BUILD_IMAGE=maven:3.8.4-eclipse-temurin-11
# the top section can also define the default value of an arg that is defined elsewhere
ARG APP=/opt/app
FROM $BUILD_IMAGE as dependencies
# define your args scoped within the stage after the FROM for that stage
ARG APP
WORKDIR ${APP}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
