如何在开发中编辑Docker容器中的代码? [英] How to edit code in a Docker container in development?

查看:297
本文介绍了如何在开发中编辑Docker容器中的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的容器中有 / srv 下的所有网站代码。



我的Dockerfile下载代码使用git并使其成为图像的一部分,以便于部署到生产。



但是,如何编辑开发中的代码?我以为使用卷是解决方案,例如: -v / docker / mycontainer / srv:/ srv 。但它会覆盖容器中的目录。如果这是我第一次运行它,它会清空它,因为主机没有任何东西。所以我在Docker文件中做的就是丢失了。



/ srv / myapp 中还有目录和文件我想在我的应用程序的不同版本中共享,例如: / srv / myapp / user-uploads 。这是专业网站开发中的常见做法



我可以做些什么来做这些事情吗?:




  • 在/ srv中编辑代码开发

  • share / srv / myapp / user-uploads跨不同版本

  • 让Dockerfile下载代码。在Docker外面做git clone或git pull将会在我看来会打败Docker的目的。此外,还有一些我无法在主机中运行的东西,如数据库迁移或其他特定于应用程序的脚本。



是否有一种做反向卷的方式?我的意思是使容器覆盖主机,而不是相反。



我想在一个solayon可能会将/ srv复制到/srv.deployment-copy,然后再运行容器的守护进程。然后当我运行守护进程检查/srv.deployment-copy是否存在并将所有内容复制到/ srv。这样我可以使用/ srv作为一个卷,仍然可以使用Dockerfile部署代码。我已经使用所有docker命令的别名,所以自动化这不会是一个问题。你觉得怎么样?

解决方案

我发现在开发中编辑代码的最好方式是像往常一样安装一切(包括克隆你的应用程序的存储库),但是将容器中的所有代码移动到 /srv/myapp.deploy.dev 。然后启动 rw 卷的容器 / srv / myapp 和一个清除该卷的init.d脚本并复制新内容,如下所示:

  rm -r / srv / myapp / * 
rm -r / srv / myapp /.[!.]*
cp -r /srv/myapp.deploy.dev/。 / srv / myapp
rm -r /srv/myapp.deploy.dev


I have all my websites' code under /srv in my containers.

My Dockerfile downloads the code using git and makes it part of the image for easier deployment to production.

But then how do I edit the code in development? I thought using volumes was the solution, eg: -v /docker/mycontainer/srv:/srv. But it overwrites the directory in the container. If it's the first time I run it it empties it because there's nothing in the host. So whatever I did in the Dockerfile was gets lost.

There are also directories and files inside /srv/myapp that I want to be shared across the different versions of my app, eg: /srv/myapp/user-uploads. This is a common practice in professional web development.

So what can I do to be able to do all these things?:

  • edit code in /srv in development
  • share /srv/myapp/user-uploads across different versions
  • let Dockerfile download the code. Doing "git clone" or "git pull" outside of Docker would defeat Docker's purpose in my opinion. Besides there are things that I can't run in the host, like the database migrations or other app-specific scripts.

Is there a way to do a reverse volume mount? I mean make the container overwrite the host, instead of the opposite.

I'm thinking one soluiton might be to copy /srv to /srv.deployment-copy before running the container's daemon. And then when I run the daemon check if /srv.deployment-copy exists and copy everything back to /srv. This way I can use /srv as a volume and still be able to deploy code to it with the Dockerfile. I'm already using aliases for all the docker commands so automating this won't be a problem. What do you think?

解决方案

I found the best way to edit code in development is install everything as usual (including cloning your app's repository), but move all the code in the container to say /srv/myapp.deploy.dev. Then start the container with a rw volume for /srv/myapp, and a init.d script that cleans that volume and copies the new contents inside like this:

rm -r /srv/myapp/*
rm -r /srv/myapp/.[!.]*
cp -r /srv/myapp.deploy.dev/. /srv/myapp
rm -r /srv/myapp.deploy.dev

这篇关于如何在开发中编辑Docker容器中的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆