Docker:易于构建,运行,完成!

by Kangze Huang

黄康泽

Docker:易于构建,运行,完成! (Docker: Easy as build, run, done!)

Docker has been getting a lot of buzz recently, and for good reason. The containerization service makes deploying microservices easy and stable, as each service can run an OS in its own virtual environment. That means full compatibility… no more worrying about OS versions, dependencies and differences between your development and production machines! And to top it off, it’s lightweight so you can run multiple Docker containers in the same machine (EC2 instance). Today we will learn how to setup and deploy Docker on Amazon EC2 in 3 easy steps!

Docker最近有很多嗡嗡声,这是有充分理由的。 容器化服务使部署微服务变得容易且稳定,因为每个服务都可以在自己的虚拟环境中运行操作系统。 这意味着完全的兼容性……您再也不用担心操作系统版本,开发机器和生产机器之间的依赖性和差异! 最重要的是,它是轻量级的,因此您可以在同一台机器(EC2实例)上运行多个Docker容器。 今天,我们将通过3个简单的步骤来学习如何在Amazon EC2上设置和部署Docker!

Before we start, take a look at this high-level diagram (courtesy of infoworld.com).

在开始之前,请看一下这个高级图表(由infoworld.com提供 )。

On the left side is your traditional virtual machine setup using a hypervisor. A hypervisor is simply your VM manager responsible for allocating hardware resources to each virtual OS. If each Guest OS needs 1GB of memory, and the host uses 1GB of memory, then the setup on the left would require 4GB total.

左侧是使用管理程序的传统虚拟机设置。 虚拟机管理程序只是您的VM经理,负责为每个虚拟OS分配硬件资源。 如果每个Guest OS需要1GB的内存,而主机使用1GB的内存,则左侧的设置总共需要4GB。

On the right side is a container setup, which would run a container engine such as Docker. The most significant difference is that a container engine is more lightweight because it can share certain hardware resources with its host OS, unlike traditional VMs that require their own separate allocation. This setup requires 1GB for the host OS and perhaps 600MB per container (because 300MB is hypothetically shared with the host OS), for a total of 2.8GB required. See those benefits? Cool, now we can start!

右侧是容器设置,它将运行诸如Docker之类的容器引擎。 最显着的区别在于,容器引擎更轻巧,因为它可以与其主机OS共享某些硬件资源,这与传统的VM需要单独分配资源不同。 对于主机操作系统,此设置需要1GB,每个容器可能需要600MB(因为假设与主机OS共享300MB),因此总共需要2.8GB。 看到那些好处了吗? 酷,现在我们可以开始了!

入门 (Getting Started)

Enter your EC2 instance and clone Kangzeroos-ES6-React-Redux-Boilerplate from Github. The code you see will be for setting up this web app, but the steps are the same for any project. Once you have it downloaded, go into the folder and find the below files. These are the files we will use with Docker.

输入您的EC2实例,并从Github克隆Kangzeroos-ES6-React-Redux-Boilerplate 。 您看到的代码将用于设置此Web应用程序,但是任何项目的步骤都是相同的。 下载后,进入文件夹并找到以下文件。 这些是我们将用于Docker的文件。

Dockerfile
build.sh
run.sh

Before we can use Docker, we must first install it. Below is the quick and simple way to install Docker, but if you want the full configuration checkout the official docs.

在使用Docker之前,我们必须先安装它。 以下是安装Docker的快速简便的方法,但是如果您想进行完整配置,请查看官方文档

$ sudo apt-get update
$ sudo apt-get install docker-engine
$ sudo service docker start
$ sudo docker run hello-world

The last command checks if Docker is successfully running, and then exits. If all this works, you are ready to start Dockerizing!

最后一个命令检查Docker是否成功运行,然后退出。 如果所有这些都有效,则准备开始Dockerizing!

第1步:构建Dockerfile (Step 1: Building the Dockerfile)

The first step is to configure the files required for Docker to build itself an image. Docker Images are simply blueprints of environments that you want to create while containers are the actual running and functional environments that your app will be executed in. In the root of our app directory, there is a folder called App. The web app itself resides in this App folder, whereas all the Docker-related stuff is outside. This is necessary as Docker will be containerizing everything inside App. So let’s make the first Docker file called Dockerfile (no file extension Dockerfile.sh, just Dockerfile) and walk through it line-by-line.

第一步是配置Docker自身构建映像所需的文件。 Docker Images只是您要创建的环境的蓝图,而容器是将在其中执行应用程序的实际运行和功能环境。在我们应用程序目录的根目录中,有一个名为App的文件夹。 Web应用程序本身位于此App文件夹中,而所有与Docker相关的内容都在外部。 这是必要的,因为Docker将容器化App内部的所有内容。 因此,让我们制作第一个名为Dockerfile Docker文件(无文件扩展名Dockerfile.sh ,只是Dockerfile )并逐行浏览。

FROM ubuntu 

# ubuntu setup
RUN apt-get update -y
RUN apt-get upgrade -y 
RUN apt-get install nodejs -y && apt-get install npm -y 

# install curl for n
RUN apt-get install curl -y
RUN apt-get install vim -y 

# obtain latest stable version of node
RUN npm cache clean -f
RUN npm install -g n
RUN n stable 

# setup working directory
# ADD /App /App
WORKDIR /App
RUN npm install 

# expose port
EXPOSE 8080

The first line is FROM ubuntu. The purpose of Dockerfile is to set up the OS and programs inside the OS, so it makes sense that the first line specifies which OS version to use. ubuntu here is referring to a specific image hosted on Docker Hub, specifically the official Ubuntu OS Image.

第一行是FROM ubuntuDockerfile的目的是在操作系统中设置操作系统和程序,因此第一行指定要使用的操作系统版本是有意义的。 这里的ubuntu是指托管在Docker Hub上的特定映像,特别是官方的Ubuntu OS映像。

# ubuntu setup
RUN apt-get update -y
RUN apt-get upgrade -y 
RUN apt-get install curl -y
RUN apt-get install vim -y

The next set of lines is setup within Ubuntu. We want to check for Ubuntu updates with RUN apt-get update -y and upgrades with RUN apt-get upgrade -y… pretty standard stuff for setting up your environment. Also install curl RUN apt-get install curl -y and vim RUN apt-get install vim -y, both nice to have for general purposes.

下一行是在Ubuntu中设置的。 我们要检查RUN apt-get update -y Ubuntu更新,以及RUN apt-get upgrade -y ……用于设置环境的相当标准的东西。 还可以安装curl RUN apt-get install curl -y和vim RUN apt-get install vim -y ,它们对于一般用途都很好。

# obtain latest stable version of node
RUN apt-get install nodejs -y && apt-get install npm -y
RUN npm cache clean -f
RUN npm install -g n
RUN n stable

The next set of lines is setup specific to NodeJS. Since we want to be using ES6 features, we will need the latest version of NodeJS attained via the node module n. Install NodeJS and NPM with RUN apt-get install nodejs -y && apt-get install npm -y. Then clean up npm to make way for n using RUN npm cache clean -f. Install n with RUN npm install -g n. And finally, we can run n (latest version of NodeJS) with RUN n stable.

下一行是特定于NodeJS的设置。 由于我们要使用ES6功能,因此我们需要通过节点模块n获得的最新版本的NodeJS。 使用RUN apt-get install nodejs -y && apt-get install npm -y和NPM。 然后使用RUN npm cache clean -f清理npm以让出n 。 使用RUN npm install -gn安装n 。 最后,我们可以使用RUN n stable运行n (最新版本的NodeJS)。

NodeJS is for Javascript, but if you were working with other languages such as Python, you would install whatever programs you need to run your Python app.

NodeJS适用于Javascript,但是如果您使用的是Python等其他语言,则可以安装运行Python应用程序所需的任何程序。

# setup working directory
ADD /App /App
WORKDIR /App
RUN npm install

# expose port
EXPOSE 8080

The last part of Dockerfile is to set up the working directory of the app itself. ADD /App /App takes the App folder from our machine and copies it into the Docker container. Next, WORKDIR /App sets the Docker working directory to /App so that any commands you run inside Docker are executed in /App. This is needed for npm install to install in the correct place (aka /App of the Docker container).

Dockerfile的最后一部分是设置应用程序本身的工作目录。 ADD /App /App从我们的机器上获取App文件夹并将其复制到Docker容器中。 接下来, WORKDIR /App将Docker工作目录设置为/App以便在Docker内部运行的所有命令都在/App中执行。 npm install需要npm install其安装在正确的位置(也就是Docker容器的/App )。

Finally, we RUN npm install which installs our NodeJS dependencies in our machine. Last of all, we will explicitly expose port 8080 of our Docker image with EXPOSE 8080 so that the outside world can access our app. The outside world includes the internet as well as other Docker containers running on the same machine.

最后,我们RUN npm install ,它将NodeJS依赖项安装在我们的计算机中。 最后,我们将使用EXPOSE 8080显式公开Docker映像的端口8080,以便外界可以访问我们的应用程序。 外部世界包括互联网以及同一台机器上运行的其他Docker容器。

步骤2:构建脚本 (Step 2: The build script)

docker build -t kangzeroo .

Create a new file in the root directory of your app called build.sh. This is a shell file for building our Docker container. This build.sh file is not actually necessary as we can directly run this command in the terminal. However, it is really nice for simplifying the process.

在应用程序的根目录中创建一个名为build.sh的新文件。 这是用于构建Docker容器的shell文件。 实际上,不需要build.sh文件,因为我们可以在终端中直接运行此命令。 但是,简化流程确实非常好。

Here’s the breakdown of this line: docker build is the command that tells Docker to build an image. -t kangzeroo sets the tag name of the Docker image to kangzeroo, which we can reference later. Please not that in order to have a valid tag name, it must be in lowercase and has no spaces (use snake-case naming). Finally, . tells Docker where to look for the Dockerfile that is needed for the build ( . means here).

这是此行的细目: docker build是告诉Docker构建映像的命令。 -t kangzeroo将Docker映像的标签名称设置为kangzeroo ,我们稍后可以参考。 请不要为了拥有有效的标签名称而使用小写字母,并且不能使用空格(使用蛇形字母命名)。 最后, . 告诉Docker在哪里寻找构建所需的Dockerfile ( .表示此处)。

If you are in an EC2 instance, we can run bash build.sh from our project’s root directory. This will start the Docker build process as it goes through the steps in the Dockerfile we created. This might take a while… at the end it should look like this: (Don’t worry about the non-critical errors such as the optional dependency skipped in the below screenshot).

如果您在EC2实例中,则可以从项目的根目录运行bash build.sh 。 在我们创建的Dockerfile中的Dockerfile步骤中,这将开始Docker构建过程。 这可能需要一段时间……最后看起来应该是这样的:(不必担心非关键错误,例如下面的屏幕快照中跳过的可选依赖项)。

Now let’s check if our image has been created. Type docker images to see the images currently running in our machine. You should see a result like this:

现在,让我们检查是否已创建图像。 输入docker images来查看当前在我们机器上运行的图像。 您应该看到如下结果:

If we want to delete this image, simply type docker rmi kangzeroo. If you type docker images again after deleting, you will see that the image is no longer there. For now, let’s leave the image because we’re gonna use it to build the Docker container for our app to run in.

如果要删除此映像,只需键入docker rmi kangzeroo 。 如果在删除后再次键入docker images ,您将看到该图像不再存在。 现在,让我们离开该图像,因为我们将使用它来构建用于运行我们的应用程序的Docker容器。

步骤3:执行指令码 (Step 3: The run script)

Now that our image has been created, let’s make run.sh. Recall that Docker Images are simply blueprints of environments that you want to create. Containers are the actual running and functional environments that your app will be executed in. So run.sh will turn our images into containers. Here is what run.sh looks like:

现在我们的映像已经创建,让我们运行run.sh 回想一下,Docker镜像仅仅是您要创建的环境的蓝图。 容器是将在其中运行您的应用程序的实际运行和功能环境。因此run.sh会将我们的图像转换为容器。 这是run.sh样子:

docker run -d -it -p 80:8080 --name=kz kangzeroo npm run ec2 -- --host=0.0.0.0

Let’s walkthrough this short script. docker run is the command to run a container from an image. -d -it is the command for daemon (running tasks in the background) and interactive terminal (giving us a way to interact with the container). If you omit -d then the docker container will not run in the background and you will see log output from the app. -p 80:8080 maps port 80 of our machine to port 8080 of the container. Recall that earlier we specified EXPOSE 8080 in our Dockerfile. So now we take incoming connections on our machine’s port 80 (port 80 is the default for http) and redirect them to our container’s port 8080. If your app is not a webpage, then you can exclude this port mapping.--name=kz gives our container the name kz. Finally, kangzeroo npm run ec2 refers to our image called kangzeroo and npm run ec2 is a command specific to this boilerplate app (for starting up the app). The last part — — host=0.0.0.0 sets the boilerplate to run on 0.0.0.0 instead of localhost (This too is specific to the boilerplate). If you were running a Python backend app, it would look like docker run -d -it --name=kz kangzeroo python app.py.

让我们来看一下这个简短的脚本。 docker run是从映像运行容器的命令。 -d -itdaemon (在daemon运行任务)和interactive terminal (为我们提供一种与容器交互的方式)的命令。 如果省略-d则docker容器将不会在后台运行,并且您将看到该应用程序的日志输出。 -p 80:8080将我们机器的端口80映射到容器的端口8080。 回想一下,我们EXPOSE 8080Dockerfile指定了EXPOSE 8080 。 因此,现在我们在计算机的端口80上建立传入连接(端口80是http的默认端口),并将它们重定向到容器的端口8080。如果您的应用程序不是网页,则可以排除此端口映射。 --name=kz为我们的容器命名为kz 。 最后, kangzeroo npm run ec2指的是我们称为kangzeroonpm run ec2是此样板应用程序专用的命令(用于启动该应用程序)。 最后一部分-host — — host=0.0.0.0将样板设置为在0.0.0.0而不是localhost上运行(这也专门针对样板)。 如果您正在运行Python后端应用程序,则它看起来像docker run -d -it --name=kz kangzeroo python app.py

Great! Save this file and run it with bash run.sh. Then check if the container is running by typing docker ps -a. This is what it should look like:

大! 保存此文件并使用bash run.sh运行它。 然后通过键入docker ps -a检查容器是否正在运行。 它应该是这样的:

Your app is now online and running inside a Docker container! Check if it works… for this boilerplate you can check from a web browser.

您的应用程序现已在线运行,并在Docker容器中运行! 检查是否有效…对于此样板,可以从Web浏览器中检查。

And it’s working! Great, now let’s turn off our Docker container. Type docker ps -a to see all the containers again. Type docker stop kz and it stops the container. If you type docker ps you will not see the container anymore, but you will see it if you type docker ps -a ( -a means all, inclusive of running and not running container. omit -a if you only want to see running containers). To remove the container, type docker rm kz. If you type docker ps -a you won’t see the container anymore.

它正在工作! 太好了,现在让我们关闭Docker容器。 键入docker ps -a再次查看所有容器。 输入docker stop kz ,它将停止容器。 如果键入docker ps ,将不会再看到该容器,但是如果键入docker ps -a ( -a表示全部,包括正在运行且未在运行的容器,则将看到该容器。如果只想查看正在运行的容器,则省略-a )。 要删除容器,请键入docker rm kz 。 如果键入docker ps -a ,将不再看到该容器。

结论 (Conclusion)

That was Docker! All things considered, Docker is a lot easier than setting up a hypervisor-based VM, and you can see how a microservice architecture becomes a lot easier to manage when you adopt containers. With our Dockerfile, build.sh and run.sh files created in EC2, we can summarize the 3 steps to running Docker from our app root directory:

那是Docker! 考虑到所有因素,与设置基于虚拟机管理程序的VM相比,Docker容易得多,并且您可以看到采用容器时微服务架构如何变得更易于管理。 借助在EC2中创建的Dockerfilebuild.shrun.sh文件,我们可以总结从应用程序根目录运行Docker的3个步骤:

$ bash build.sh
$ bash run.sh
$ exit

That’s it! Docker: easy as build, run, done!

而已! Docker:易于构建,运行,完成!

奖金备忘单 (Bonus Cheatsheet)

Since this tutorial took a step-by-step approach to teaching Docker, I think it’s appropriate to leave you off with an overview of all the Docker commands you will need for general purpose use.

由于本教程采用了逐步讲授Docker的方法,因此我认为对通用用途的所有Docker命令进行概述是合适的。

$ docker images                     // To view install images
$ docker rmi <IMAGE_NAME>           // To remove an installed image

$ docker ps -a                      // To view all docker containers
$ docker stop <CONTAINER_NAME>      // To stop a docker container
$ docker rm <CONTAINER_NAME>        // To remove a docker container

$ docker exec -it <CONTAINER_NAME> bash    // Execute into container and run bash

* If you want to see the log output from a docker container, omit the -d from run.sh

These methods were partially used in the deployment of renthero.ca

这些方法部分地用于了renthero.ca的部署中

翻译自: https://www.freecodecamp.org/news/docker-easy-as-build-run-done-e174cc452599/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值