最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

docker - Dockerfile setting ENV PATH causing values from host PATH to be used inside container - Stack Overflow

programmeradmin1浏览0评论

I'm somewhat confused by the behavior of the environment variable $PATH inside a Docker container.

  • I am trying to set the $PATH variable to be /kdb/bin, plus some other values.
  • Instead, the value of the $PATH variable appears to be set to the same value as the Docker Container host system

The value of echo $PATH on the host is:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin

(Plus some other stuff which I have omitted for breivty.)

Here is my Dockerfile:

FROM ubuntu:24.04
WORKDIR /kdb
ENV QHOME=/kdb
ENV PATH="${QHOME}/bin:${QHOME}:$PATH"
ENV QLIC=/kdb/kc.lic
COPY ./q q
COPY ./bin bin
COPY ./src src
CMD ["/bin/bash", "-c", "sleep infinity"]

and the docker-compose.yml

services:
    kdb:
        container_name: kdb
        build: .
        command: echo $PATH
        ports:
            - 5000:5000

I have set the default command in the docker-compose.yml to be echo $PATH, such that the value of PATH will be shown in the logs.

docker logs shows that the same value from the host is being used inside the container. The Container PATH variable does not include /kdb/bin or /kdb.

Why is this?

I'm somewhat confused by the behavior of the environment variable $PATH inside a Docker container.

  • I am trying to set the $PATH variable to be /kdb/bin, plus some other values.
  • Instead, the value of the $PATH variable appears to be set to the same value as the Docker Container host system

The value of echo $PATH on the host is:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin

(Plus some other stuff which I have omitted for breivty.)

Here is my Dockerfile:

FROM ubuntu:24.04
WORKDIR /kdb
ENV QHOME=/kdb
ENV PATH="${QHOME}/bin:${QHOME}:$PATH"
ENV QLIC=/kdb/kc.lic
COPY ./q q
COPY ./bin bin
COPY ./src src
CMD ["/bin/bash", "-c", "sleep infinity"]

and the docker-compose.yml

services:
    kdb:
        container_name: kdb
        build: .
        command: echo $PATH
        ports:
            - 5000:5000

I have set the default command in the docker-compose.yml to be echo $PATH, such that the value of PATH will be shown in the logs.

docker logs shows that the same value from the host is being used inside the container. The Container PATH variable does not include /kdb/bin or /kdb.

Why is this?

Share Improve this question asked Jan 19 at 9:36 user2138149user2138149 16.6k30 gold badges145 silver badges286 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Because Compose substitutes environment-variable references before it does its processing. This is a little more commonly used in other parts of the file (for example to substitute the image tag or provide a configurable host port).

The other complication here is that the Compose command: does not automatically run a shell, unlike Dockerfile CMD. You need to provide a shell yourself. So you need to write

command: sh -c 'echo $$PATH'

with an explicit sh -c wrapper, and escaping the dollar sign.


There's an easier way to do this, though, especially while you're debugging. You can docker-compose run a container based off a Compose specification, but replacing the command: or Dockerfile CMD with something you type in at the command line.

docker compose run kdb sh -c 'echo $PATH'

Then you can delete the command: override in the Compose file. Since you can easily enough launch an interactive shell if you need to, also make the Dockerfile CMD run the thing you normally want the container to run, probably something like CMD ["kdb"]; there's no reason to have a container running a meaningless sleep command.

发布评论

评论列表(0)

  1. 暂无评论