[英]docker-compose same config for dev and production but enable code sharing between host and container only in development
As the most important benefit of using docker is to keep dev and prod env to be the same so let's rule out the option of using two different docker-compose.yml
由于使用docker的最重要好处是使dev和prod env保持相同,因此我们排除使用两个不同
docker-compose.yml
的选项
Let's say we have a Django application, and we use gunicorn
to serve for production and we have a dedicated apache2 as a reverse proxy(this apache2 is out of docker by design). 假设我们有一个Django应用程序,我们使用
gunicorn
进行生产,并且有一个专用的apache2作为反向代理(该apache2在设计上超出了docker)。 So this application(docker-compose) has only two parts, web
(Django) and db
(mysql). 因此,此应用程序(docker-compose)只有两部分,即
web
(Django)和db
(mysql)。 There's nothing wrong with the db part. 数据库部分没有任何问题。
For the Django part, the dev routine without docker would be using venv and python3 manage.py runserver
or whatever shortcut that an IDE provides. 对于Django部分,没有docker的dev例程将使用venv和
python3 manage.py runserver
或IDE提供的任何快捷方式。 We can happily change our code, the dev server is smart to pick up and change and reflect in no time. 我们可以愉快地更改我们的代码,开发服务器很聪明,可以立即进行更改和反映。
Things get tricky when docker comes in since all source code should be packed into the image, this gives our dev a big overhead of recreating the image&container again and again. 当docker进入时,事情变得棘手,因为所有源代码都应打包到映像中,这给我们的开发人员带来了很大的开销,需要一次又一次地重新创建映像和容器。 One might have the following solutions(which I found not elegant):
一个人可能有以下解决方案(我发现这不是很优雅):
In docker-compose.yml
use volume to mount source code folder into the container, so that all changes in the host source code folder will automatically reflect in the container, then gunicorn
will pick up the change and reflect. 在
docker-compose.yml
使用volume将源代码文件夹安装到容器中,以便主机源代码文件夹中的所有更改将自动反映在容器中,然后gunicorn
将拾取更改并进行反映。 --- This does remove most of the recreating container overhead, but we can't use the same docker-compose.yml
in production as this introduces a dependency to the source code on the host server. ---这确实消除了大多数重新创建容器的开销,但是我们不能在生产中使用相同
docker-compose.yml
,因为这会引入对主机服务器源代码的依赖性。
I know there is a command line option to mount a host folder to the container, but to my knowledge, this option only exists in docker run
not docker-compose
. 我知道有一个命令行选项可将主机文件夹安装到容器,但据我所知,此选项仅存在于
docker run
而不存在于docker-compose
。 So using a different command to bring the service up in different env is another dead end. 因此,使用不同的命令在不同的环境中启动服务是另一个死角。 ( I am not 100% sure about this as I'm still quite new to docker, please correct me if I'm wrong )
( 我对此不是100%的确定,因为我对docker还是很陌生,如果我错了,请纠正我 )
TLDR; TLDR; How can I set up my env so that
我该如何设置我的环境
docker-compose.yml
for both dev and prod docker-compose.yml
Thanks a lot! 非常感谢!
I have also liked to jam as much functionality into a single docker-compose.yml file. 我还喜欢将尽可能多的功能添加到单个docker-compose.yml文件中。 A few strategies I would consider:
我会考虑一些策略:
define different services for prod and dev. 为产品和开发人员定义不同的服务。 So you'll run
docker-compose up dev
or docker-compose up prod
or docker-compose run dev
. 因此,您将运行
docker-compose up dev
或docker-compose up prod
或docker-compose run dev
。 There is some copying here but usually not a lot. 这里有一些复制品,但通常不是很多。
Use multiple docker-compose.yml files and merge them. 使用多个docker-compose.yml文件并将其合并。 eg:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
. 例如:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
。 More details here: https://docs.docker.com/compose/extends/ 此处有更多详细信息: https : //docs.docker.com/compose/extends/
I usually just comment out my volumes section, but that's probably not the best solution. 我通常只是注释掉我的卷部分,但这可能不是最好的解决方案。
Define your django service in docker-compose.yml
as 在
docker-compose.yml
中将django服务定义为
services:
backend:
image: backend
Then add a file for dev: docker-compose.dev.yml
然后为dev添加文件:
docker-compose.dev.yml
services:
backend:
extends:
file: docker-compose.yml
service: backend
volume: local_path:path
To launch for prod, just docker-compose up
要启动产品,只需
docker-compose up
To launch for dev docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
为dev
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
To hot reload dev django app, just reload gunicorn ps aux | grep gunicorn | grep greencar_proj | awk '{ print $2 }' | xargs kill -HUP
要热重载dev django应用,只需重载gunicorn
ps aux | grep gunicorn | grep greencar_proj | awk '{ print $2 }' | xargs kill -HUP
ps aux | grep gunicorn | grep greencar_proj | awk '{ print $2 }' | xargs kill -HUP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.