[英]How do I build docker images when my development environment mounts my codebase into the container?
I dockerized my app, but in order to see my changes, I would have to rebuild my image and it wouldn't give me a live preview of my app. 我对应用程序进行了docker化处理,但是要查看我的更改,我必须重建图像,并且不能为我提供应用程序的实时预览。
So I created a docker-compose environment which links redis to my a node container, and then mounts the source code from the host to the node app and watches it for changes. 因此,我创建了一个docker-compose环境,该环境将redis链接到我的节点容器,然后将源代码从主机挂载到节点应用程序并监视其更改。 However, since the code isn't part of UnionFS, it can't be containerized. 但是,由于代码不是UnionFS的一部分,因此无法进行容器化。
How do I setup a development environment which can produce dockerized images but doesn't have to be restarted every time I make a change to the codebase? 如何设置一个开发环境,该环境可以生成dockerized映像,但是每次更改代码库时都不必重新启动?
The container image you develop with and mount directories in should be the same image you produce to run the app elsewhere, or at least based the production image if that can't be achieved. 您用来开发和安装目录的容器映像应该与您在其他地方运行应用程序时生成的映像相同,或者如果无法实现,则至少基于生产映像。
Using a simple node app as an example, here is the base image: 以一个简单的节点应用程序为例,以下是基本图像:
FROM node:6
ENV NODE_ENV=production
RUN npm install forever -g
COPY docker-entrypoint.sh /entrypoint.sh
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY . /app
EXPOSE 3000
ENTRYPOINT ["/entrypoint.sh"]
CMD ["node", "/app/index.js"]
Production 生产
docker run \
--detach \
--restart always \
--publish 3000:3000 \
myapp
Development 发展
docker run \
--publish 3000:3000 \
--volume .:/app \
--env NODE_ENV=development \
myapp \
forever -w /app/index.js
So I've modified the mount, but the base image is the same. 所以我修改了挂载,但是基本映像是相同的。 The mounted files replace the "built" files in the container. 挂载的文件替换了容器中的“内置”文件。
In the case of a node.js application, there's a couple of extra development changes. 对于node.js应用程序,还有一些额外的开发更改。 An environment variable and the command used to watch/restart the process on changes. 环境变量和用于监视/重启更改的命令。 You also need to manually exec an npm install
in the container with docker exec $container npm install
when you make changes to the dependencies in package.json
. 您还需要手动exec的一个npm install
在与容器docker exec $container npm install
时,您更改的依赖关系package.json
。
If you require a number of modifications for a development environment and don't want to specify them manually you can create a development image FROM
your base image that houses the development specifics. 如果您需要对开发环境进行大量修改,并且不想手动指定它们,则可以FROM
包含开发细节的基础映像中创建一个开发映像。 Dockerfile.dev
: Dockerfile.dev
:
from myapp:latest
env NODE_ENV=development
volume ["/app"]
command ["forever", "-w", "--watchDirectory", "/app" "/app/index.js"]
Then the dev specifics are stored in the new image but still linked to your real image. 然后,开发细节将存储在新映像中,但仍链接到您的真实映像。
docker build -f Dockerfile.dev -t myapp-dev .
docker run -p 3000:3000 -v .:/app myapp-dev
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.