docker-compose yml 文件中的working_dir 标签是什么意思

问题描述 投票:0回答:2

只是想知道working_dir 文件对于通过 docker-compose 加载的图像到底做了什么。示例 docker-compose.yml 文件如下:

dev:
    extends:
        file: common.yml
        service: workspace
    volumes:
        - $ATOMSPACE_SOURCE_DIR:/atomspace
        - $COGUTILS_SOURCE_DIR:/cogutils
        # Uncomment the following lines if you want to work on moses
        # - $MOSES_SOURCE_DIR:/moses
    working_dir: /opencog # This is the same as the volume mount point below
    links:
        - postgres:db
        - relex:relex

postgres:
    image: opencog/postgres
    # Uncomment the following lines if you want to work on a production
    # system.
    # NOTE: The environment variable `PROD` is set `True` then the entrypoint
    # script in opencog/postgres does additional configurations.
    # environment:
    #     - PROD=True

relex:
    image: opencog/relex
    command: /bin/sh -c "./opencog-server.sh"
docker-compose dockerfile
2个回答
29
投票

working_dir
设置创建的容器的工作目录。它与
--workdir
标志至
docker run
相同。


0
投票

我知道这个问题已经得到解答,但我想在

working_dir
中添加有关
docker-compose.yaml
的重要细节,以及在
WORKDIR
中使用
dockerfile
时遇到的严重问题。

如果您在

working_dir
中为服务设置
docker-compose.yaml
,它不仅会覆盖第一个
dockercompose
WORKDIR
声明,还会覆盖所有 WORKDIR 声明。考虑这个
docker-compose.yaml
服务声明。
  my-demo-svc:
    build:
      dockerfile: ./apps/my-demo-svc/dockerfile
      working_dir: /other-dir # <- Note the /other-dir here.

现在考虑像下面这样的 
Dockerfile

./apps/my-demo-svc/Dockerfile
FROM busybox

WORKDIR /src

# Set the working directory within the project folder
WORKDIR /src/apps

CMD ["sh", "-c", "pwd"]

现在,如果我运行以下 docker-compose 命令,我们认为它会打印出什么?

docker-compose up --force-recreate \ --build my-demo-svc \ my-demo-svc

如果您回答
/src/apps

,您将会感到震惊。它实际上打印出来:

 ✔ Container my-demo-svc Recreated  0.0s 
   Attaching to my-demo-svc
   my-demo-svc  | /other-dir

根据我们的 
pwd

命令,我们处于

/other-dir
现在让我们尝试相同的命令,但这次 

working_dir: /other-dir

中没有 docker-compose.yaml

 ✔ Container my-demo-svc Recreated  0.0s 
   Attaching to my-demo-svc
   my-demo-svc  | /src/apps

这样更好。总而言之,在使用
working_dir
中的

docker-compose.yaml

 属性之前,请先了解其行为。否则你可能会得到一些非常奇怪的结果。

© www.soinside.com 2019 - 2024. All rights reserved.