运行前 Docker-compose 卷挂载 [英] Docker-compose volume mount before run

查看:105
本文介绍了运行前 Docker-compose 卷挂载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 docker-compose.yml 指向一个 Dockerfile.

I have a Dockerfile I'm pointing at from a docker-compose.yml.

我希望 docker-compose.yml 中的卷挂载发生在 Dockerfile 中的 RUN 之前.

I'd like the volume mount in the docker-compose.yml to happen before the RUN in the Dockerfile.

Dockerfile:

Dockerfile:

FROM node

WORKDIR /usr/src/app

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

ENTRYPOINT gulp watch

docker-compose.yml

docker-compose.yml

version: '2'

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

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

首先执行 Dockerfile,然后从 docker-compose 挂载是完全有意义的,但是有没有办法绕过它.

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 的通用性,同时从 compose 中传递更具体的位.也许这不是最佳做法?

I want to keep the Dockerfile generic, while passing more specific bits in from compose. Perhaps that's not the best practice?

推荐答案

Erik Dannenberg 的是正确的,音量分层意味着我试图做的事情毫无意义.(如果您想阅读更多内容,Docker 网站上还有另一个非常好的解释).如果我想让 Docker 执行 npm install 那么我可以这样做:

Erik Dannenberg's is correct, the volume layering means that what I was trying to do makes no sense. (There is another really good explaination on the Docker website if you want to read more). If I want to have Docker do the npm install then I could do it like this:

FROM node

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

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

CMD ["gulp", "watch"]

但是,这不适合作为我的情况的解决方案.目标是使用 NPM 安装项目依赖项,然后运行 ​​gulp 来构建我的项目.这意味着我需要对项目文件夹的读写访问权限并且它需要在容器消失后保持不变.

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. 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:

docker/gulp/Dockerfile:

FROM node

RUN npm install --global gulp-cli

ADD start-gulp.sh .

CMD ./start-gulp.sh

docker/gulp/start-gulp.sh:

docker/gulp/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:

docker-compose.yml:

version: '2'

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

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

所以现在容器启动一个 bash 脚本,该脚本将不断循环,直到它可以进入目录运行 npm install.这仍然很脆弱,但它有效.:)

So now the container starts a bash script that will continuously loop until it can get into the directory and run npm install. This is still quite brittle, but it works. :)

这篇关于运行前 Docker-compose 卷挂载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆