如何使用Docker轻松设置Ruby on Rails开发环境

You've likely heard about Docker terms like containers, images, services, volumes, network, Dockerfile, docker-compose file, right? Or you've watched some videos about what it is, but you're not sure how can it apply to your daily life as a developer?

您可能听说过Docker术语,例如容器,图像,服务,卷,网络,Dockerfile,docker-compose文件,对吗? 或者您已经看过一些有关它的视频,但是不确定如何将其应用到您作为开发人员的日常生活中?

That's what I first thought after watching videos about Docker. Not only can Docker run web-apps, servers, and databases, but it can also be a local development environment! I wrote this article not only so you can learn how to do it, but also for me, so I won't forget it. Let's get started!

这是我在观看有关Docker的视频后首先想到的。 Docker不仅可以运行Web应用,服务器和数据库,还可以是本地开发环境! 我写这篇文章不仅是为了使您学习如何做,而且对我来说也是,所以我不会忘记它。 让我们开始吧!

目录 (Table of Contents)

为什么选择Docker? (Why Docker?)

Why use docker? Why not just install it on your local machine, and install Ruby Version Manager (rvm) or Ruby Environment (rbenv)?

为什么要使用docker? 为什么不将其安装在本地计算机上,然后安装Ruby Version Manager(rvm)或Ruby Environment(rbenv)?

Setting up Ruby On Rails with with these tools is great. It took me over 3 hours of installing, troubleshooting, and searching the documentation to get it working. But recently, I got my mac reformatted. But I didn't list or take note of the websites I've visited to make it work on my machine. I forgot how to install it again and it's a pain to repeat the steps.

使用这些工具设置Ruby On Rails很棒。 我花了3个多小时来进行安装,故障排除和搜索文档,以使其正常运行。 但是最近,我重新格式化了Mac。 但是我没有列出或记下我访问过的网站以使其在我的机器上正常工作。 我忘记了如何再次安装,重复这些步骤很痛苦。

This is where Docker shines. Install docker, load your dockerfiles, run a few commands in your terminal, you're already setup! And also, what if you want to uninstall all that you did? It's hard to track what steps to undo. With Docker, it's just a few commands to cleanup.

这就是Docker的亮点。 安装docker,加载dockerfile,在终端中运行一些命令,您已经设置好了! 而且,如果您要卸载所做的一切,该怎么办? 很难跟踪要撤消的步骤。 使用Docker时,只需清理几个命令。

When I was watching an online tutorial about docker, Dan Wahlin, the teacher, said that one of the benefits of docker is to accelerate developer on-boarding. Dan said in his tutorial:

当我观看有关Docker在线教程时 ,老师Dan Wahlin说,Docker的好处之一就是可以加快开发人员的入门速度。 丹在教程中说:

"Docker can help there because we can make one or more images that can then be converted into running containers, and those containers can run on our different developer, and even designer machines."

“ Docker可以为您提供帮助,因为我们可以制作一个或多个映像,然后将其转换为运行中的容器,并且这些容器可以在我们的不同开发人员甚至设计人员的计算机上运行。”

Let's say you have a team of developers, designers, and testers, and have an app with a back-end server, database server, and caching server. You have 12 clean slate machines, with a combination of linux and mac computers. Do you really want to go install, troubleshoot, and follow varying installation instructions that depend on the machine? Then make the pieces work, one-by-one on every machine, without the assurance of they won't encounter any varying errors along the way?

假设您有一个由开发人员,设计人员和测试人员组成的团队,并且拥有一个具有后端服务器,数据库服务器和缓存服务器的应用程序。 您有12台干净的平板电脑,并结合了linux和mac计算机。 您是否真的要去安装,排除故障并遵循取决于机器的各种安装说明? 然后,让这些片段在每台机器上一个接一个地工作,而又不能保证它们不会在途中遇到任何变化的错误吗?

This is why I took time to study Docker. With a few commands and a few lines of a configuration file, you're already setup. In the next section, we'll get our hands dirty with docker setup.

这就是为什么我花时间研究Docker。 使用几条命令和几行配置文件,您已经完成设置。 在下一节中,我们将通过Docker设置来解决问题。

安装Docker并包含Ignorefiles (Installing Docker and including Ignorefiles)

1.安装Docker (1. Install Docker)

I'm not gonna talk much about installing docker, there are many videos out there. But usually, it's just download and open the installer, register an account on dockerhub, and you're good to go. Check out Docker installation docs.

我不会谈论太多关于安装docker的信息,那里有很多视频。 但是通常,只需下载并打开安装程序,在dockerhub上注册一个帐户,就可以了。 查看Docker安装文档

2.制作一个.dockerignore文件 (2. Make a .dockerignore file)

What is a dockerignore file? Dockerignore file just tells Docker which files to ignore in it's container. An example is when you have a minified assets, js, css files, that gets changed from time to time whenever you change the original code. This also applies to gitignore files. Usually, the list of recommended files to ignore are usually found in the internet. You can copy this gist on your own .dockerignore .

什么是dockerignore文件? Dockerignore文件只是告诉Docker容器中要忽略哪些文件。 例如,当您拥有一个精简的资产(js,css文件)时,只要更改原始代码,该资产就会不时更改。 这也适用于gitignore文件。 通常,通常可以在Internet上找到要忽略的推荐文件列表。 您可以自己复制 .dockerignore 要点

And if you use git, put this snippet of code in your .gitignore.

并且,如果您使用git,请将此代码段放入.gitignore

The ignorefiles are a little bit lenghty that's why I just put links.

ignorefiles有点冗长,这就是为什么我只放置链接。

Dockerfile和docker-compose文件 (Dockerfile and docker-compose file)

This is where most of the operation happens. Think of these two files as set of instructions Docker follows on how to setup your virtual container. Dockerfile and docker-compose file works hand in hand. You can have multiple Dockerfiles for different services, and one docker-compose file to tie them up together.

这是大多数操作发生的地方。 可以将这两个文件视为Docker遵循的如何设置虚拟容器的一组指令。 Dockerfile和docker-compose文件协同工作。 您可以为不同的服务使用多个Dockerfile,并可以使用一个docker-compose文件将它们捆绑在一起。

3.创建一个名为Dockerfile的文件 (3. Make a file named Dockerfile )

A Dockerfile is a file with set of rules you'll set that docker will follow. There are pre-built set of rules found on Docker hub. An example is pre-built installation instructions for MySQL, or PHP, or Node.js. After making your Dockerfile, put this code on your Dockerfile. And I'll go through a brief explanation on what does these lines do.

Dockerfile是具有规则集的文件,您可以设置该规则集,以便Docker遵循。 在Docker hub上有一套预先构建的规则。 一个示例是针对MySQL,PHP或Node.js的预构建安装说明。 制作Dockerfile ,将此代码放在Dockerfile上。 我将对这些行的作用进行简要说明。

  • FROM ruby - this means docker will pull a pre-built setup by ruby. You don't need to think about updating or installing on your machine the latest ruby version. You'll see the list of Docker's pre-built images on their Dockerhub. Think of it as like npm.

    FROM ruby这意味着docker将通过ruby提取预构建的设置。 您无需考虑在计算机上更新或安装最新的Ruby版本。 您将在Dockerhub上看到Docker的预构建映像列表 。 认为它像npm。

  • WORKDIR /home/app - Work directory. Work directory means this is be your default folder location when you start your development environment. You can name it whatever you want.

    WORKDIR /home/app工作目录。 工作目录意味着这是您启动开发环境时的默认文件夹位置。 您可以随意命名。

  • ENV PORT 3000 - Environment variable. This will set a variable named $PORT on your bash terminal to be '3000'.

    ENV PORT 3000环境变量。 这将在bash终端上将名为$PORT的变量设置为'3000'。

  • EXPOSE $PORT - expose port 3000 (that we've set earlier) of the virtual container to your local machine.

    EXPOSE $PORT将虚拟容器的端口3000(我们之前设置的)暴露给本地计算机。

  • RUN - Run commands are some setup instructions that you want the terminal to run before you use it. In our case, we installed ruby on rails, bundler, and node.js before we even use the development environment so it's all ready when we use it.

    RUN “运行”命令是您希望终端在使用前运行的一些设置说明。 在我们的案例中,我们在使用开发环境之前就已经在ruby,bundler和node.js上安装了ruby,因此在使用时就已经准备就绪。

  • ENTRYPOINT ["/bin/bash"]- this command tells docker what command to execute when we run the container. In our case, we need to run bash terminal so we can have access to rails.

    ENTRYPOINT ["/bin/bash"] -此命令告诉ENTRYPOINT ["/bin/bash"]我们在运行容器时要执行的命令。 在我们的例子中,我们需要运行bash终端,以便可以访问rails。

Note that these explanations are only brief. You can see more explanations or you can do your deep-dive on Dockerfile reference docs.

注意,这些解释只是简短的。 您可以查看更多说明,也可以在Dockerfile参考文档上进行深入研究

You can do sorts of cool stuff with Dockerfile. In my case, I've tried to install zsh and oh-my-zsh on my container because it has some cool auto-completion features. But in our example we don't really need it, it will only increase our image size, so I didn't include it.

您可以使用Dockerfile做一些很酷的事情。 就我而言,我尝试在容器上安装zsh和oh-my-zsh ,因为它具有一些很酷的自动完成功能。 但是在我们的示例中,我们并不是真的需要它,它只会增加我们的图像大小,因此我没有包含它。

4.制作一个名为docker-compose.yml的文件 (4. Make a file named docker-compose.yml)

Docker compose file is a file that ties up different services together. A good example is when you're wiring up your rails app to different servers like MySQL database server, or redis caching server. You can easily make them work with this file. But for our case, we'll stick to the minimum setup for clarity purposes. A YAML file is a type of markdown file with different rules on how to format your file. Just think of it as a JSON file, without the braces. Put this in your docker-compose.yml file.

Docker compose文件是将不同服务捆绑在一起的文件。 一个很好的例子是将Rails应用程序连接到其他服务器(例如MySQL数据库服务器或Redis缓存服务器)时。 您可以轻松地使它们使用此文件。 但是对于我们的情况,为了清晰起见,我们将坚持最小设置。 YAML文件是一种markdown文件,其格式设置规则不同。 只需将其视为JSON文件即可,无需括号。 将此放入您docker-compose.yml文件。

As you can see, it kinda looks like the Dockerfile, but with a little bit of indentation. Let's go through the lines.

如您所见,它看起来有点像Dockerfile,但有一点缩进。 让我们仔细看看。

  • version - Through time, docker-compose file went through changes. That's why in docker-compose files, they need to specify which version they are using. In our case we just use the latest version as of this time.

    version -经过一段时间,docker-compose文件经历了更改。 这就是为什么在docker-compose文件中,他们需要指定正在使用的版本。 在我们的情况下,我们仅使用最新版本。

  • services - Specify list of services. As I said earlier, you can have many services like a rails server, and a MySQL server on your project. You can name your services any name you want. I named it ruby_dev.

    services -指定服务列表。 如前所述,您的项目上可以有很多服务,例如Rails服务器和MySQL服务器。 您可以将服务命名为任意名称。 我将其命名为ruby_dev

  • build: . - The dot here means a file path where to find the Dockerfile, which is the build instructions.

    build: . -此处的点表示在其中找到Dockerfile的文件路径,这是构建说明。

  • container_name - The name of the container.

    container_name的名称。

  • ports: - these are the ports to expose from the docker container to our host local machine. The pattern here is HOST:CONTAINER. In our case it's "3000:3000". Which means we are letting the default Rails server port (3000) be available in our local machine's "localhost:3000".

    ports: -这些是从docker容器公开到我们的主机本地计算机的端口。 这里的模式是HOST:CONTAINER 。 在我们的例子中是“ 3000:3000”。 这意味着我们将在本地计算机的“ localhost:3000”中提供默认的Rails服务器端口(3000)。

  • volumes: - volume means even if we quit or delete Docker, we can specify which files we can keep in our local machine. We put ./:/home/app there because we named in our Dockerfile earlier the workdir to be /home/app.

    volumes: -卷意味着即使我们退出或删除Docker,我们也可以指定可以保留在本地计算机中的文件。 我们将./:/home/app放在那里,因为我们在Dockerfile中将工作目录命名为/home/app

You can see more explanations on Docker Compose reference docs.

您可以在Docker Compose参考文档中查看更多说明。

构建和运行容器 (Building and running the container)

With all our config files setup, let's build and run the container! After the loads of terms we encountered, building and running the container is way simpler. It will only involve few commands.

使用我们所有的配置文件设置,让我们构建并运行容器! 在我们遇到了很多术语之后,构建和运行容器会变得更加简单。 它只会涉及几个命令。

5.在您的终端中,运行docker-compose build (5. In your terminal, run docker-compose build)

Running this command will get Dockerfile and install all the necessary things to make a rails development environment. Note that the installation may take a while because docker will need to download the necessary packages.

运行此命令将获取Dockerfile并安装所有必要的组件以创建Rails开发环境。 请注意,安装过程可能需要一段时间,因为Docker需要下载必要的软件包。

6.在您的终端中, docker-compose run --rm --service-ports ruby_dev (6. In your terminal, run docker-compose run --rm --service-ports ruby_dev )

This command will start a bash terminal that will be your rails development environment where the rails commands are available. With only these two commands, and two config files, you already have a rails environment without even going through a log of troubleshooting! Notice that our command has some flags, --rm means remove the container after using it, and --service-ports means use port 3000 in our container so we can see our rails server in action. The name ruby_dev also came from services found at our docker-compose.yml.

此命令将启动一个bash终端,该终端将是您的可用rails命令的rails开发环境。 仅使用这两个命令和两个配置文件,您就已经具有Rails环境,甚至无需进行故障排除日志! 注意,我们的命令有一些标志,-- --rm表示使用后删除容器,-- --service-ports表示在容器中使用端口3000,这样我们就可以看到rails服务器正在运行。 名称ruby_dev也来自在docker-compose.yml中找到的服务。

测试运行Rails应用 (Test-run a rails app)

Now that we've successfully made our rails development environment, we'll test a sample rails app.

现在我们已经成功构建了Rails开发环境,我们将测试一个示例Rails应用程序。

1.运行rails new myapp && cd myapp (1. Run rails new myapp && cd myapp)

This command will create a new rails app in a folder named myapp. After that the terminal will go the folder. You can name it whatever you want.

此命令将在名为myapp的文件夹中创建一个新的Rails应用程序。 之后,终端将进入该文件夹。 您可以随意命名。

2.更新并安装gem。 运行bundle update && bundle install (2. Update and install gems. Run bundle update && bundle install)

Just make sure you're in the right folder, in myapp, which contains the rails app files. This command will update and install your dependencies.

只要确保您在myapp的正确文件夹中即可,该文件夹包含rails应用程序文件。 此命令将更新并安装您的依赖项。

3.通过运行rails server -p $PORT -b 0.0.0.0测试服务器 (3. Test the server by running rails server -p $PORT -b 0.0.0.0)

Remember the port we specified in our Dockerfile before? This is where we can use it. In our case, rails will use port 3000 to start the server. Don't forget to put -b 0.0.0.0 because you won't see the app on your local machine without this.

还记得我们之前在Dockerfile中指定的端口吗? 这是我们可以使用它的地方。 在我们的例子中,rails将使用端口3000来启动服务器。 不要忘了放-b 0.0.0.0因为没有它,您将不会在本地计算机上看到该应用程序。

4.通过按键盘上的ctrl-d停止服务器。 (4. Stop the server by pressing ctrl-d on your keyboard.)

打扫干净 (Cleaning Up)

After you're done with everything, you can exit on your container by running exit on your container's bash terminal. The --rm flag you typed before will remove the container, but will you get to keep your ruby on rails files.

完成所有操作后,可以通过在容器的bash终端上运行exit容器。 您之前输入的--rm标志将删除容器,但您将可以将ruby保留在rails文件中。

运行docker-compose down进行清理 (Run docker-compose down to cleanup)

Cleaning up is when you're done with the project, and you want to remove your dev environment so you can save space. If you're really done, you can use this command. Docker will remove all your setup, and the images you downloaded. This so powerful, because, imagine you followed a lot of steps and a lot of installation on your mac. The only way to remove that setup is to uninstall them one by one. With docker on our side, it's just one command. Aww yeah!

完成项目后便需要清理,并且您想要删除开发环境以节省空间。 如果您确实完成了操作,则可以使用此命令。 Docker将删除您的所有设置以及您下载的映像。 如此强大,是因为想象您在Mac上遵循了很多步骤并进行了很多安装。 删除该设置的唯一方法是逐个卸载它们。 在我们身边使用docker时,这只是一个命令。 噢,是的!

结论 (Conclusion)

Glad you made this far! Let's look at the big picture. Setting up a dev environment in Docker can be broken down in 2 steps:

很高兴您能做到这么远! 让我们看一看。 在Docker中设置开发环境可以分为两个步骤:

  1. List the instructions you need on your Dockerfile and docker-compose file.

    在Dockerfile和docker-compose文件上列出所需的指令。
  2. Start or stop or cleanup your dev environment with the docker-compose command.

    使用docker-compose命令启动或停止或清理您的开发环境。

This is a big win for us. You just need to keep the Dockerfile and compose file and whenever you switch machines, you just run two commands! Setup once, and forget.

对我们来说,这是一个巨大的胜利。 您只需要保留Dockerfile和compose文件,每当切换机器时,您只需运行两个命令! 设置一次,然后忘记。

资料库 (Repository)

You can see the repository on how the setup looks like, and additional commands that you need here by checking out the full github repo here.

通过在此处签出完整的github存储库,您可以查看有关设置外观的存储库以及此处所需的其他命令

If you find the article helpful, or if you have some additional questions, throw it in the comments. I'll be glad to help!

如果您认为该文章很有帮助,或者还有其他问题,请在评论中注明。 我很乐意提供帮助!

This article was written by Jonathan Cunanan on freeCodeCamp News.

本文由Jonathan CunananfreeCodeCamp News撰写

🐦 Twitter - 🔥 freeCodeCamp -  👀 Portfolio - ⚛️ Github

🐦Twitter - 🔥freeCodeCamp - 👀投资组合 -⚛️Github

翻译自: https://www.freecodecamp.org/news/painless-rails-development-environment-setup-with-docker/

  • 0
    点赞
  • 1
    收藏
  • 0
    评论
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值