docker-compose绑定安装的目录对于Dockerfile似乎不可见

时间:2019-05-07 20:13:08

标签: docker-compose elixir dockerfile phoenix-framework

我有一个docker-compose.yml文件和一个Dockerfile。我添加了绑定绑定。如果我不尝试编译或运行任何mix命令并使docker-compose变为“ up”并输入一个交互式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提取数据-然后在杀死我的容器之前提交更改。但是我想先尝试一下。

[UPDATE]:因此,我发现,如果我在entrypoiont.sh文件中运行mix命令,则一切正常。我不知道为什么该卷在Dockerfile中不可用,因此没有回答我自己的问题。我从here得到了线索。

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

1 个答案:

答案 0 :(得分:0)

您正在使用“ / opt / gametime_app”和“ / opt / gametime”,但我看不到第二个创建位置。可能是错字了吗?

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

VOLUME /opt/gametime_app

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

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