- 1 year ago
Dockerfile PATH variables
In a Dockerfile, you can set environment variables for the PATH variable by using the ENV
instruction. Here is an example of how you can add a directory to the PATH variable in a Dockerfile:
dockerfileFROM ubuntu:latest
ENV PATH="/opt/myapp/bin:${PATH}"
RUN mkdir -p /opt/myapp/bin && \
echo "Hello, World!" > /opt/myapp/bin/hello
CMD ["/opt/myapp/bin/hello"]
In this example, we start with an Ubuntu base image, and we set the environment variable PATH
to include the directory /opt/myapp/bin
. The ${PATH}
at the end of the line preserves the existing value of the PATH variable, so that we can add our own directory to it.
We then create the directory /opt/myapp/bin
and add a simple script called hello
to it. Finally, we set the default command to run the hello
script.
When this Dockerfile is built and the resulting image is run, the /opt/myapp/bin
directory will be added to the PATH variable, so that the hello
script can be executed without specifying the full path.
Note that the exact syntax for setting environment variables in a Dockerfile may vary depending on the base image you are using and the operating system you are working with. However, the ENV
instruction is a standard way to set environment variables in a Dockerfile.