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