当我的开发环境将我的代码库安装到容器中时,如何构建docker图像? [英] How do I build docker images when my development environment mounts my codebase into the container?

查看:177
本文介绍了当我的开发环境将我的代码库安装到容器中时,如何构建docker图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是docked,但为了看到我的变化,我将不得不重建我的图像,它不会给我一个我的应用程序的实时预览。



所以我创建了一个docker-compose环境,将redis链接到我的一个节点容器,然后将源代码从主机安装到节点应用程序,并观察它进行更改。但是,由于该代码不是UnionFS的一部分,因此无法集中化。



如何设置可生成码头化图像的开发环境,但不能必须在每次更改代码库时重新启动?

解决方案

您开发的容器映像和挂载目录应该与您生产的相同的图像在其他地方运行应用程序,或至少在很大程度上相同。



使用简单的节点应用程序作为示例

  from node: 6 
env NODE_ENV = production
运行npm install forever -g
copy docker-entrypoint.sh /entrypoint.sh
copy。 / app
workdir / app
运行npm install
exposed [3000]
entrypoint [/entrypoint.sh]
命令[node, /app/index.js]

生产

  docker run \ 
--detach \
--restart always \
--publish 3000:3000 \
myapp

开发

  docker run \ 
--publish 3000:3000 \
--volume。:/ app \
--env NODE_ENV =开发\
myapp \
forever -w /app/index.js

所以我修改了mount,但是基本的镜像是一样的。装入的文件替换容器中的内置文件。



在节点应用程序的情况下,还有一些额外的开发更改。一个环境变量和用于监视/重新启动更改过程的命令。



开发Dockerfile



如果你需要对开发环境进行许多修改,并且不想手动指定它们,您可以创建开发图像 FROM 您的基本图像,其中包含开发细节。 Dockerfile.dev

  from myapp:latest 
env NODE_ENV = development
volume [/ app]
command [forever,-w,--watchDirectory,/ app/app/index.js]

然后,dev细节存储在新图像中,但仍然链接到您的真实图像。

  docker build -f Dockerfile.dev -t myapp-dev。 
docker run -p 3000:3000 -v。:/ app myapp-dev


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.

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. However, since the code isn't part of UnionFS, it can't be containerized.

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?

解决方案

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 largely the same.

Using a simple node app as an example

from node:6
env NODE_ENV=production
run npm install forever -g
copy docker-entrypoint.sh /entrypoint.sh
copy . /app
workdir /app
run npm install
expose ["3000"]
entrypoint ["/entrypoint.sh"]
command ["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 application, there's a couple of extra development changes. An environment variable and the command used to watch/restart the process on changes.

Development Dockerfile

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

这篇关于当我的开发环境将我的代码库安装到容器中时,如何构建docker图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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