简体   繁体   English

Docker 卷未安装在容器中

[英]Docker volume not being mounted in container

I'm trying to mount a volume using docker-compose so I can hot reload some C code when developing.我正在尝试使用 docker-compose 安装卷,以便在开发时可以热重载一些 C 代码。 I've used Docker a couple times before and specifically hit this use case while working on a nodejs website but I'm completely at a loss here.我之前使用过 Docker 几次,并且在处理 nodejs 网站时专门遇到了这个用例,但我在这里完全不知所措。

My docker-compose.yml and Dockerfile have been stripped down entirely to just the bare minimum.我的docker-compose.ymlDockerfile已被完全剥离到最低限度。 I would like to mount my current directory (all source code) into the container.我想将当前目录(所有源代码)挂载到容器中。 My Dockerfile just installs some dependencies, sets the working directory and then attempts to run the makefile while my docker-compose.yml adds the volume.我的Dockerfile只是安装了一些依赖项,设置了工作目录,然后尝试运行 makefile 而我的docker-compose.yml添加了卷。 The result is a container that cannot access the mounted volume and resulting code (it's nothing wrong with the Makefile as it works on the host directory and when copied in, instead of using a volume).结果是一个容器无法访问已安装的卷和生成的代码(Makefile 没有问题,因为它在主机目录上工作并且在复制时,而不是使用卷)。 Does anyone see anything wrong with either of these files?有没有人发现这两个文件有什么问题? It appears the /cfs folder isn't even being created in the container.似乎/cfs文件夹甚至没有在容器中创建。 I tried mounting it to the home directory to no avail.我尝试将其安装到主目录无济于事。

docker-compose.yml docker-compose.yml

version: '3'

services:
  cfs:
    volumes:
      - ./:/cfs
    build:
        context: ./
        dockerfile: ./Dockerfile
    networks:
      - default
  
networks:
  default:
    internal: true    

Dockerfile Dockerfile

FROM ubuntu:20.04

# install dependencies
RUN apt-get -qy update                           \
    && apt-get -y install                        \
        cmake=3.16.3-1ubuntu1                    \
        make=4.2.1-1.2                           \
        gcc=4:9.3.0-1ubuntu2                     \
        g++=4:9.3.0-1ubuntu2

WORKDIR /cfs

RUN make prep
RUN make
RUN make install

Most Compose settings aren't visible during an image build.大多数 Compose 设置在映像构建期间不可见。 volumes: aren't mounted, environment: variables aren't set, networks: aren't accessible. volumes:未安装, environment:未设置变量, networks:不可访问。 Only the settings within the immediate build: block have an effect.只有立即build:块中的设置才有效。

That means you should look at the Dockerfile in isolation, ignoring the docker-compose.yml file.这意味着您应该单独查看 Dockerfile,忽略docker-compose.yml文件。 At that point, the /cfs directory is empty (you don't COPY any source code into it), so the RUN make... commands will fail.此时, /cfs目录为空(您无需将任何源代码COPY到其中),因此RUN make...命令将失败。 It doesn't matter that the directory will eventually have something else mounted over it.目录最终会在其上安装其他东西并不重要。

If you're just planning to recompile the application when the source code changes, delete the volumes: , COPY the source into the image, and run docker-compose build at the point you'd typically run make .如果您只是打算在源代码更改时重新编译应用程序,请删除volumes: ,将源代码COPY到映像中,然后在您通常运行make的位置运行docker-compose build If you do have a setup that can rebuild the application when its source changes, you need to set the image's CMD to launch that, but if you don't COPY the source in, you can't build it at image build time.如果您确实有可以在源更改时重建应用程序的设置,则需要设置映像的CMD以启动该应用程序,但如果您不COPY源代码,则无法在映像构建时构建它。 (...and if you're going to overwrite the entire interesting content of the image with a volume, it will get lost anyways.) (......如果你要用一个卷覆盖图像的整个有趣内容,它无论如何都会丢失。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM