简体   繁体   English

如何在不重建Docker镜像的情况下更改源代码?

[英]How to change source code without rebuilding image in Docker?

What is the best practice to use Docker container for dev/prod. 使用Docker容器进行开发/生产的最佳实践是什么。

Let's say I want my changes to be applied automatically during development without rebuilding and restarting images. 假设我希望我的更改在开发过程中自动应用,而无需重建和重新启动映像。 As far as I understand I can inject volume for this when running container. 据我了解,我可以在运行容器时为此注入体积。

docker run -v `pwd`/src:/src --rm -it username/node-web-app0

Where pwd/src stands for the directory source code. 其中pwd/src代表目录源代码。 It's working fine so far. 到目前为止,一切正常。

But how to delivery code to production? 但是如何将代码交付生产? I think it worse to keep code along with binaries into the docker container. 我认为将代码和二进制文件一起放入Docker容器更糟。 Do I need to create another similar docker file which will use COPY instead? 我是否需要创建另一个将使用COPY的类似docker文件? Or it's better to deploy source-code separately like for dev-mode and mount it to docker. 或者最好像dev-mode一样分别部署源代码并将其安装到docker。

The best practice is to build a new docker image for every version of your code. 最佳实践是为您的每个版本的代码构建一个新的docker映像。 That has many advantages in production environments as faster deployments, independence from other systems, easier rollbacks, exportability, etc. 这在生产环境中具有许多优势,例如更快的部署,与其他系统的独立性,更容易的回滚,可导出性等。

It is possible to do it within the same Dockerfile, using multi-stage builds . 使用多阶段构建 ,可以在同一个Dockerfile中完成该操作。

The following is a simple example for a NodeJS app: 以下是NodeJS应用程序的简单示例:

FROM node:10 as dev
WORKDIR /src
CMD ["myapp.js"]

FROM node:10
COPY package.json .
RUN npm install
COPY . .

Note that this Dockerfile is only for demo purposes, it can be improved in many ways. 请注意,此Dockerfile仅用于演示目的,可以通过多种方式进行改进。

When working on dev environment use the following commands to build the base image and run your code with a mounted folder: 在开发环境上工作时,请使用以下命令来构建基本映像,并使用已安装的文件夹运行代码:

docker build --target dev -t username/node-web-app0 .
docker run -v `pwd`/src:/src --rm -it username/node-web-app0

And when you're ready for production, just exec docker run without the --target argument to build the full image, that contains the code: 当您准备好进行生产时,只需执行docker docker运行,而无需使用--target参数即可构建完整映像,其中包含以下代码:

docker build -t username/node-web-app0:v0.1 .
docker push username/node-web-app0:v0.1 

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

相关问题 如何更新源代码而不每次都重建图像? - How to update source code without rebuilding image each time? 如何在不重建整个图像的情况下编辑 docker 图像中的文件? - How to edit a file inside a docker image without rebuilding the entire image? 如何在不重建的情况下在 rails docker 图像中安装新宝石 - How to install new gems in a rails docker image without rebuilding it 如何使用 Docker Compose 而不在每次更改时重建图像? - How to use Docker Compose without rebuilding images everytime a change is made? 如何在不重建图像的情况下修改docker健康检查? - How to modify docker health check without rebuilding image? 如何在不重新启动的情况下更改docker容器的源代码? - How to change docker container's source code without restarting it? Docker 容器/映像不会在代码修改时自动重建 - Docker container/image is not rebuilding automatically on code amendment 修改和重建Docker映像 - Modifying and rebuilding a Docker image 如何在不重建的情况下动态更改在 Docker 容器中运行的反应应用程序中的 API URL? - How to dynamically change API URL's in react app which ran in a Docker container without rebuilding? 如何在 Openstack kolla 中更改 docker 图像源? - how to change docker image source in Openstack kolla?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM