简体   繁体   English

Docker在运行之前组合卷安装

[英]Docker-compose volume mount before run

I have a Dockerfile I'm pointing at from a docker-compose.yml. 我有一个Dockerfile,我指的是docker-compose.yml。

I'd like the volume mount in the docker-compose.yml to happen before the RUN in the Dockerfile. 我希望docker-compose.yml中的卷挂载发生在Dockerfile中的RUN之前。

Dockerfile: Dockerfile:

FROM node

WORKDIR /usr/src/app

RUN npm install --global gulp-cli \
 && npm install

ENTRYPOINT gulp watch

docker-compose.yml 泊坞窗,compose.yml

version: '2'

services:
  build_tools:
    build: docker/gulp
    volumes_from:
      - build_data:rw

  build_data:
    image: debian:jessie
    volumes:
      - .:/usr/src/app

It makes complete sense for it to do the Dockerfile first, then mount from docker-compose, however is there a way to get around it. 首先完成Dockerfile,然后从docker-compose安装它是完全合理的,但有没有办法解决它。

I want to keep the Dockerfile generic, while passing more specific bits in from compose. 我想保持Dockerfile的通用性,同时从compose中传递更多特定的位。 Perhaps that's not the best practice? 也许这不是最好的做法?

Erik Dannenberg's is correct, the volume layering means that what I was trying to do makes no sense. Erik Dannenberg是正确的,音量分层意味着我试图做的事情毫无意义。 (There is another really good explaination on the Docker website if you want to read more). (如果您想阅读更多内容, Docker网站上还有另一个非常好的解释)。 If I want to have Docker do the npm install then I could do it like this: 如果我想让Docker执行npm install那么我可以这样做:

FROM node

ADD . /usr/src/app
WORKDIR /usr/src/app

RUN npm install --global gulp-cli \
 && npm install

CMD ["gulp", "watch"]

However, this isn't appropriate as a solution for my situation. 但是,这不适合作为我的情况的解决方案。 The goal is to use NPM to install project dependencies, then run gulp to build my project. 目标是使用NPM来安装项目依赖项,然后运行gulp来构建我的项目。 This means I need read and write access to the project folder and it needs to persist after the container is gone. 这意味着我需要对项目文件夹进行读写访问, 并且在容器消失后需要保留。


I need to do two things after the volume is mounted, so I came up with the following solution... 安装卷后我需要做两件事,所以我想出了以下解决方案......

docker/gulp/Dockerfile: 泊坞窗/一饮而尽/ Dockerfile:

FROM node

RUN npm install --global gulp-cli

ADD start-gulp.sh .

CMD ./start-gulp.sh

docker/gulp/start-gulp.sh: 泊坞窗/一饮而尽/ start-gulp.sh:

#!/usr/bin/env bash

until cd /usr/src/app && npm install
do
    echo "Retrying npm install"
done
gulp watch

docker-compose.yml: 泊坞窗,compose.yml:

version: '2'

services:
  build_tools:
    build: docker/gulp
    volumes_from:
      - build_data:rw

  build_data:
    image: debian:jessie
    volumes:
      - .:/usr/src/app

So now the container starts a bash script that will continuously loop until it can get into the directory and run npm install . 所以现在容器启动一个bash脚本,它将不断循环,直到它可以进入目录运行npm install This is still quite brittle, but it works. 这仍然非常脆弱,但它确实有效。 :) :)

You can't mount host folders or volumes during a Docker build. 在Docker构建期间无法装入主机文件夹或卷。 Allowing that would compromise build repeatability. 允许这会损害构建的可重复性。 The only way to access local data during a Docker build is the build context, which is everything in the PATH or URL you passed to the build command. 在Docker构建期间访问本地数据的唯一方法是构建上下文,它是传递给build命令的PATHURL中的所有内容。 Note that the Dockerfile needs to exist somewhere in context. 请注意, Dockerfile需要存在于上下文中的某个位置。 See https://docs.docker.com/engine/reference/commandline/build/ for more details. 有关详细信息,请参阅https://docs.docker.com/engine/reference/commandline/build/

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

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