使用Docker容器作为Ruby on Rails开发环境

时间:2021-10-21 10:59:03

I want to use Docker as a development environment. I am familiar with the basic Docker concepts such as containers, images, volumes, etc. I am also reading this article.

我想将Docker用作开发环境。我熟悉基本的Docker概念,如容器,图像,卷等。我也在阅读这篇文章。

  1. I think that there are already images specifically created for RoR development. Could someone recommend me a couple of images to start with?

    我认为已经有专门为RoR开发创建的图像。有人可以推荐我几张图片吗?

  2. Suppose that I create a container, mount my working folder (RoR projects). Besides code writing, there are also command line jobs such as Linux tasks (update, install), Rails specific commands (Rake, migrations....). I may need to install new binaries or new gems, change Ruby version using rbenv. How can I accomplish these tasks under Docker? May I type a command in a console or ssh the container?

    假设我创建了一个容器,挂载我的工作文件夹(RoR项目)。除了代码编写之外,还有命令行工作,例如Linux任务(更新,安装),Rails特定命令(Rake,迁移......)。我可能需要安装新的二进制文件或新宝石,使用rbenv更改Ruby版本。如何在Docker下完成这些任务?我可以在控制台中输入命令或者ssh容器吗?

I managed to create an ubuntu container and run it as following:

我设法创建一个ubuntu容器并运行如下:

docker run -it -v /Users/me/Documents/Projects:/var/source_files ubuntu

It creates a console for my container. Next I guess I can run commands like gem install, apt-get update and etc. Is this how we should configure our environment?

它为我的容器创建了一个控制台。接下来我想我可以运行gem install,apt-get update等命令。这是我们应该如何配置我们的环境?

I cannot find information on how to run, how to maintain, how add/remove gems, etc.

我找不到有关如何运行,如何维护,如何添加/删除宝石等信息。

1 个解决方案

#1


1  

It's really up to you and what you're comfortable the most with. I'm assuming solo development on some kind of libraries rather than full-fledged apps[1].

这真的取决于你和你最喜欢的东西。我假设在某种类型的库上进行单独开发,而不是完整的应用程序[1]。

I, for example, tend to use Makefiles when developing on specific Golang projects and have some separate images I tend to use for different occasions. For example, if I have to test a Python / Node scripts, I simply type play and I get into a silly container with a few dependencies pre-installed:

例如,在开发特定的Golang项目时,我倾向于使用Makefile,并且我会在不同的场合使用一些单独的图像。例如,如果我必须测试Python / Node脚本,我只需输入play,然后进入一个预先安装了几个依赖项的愚蠢容器:

In my personal experience, though, I found that shell scripts / aliases work very well across projects, so I tend to have simple aliases that work on most projects. If I were you, for example, I would use a minimalistic approach and alias dev to docker run -ti -v $(pwd):/src $RUBY_IMAGE so that you can then run dev rake test, dev rails server etc etc from any project. Your $RUBY_IMAGE should have a few utilities installed (htop, curl and so on) and you should be good to go.

但是,根据我的个人经验,我发现shell脚本/别名在项目中非常有效,因此我倾向于使用简单的别名来处理大多数项目。例如,如果我是你,我会使用简约方法和别名开发者来停泊运行-ti -v $(pwd):/ src $ RUBY_IMAGE,这样你就可以从任何地方运行dev rake test,dev rails server等项目。您的$ RUBY_IMAGE应该安装了一些实用程序(htop,curl等),你应该很高兴。

Again, I must stress on the fact that it really depends what you're comfortable with -- most of the times I'm extremely productive with just a Makefile.

同样,我必须强调这一事实,这实际上取决于你感到满意 - 大多数时候我只用Makefile非常有效率。

[1] if working on full-fledged apps docker-compose works well for a lot of people and has a very good DX. minikube is a tool I'd recommend you to pick up only if you know how to work with kubernetes. We used docker-compose for a long time but have switched to minikube since a few months as it closely mirrors our production environment, and minikube works better (imho) when you have quite a few services talking to each other.

[1]如果在成熟的应用程序上工作docker-compose适用于很多人,并且具有非常好的DX。 minikube是一个工具,我建议你只有在知道如何使用kubernetes时才能选择。我们使用了docker-compose很长一段时间,但是几个月后我们已经切换到minikube,因为它与我们的生产环境非常接近,当你有很多服务相互交谈时,minikube效果更好(imho)。

#1


1  

It's really up to you and what you're comfortable the most with. I'm assuming solo development on some kind of libraries rather than full-fledged apps[1].

这真的取决于你和你最喜欢的东西。我假设在某种类型的库上进行单独开发,而不是完整的应用程序[1]。

I, for example, tend to use Makefiles when developing on specific Golang projects and have some separate images I tend to use for different occasions. For example, if I have to test a Python / Node scripts, I simply type play and I get into a silly container with a few dependencies pre-installed:

例如,在开发特定的Golang项目时,我倾向于使用Makefile,并且我会在不同的场合使用一些单独的图像。例如,如果我必须测试Python / Node脚本,我只需输入play,然后进入一个预先安装了几个依赖项的愚蠢容器:

In my personal experience, though, I found that shell scripts / aliases work very well across projects, so I tend to have simple aliases that work on most projects. If I were you, for example, I would use a minimalistic approach and alias dev to docker run -ti -v $(pwd):/src $RUBY_IMAGE so that you can then run dev rake test, dev rails server etc etc from any project. Your $RUBY_IMAGE should have a few utilities installed (htop, curl and so on) and you should be good to go.

但是,根据我的个人经验,我发现shell脚本/别名在项目中非常有效,因此我倾向于使用简单的别名来处理大多数项目。例如,如果我是你,我会使用简约方法和别名开发者来停泊运行-ti -v $(pwd):/ src $ RUBY_IMAGE,这样你就可以从任何地方运行dev rake test,dev rails server等项目。您的$ RUBY_IMAGE应该安装了一些实用程序(htop,curl等),你应该很高兴。

Again, I must stress on the fact that it really depends what you're comfortable with -- most of the times I'm extremely productive with just a Makefile.

同样,我必须强调这一事实,这实际上取决于你感到满意 - 大多数时候我只用Makefile非常有效率。

[1] if working on full-fledged apps docker-compose works well for a lot of people and has a very good DX. minikube is a tool I'd recommend you to pick up only if you know how to work with kubernetes. We used docker-compose for a long time but have switched to minikube since a few months as it closely mirrors our production environment, and minikube works better (imho) when you have quite a few services talking to each other.

[1]如果在成熟的应用程序上工作docker-compose适用于很多人,并且具有非常好的DX。 minikube是一个工具,我建议你只有在知道如何使用kubernetes时才能选择。我们使用了docker-compose很长一段时间,但是几个月后我们已经切换到minikube,因为它与我们的生产环境非常接近,当你有很多服务相互交谈时,minikube效果更好(imho)。