简体   繁体   English

当我的开发环境将我的代码库安装到容器中时,如何构建docker映像?

[英]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

Development Dockerfile 开发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. 如果您需要对开发环境进行大量修改,并且不想手动指定它们,则可以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.

相关问题 为 Quasar 创建 docker 开发环境时,如何解决“Unknown command 'build'”错误? - How do I resolve the "Unknown command 'build' " error when creating a docker development environment for Quasar? 如何创建资金账户以使用我的本地 Nearup docker 容器进行开发 - how do I create a funded account to use my local nearup docker container for development Docker:如何删除用于构建容器的所有映像? - Docker: How Do I remove all images used to build a container? 如何从docker容器中的react build中提供静态文件 - How do I serve static files from my react build in a docker container Docker 容器找不到我的节点构建目录,我该如何解决? - Docker container can't find my node build directory, how do I fix it? 当我的 dotnet 测试在 Docker 构建中失败时,如何将测试报告从 docker 复制到代理? - How do I copy test report from docker to Agent when my dotnet tests fail in Docker Build? 当 Jenkins 构建时,如何重新启动 Docker 容器? - How can I restart Docker Container when Jenkins do build? 我可以使用Docker隔离我的开发环境吗? - Can I use docker to isolate my development environment? 如何将我的 Docker React 容器配置为仅在需要时安装模块? - How do I configure my Docker React container to only install modules when it needs them? 如何防止对我的 docker 容器的 root 访问 - How do I prevent root access to my docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM