Docker compose docker compose绑定装载的目录对Dockerfile似乎不可见

Docker compose docker compose绑定装载的目录对Dockerfile似乎不可见,docker-compose,elixir,dockerfile,phoenix-framework,Docker Compose,Elixir,Dockerfile,Phoenix Framework,我有一个docker-compose.yml文件和一个Dockerfile。我增加了一个绑定坐骑。如果我不尝试编译或运行任何mix命令,让docker compose“启动”并进入一个交互式shell。我实际上可以看到“绑定”卷和所有文件 问题是,当我尝试在该目录中cd或运行命令时,它就好像不存在一样,并且它“返回一个带有1的退出代码”错误 docker-compose.yml # Version of docker-compose version: '3.7'

我有一个docker-compose.yml文件和一个Dockerfile。我增加了一个绑定坐骑。如果我不尝试编译或运行任何mix命令,让docker compose“启动”并进入一个交互式shell。我实际上可以看到“绑定”卷和所有文件

问题是,当我尝试在该目录中cd或运行命令时,它就好像不存在一样,并且它“返回一个带有1的退出代码”错误

docker-compose.yml

        # Version of docker-compose
    version: '3.7'

    # Containers we are going to run
    services:
      # Our Phoenix container
      phoenix:
        # The build parameters for this container.
        build:
          # Here we define that it should build from the current directory
          context: .
        environment:
          # Variables to connect to our Postgres server
          PGUSER: gametime_dev
          PGPASSWORD: gametime-dev
          PGDATABASE: gametime_dev
          PGPORT: 5432
          # Hostname of our Postgres container
          PGHOST: db
        ports:
          # Mapping the port to make the Phoenix app accessible outside of the container
          - "4000:4000"
        depends_on:
          # The db container needs to be started before we start this container
          - db
          - redis
        volumes:
          - type: bind
            source: .
            target: /opt/gametime
      redis:
        image: "redis:alpine"
        ports:
          - "6379:6379"
        sysctls:
          net.core.somaxconn: 1024

      db:
        # We use the predefined Postgres image
        image: kartoza/postgis:11.0-2.5
        environment:
          # Set user/password for Postgres
          POSTGRES_USER: gametime_dev
          POSTGRES_PASS: gametime-dev
          # Set a path where Postgres should store the data
          PGDATA: /var/lib/postgresql/data/pgdata
        restart: always
        volumes:
          - pgdata:/usr/local/var/postgres_data
    # Define the volumes
    volumes:
      pgdata:
Dockerfile:

        # Latest version of Erlang-based Elixir installation: https://hub.docker.com/_/elixir/
    FROM bitwalker/alpine-elixir-phoenix as build
                                                                                                                                                                                  RUN apk update && \
      apk add postgresql-client
                                                                                                                                                                                  # Create and set home directory
    ENV HOME /opt/gametime                                                                                                                                                        WORKDIR $HOME
                                                                                                                                                                                  # Configure required environment
    ENV MIX_ENV dev                                                                                                                                                               # Set and expose PORT environmental variable
                                                                                                                                                                                  ENV PORT ${PORT:-4000}
    EXPOSE $PORT
    VOLUME /opt/gametime

                                                                                                                                                                                  # Install hex (Elixir package manager)
    RUN mix local.hex --force
    # Install rebar (Erlang build tool)                                                                                                                                           RUN mix local.rebar --force
                                                                                                                                                                                  # Copy all dependencies files
    # not commenting this out defeats the purpose of needing the volume
    # COPY mix.* ./
    # Install all production dependencies                                                                                                                                         RUN mix deps.get --only dev
                                                                                                                                                                                  # Compile all dependencies
    #THIS FAILS - BECAUSE IT CAN'T FIND THE mix.exs file
    RUN mix deps.compile
    # Copy all application files                                                                                                                                                  # COPY . .

    # Compile the entire project                                                                                                                                                  
    RUN mix compile
    # IF I COMMENT OUT THE ABOVE THIS ALSO FAILS BECAUSE IT CAN'T FIND ASSETS DIRECTORY                                                                                                                                                                   
    RUN cd assets && npm install

    CMD ["./entrypoint.sh"]
VOLUME /opt/gametime_app
错误:

    Step 11/15 : RUN mix deps.get --only dev
 ---> Running in 831e2e0d3fe2
** (Mix) Could not find a Mix.Project, please ensure you are running 
Mix in a directory with a mix.exs file
ERROR: Service 'phoenix' failed to build: The command '/bin/sh -c mix 
deps.get --only dev' returned a non-zero code: 1
我试图做的是共享应用程序目录,并在容器中使用elixir/erlang/OTP libs来构建和运行代码。这样我就有了一个开发环境,我所做的任何更改基本上都保存在本地机器上

为了公平起见,我可以从github中提取数据,然后在杀死容器之前提交更改。但我想先试试这个

[更新]:所以我发现如果我在
entrypoiont.sh
文件中运行mix命令,一切都会正常工作。我不知道为什么该卷在Dockerfile中不可用,因此没有回答我自己的问题。我从他那里得到了线索

我想这样做的原因是将
deps.get
deps.compile
步骤缓存为一个层,这样我就不必每次运行
docker compose up

时都这样做了,因为您使用的是“/opt/gametime\u app”和“/opt/gametime”,但我看不到第二个步骤是在哪里创建的。可能是打字错误吗

另外,最好在dockerfile中将/opt/gametime_应用程序声明为卷:

        # Latest version of Erlang-based Elixir installation: https://hub.docker.com/_/elixir/
    FROM bitwalker/alpine-elixir-phoenix as build
                                                                                                                                                                                  RUN apk update && \
      apk add postgresql-client
                                                                                                                                                                                  # Create and set home directory
    ENV HOME /opt/gametime                                                                                                                                                        WORKDIR $HOME
                                                                                                                                                                                  # Configure required environment
    ENV MIX_ENV dev                                                                                                                                                               # Set and expose PORT environmental variable
                                                                                                                                                                                  ENV PORT ${PORT:-4000}
    EXPOSE $PORT
    VOLUME /opt/gametime

                                                                                                                                                                                  # Install hex (Elixir package manager)
    RUN mix local.hex --force
    # Install rebar (Erlang build tool)                                                                                                                                           RUN mix local.rebar --force
                                                                                                                                                                                  # Copy all dependencies files
    # not commenting this out defeats the purpose of needing the volume
    # COPY mix.* ./
    # Install all production dependencies                                                                                                                                         RUN mix deps.get --only dev
                                                                                                                                                                                  # Compile all dependencies
    #THIS FAILS - BECAUSE IT CAN'T FIND THE mix.exs file
    RUN mix deps.compile
    # Copy all application files                                                                                                                                                  # COPY . .

    # Compile the entire project                                                                                                                                                  
    RUN mix compile
    # IF I COMMENT OUT THE ABOVE THIS ALSO FAILS BECAUSE IT CAN'T FIND ASSETS DIRECTORY                                                                                                                                                                   
    RUN cd assets && npm install

    CMD ["./entrypoint.sh"]
VOLUME /opt/gametime_app
这将在生成时创建文件夹,并为其提供正确的所有者和权限

至于找不到的文件:
它不起作用,因为卷安装在容器上。它们在构建时不可用。因此,在构建时,卷文件夹是空的。您应该在entrypoint脚本中移动所有编译等。这样,它将在容器启动且卷可用时运行。

是的,不清楚。。我在尝试调试时更改了名称。不过,还是要尝试将
卷添加到Dockerfile中-谢谢。如果那样行的话,我会认为你的答案是正确的。再次感谢您的回答。**因此,在尝试您建议的解决方案后-在运行Dockerfile时仍然会得到丢失的文件/文件夹<代码>(Mix)找不到Mix。Project,请确保您在一个目录中运行Mix,其中包含Mix.exs文件错误:服务“phoenix”无法生成:命令“/bin/sh-c Mix deps.get--only dev”返回了一个非零代码:1
我更了解您的设置。它不起作用,因为卷安装在容器上。它们在构建时不可用。因此,在构建时,卷文件夹是空的。您应该在entrypoint脚本中移动所有编译等。这样,当容器启动并且容量可用时,它就会运行。令人惊讶的是,你也意识到了这一点,并更新了问题,然后你回答了问题,哈哈。请你把这个作为答案输入,我会把它标记为正确的?谢谢你的帮助!