【问题标题】:Directories for docker-compose bind mount seem invisible to Dockerfiledocker-compose 绑定挂载的目录似乎对 Dockerfile 不可见
【发布时间】:2019-05-10 00:22:31
【问题描述】:

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

问题是当我尝试 cd 或 RUN 该目录中的命令时 - 就好像它不存在并且“返回退出代码为 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"]

错误:

    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 库来构建和运行该代码。这样我就有了一个开发环境,并且我所做的任何更改基本上都保存在我的本地机器上。

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

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

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

【问题讨论】:

    标签: docker-compose elixir dockerfile phoenix-framework


    【解决方案1】:

    您正在使用“/opt/gametime_app”和“/opt/gametime”,但我看不到第二个是在哪里创建的。应该是笔误吧?

    另外,最好将 /opt/gametime_app 声明为 dockerfile 中的卷:

    VOLUME /opt/gametime_app
    

    这将在构建时创建文件夹并为其提供正确的所有者和权限。

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

    【讨论】:

    • 是的,不清楚..我在尝试调试时更改了名称。不过,我会尝试将VOLUME 添加到 Dockerfile - 谢谢。如果可行,我会将您的答案标记为正确。再次感谢您的回答。
    • ** 所以在尝试了您提出的解决方案之后 - 在运行 Dockerfile 时仍然会丢失文件/文件夹...(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
    • 我看你的设置更好。它不起作用,因为卷安装在容器上。它们在构建时不可用。因此,在构建时,卷文件夹为空。您应该在入口点脚本中移动所有编译等。这样它就会在容器启动并且卷可用时运行。
    • 太棒了——实际上也刚刚意识到这一点并更新了问题,然后你回答了这个问题,哈哈。您能否将其作为答案弹出,我会将其标记为正确?感谢您的帮助!
    猜你喜欢
    • 2020-01-28
    • 1970-01-01
    • 2019-10-20
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2021-05-08
    相关资源
    最近更新 更多