简体   繁体   English

使用Docker容器设置开发环境时如何管理开发任务?

[英]How do you manage development tasks when setting up your development environment using docker containers?

I have been researching docker and understand almost everything I have read so far. 我一直在研究docker,并了解到目前为止我已经阅读的几乎所有内容。 I have built a few images, linked containers together, mounted volumes, and even got a sample django app running. 我构建了一些图像,将容器链接在一起,装入了卷,甚至运行了一个示例django应用程序。

The one thing I can not wrap my head around is setting up a development environment. 我不能束手无策的一件事就是建立一个开发环境。 The whole point of docker is to be able to take your environment anywhere so that everything you do is portable and consistent. docker的全部重点是能够将您的环境带到任何地方,以便您所做的一切都可移植且一致。 If I am running a django app in production being served by gunicorn for example, I need to restart the server in order for my code changes to take affect; 例如,如果我在生产中运行由gunicorn提供服务的django应用程序,则需要重新启动服务器以使我的代码更改生效。 this is not ideal when you are working on your project in your local laptop environment. 当您在本地笔记本电脑环境中处理项目时,这不是理想的选择。 If I make a change to my models or views I don't want to have to attach to the container, stop gunicorn, and then restart it every time I make a code change. 如果我对模型或视图进行了更改,则不需要附加到容器上,请停止gunicorn,然后在每次更改代码时重新启动它。

I am also not sure how I would run management commands. 我也不确定如何运行管理命令。 python manage.py syncdb would require me to get inside of the container and run commands. python manage.py syncdb将需要我进入容器并运行命令。 I also use south to manage data and schema migrations python manage.py migrate . 我还使用south来管理python manage.py migrate migration的数据和架构python manage.py migrate How are others dealing with this issue? 其他人如何处理这个问题?

Debugging is another issue. 调试是另一个问题。 Would I have to somehow get all my logs to save somewhere so I can look at things? 我是否必须以某种方式将所有日志保存到某个地方,以便我可以查看内容? I usually just look at the django development server's output to see errors and prints. 我通常只看django开发服务器的输出以查看错误和打印。

It seems that I would have to make a special dev-environment container that had a bunch of workarounds; 看来我必须制造一个特殊的开发环境容器,该容器具有很多解决方法; that seems like it completely defeats the purpose of the tool in the first place though. 首先,这似乎完全破坏了该工具的目的。 Any suggestions? 有什么建议么?

Update after doing more research: 做更多研究后更新:

Thanks for the responses. 感谢您的答复。 They set me on the right path. 他们使我走上了正确的道路。

I ended up discovering fig http://www.fig.sh/ It let's you orchestrate the linking and mounting of volumes, you can run commands. 我最终发现了无花果http://www.fig.sh/。它可以让您协调卷的链接和安装,您可以运行命令。 fig run container_name python manage.py syncdb . 无花果运行container_name python manage.py syncdb。 It seems pretty nice and I have been able to set up my dev environment using it. 看起来非常不错,我已经能够使用它建立我的开发环境。

Made a diagram of how I set it up using vagrant https://www.vagrantup.com/ . 绘制了如何使用流浪汉https://www.vagrantup.com/进行设置的图表。 在此处输入图片说明

I just run 我刚跑

fig up

in the same directory as my fig.yml file and it does everything needed to link the containers and start the server. 与我的fig.yml文件位于同一目录中,它完成了链接容器和启动服务器所需的所有操作。 I am just running the development server when working on my mac so that it restarts when I change python code. 在Mac上工作时,我只是在运行开发服务器,因此当我更改python代码时它会重新启动。

At my current gig we setup a bash script called django_admin. 在当前的演出中,我们设置了一个名为django_admin的bash脚本。 You run it like so: 您可以这样运行它:

django_admin <management command>

Example: 例:

django_admin syncdb

The script looks something like this: 该脚本如下所示:

docker run -it --rm  \
-e PYTHONPATH=/var/local \
-e DJANGO_ENVIRON=LOCAL \
-e LC_ALL=en_US.UTF-8 \
-e LANG=en_US.UTF-8 \
-v /src/www/run:/var/log \
-v /src/www:/var/local \
--link mysql:db \
localhost:5000/www:dev /var/local/config/local/django-admin $@

I'm guessing you could also hook something up like this to manage.py 我猜你也可以像这样来管理

I normally wrap my actual CMD in a script that launches a bash shell. 我通常将我的实际CMD包装在启动bash shell的脚本中。 Take a look at Docker-Jetty container as an example. Docker-Jetty容器为例。 The final two lines in the script are: 脚本中的最后两行是:

/opt/jetty/bin/jetty.sh restart
bash

This will start jetty and then open a shell. 这将开始码头,然后打开一个壳。

Now I can use the following command to enter a shell inside the container and run any commands or look at logs. 现在,我可以使用以下命令在容器内输入外壳,然后运行任何命令或查看日志。 Once I am done I can use Ctrl-p + Ctrl-q to detach from the container. 完成后,可以使用Ctrl-p + Ctrl-q从容器中分离。

docker attach CONTAINER_NAME

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM