Docker 101-如何从创建到部署

Docker is a game-changer, and has very much altered the world of application development. Learn the vital skills needed to work with this container technology today.

Docker是改变游戏规则的人,它极大地改变了应用程序开发的世界。 了解当今使用此容器技术所需的重要技能。

什么是Docker? (What is Docker?)

In simple terms, Docker is a tool that lets developers to create, deploy, and run applications in containers. Containerization is the use of Linux containers to deploy applications.

简而言之, Docker是一种工具,开发人员可以使用它在容器中创建,部署和运行应用程序。 容器化是使用Linux容器部署应用程序。

So why is Docker so great, and why should we as developers even bother learning it?

那么,为什么Docker如此出色?为什么我们作为开发人员还要费心地学习它呢?

ReasonExplanation
FlexibleEven the most complex applications can be containerized.
LightweightContainers leverage and share the host kernel.
InterchangeableYou can deploy updates and upgrades on-the-fly.
PortableYou can build locally, deploy to the cloud, and run anywhere.
ScalableYou can increase and automatically distribute container replicas.
StackableYou can stack services vertically and on-the-fly.
原因 说明
灵活 甚至最复杂的应用程序也可以被容器化。
轻巧的 容器利用并共享主机内核。
可互换 您可以即时部署更新和升级。
随身携带 您可以在本地构建,部署到云并在任何地方运行。
可扩展 您可以增加并自动分发容器副本。
可堆叠 您可以垂直和动态地堆叠服务。

Now that we know why Docker is such a big deal, let's have it installed on our local machine.

现在我们知道了为什么Docker这么重要,让我们将其安装在本地计算机上。

Sign up for an account on Docker Hub and download the free Docker Desktop application.

Docker Hub上注册一个帐户,然后下载免费的Docker Desktop应用程序。

Docker与传统虚拟机有何不同? (How is Docker different from traditional Virtual Machines?)

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs as a discrete process, taking no more memory than any other executable meaning it's very lightweight.

容器在Linux上本地运行,并与其他容器共享主机的内核。 它作为离散进程运行,占用的内存不超过任何其他可执行文件,这意味着它非常轻巧。

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

相比之下,虚拟机(VM)运行具有“虚拟机管理程序”对主机资源的虚拟访问权的成熟“来宾”操作系统。 通常,VM为环境提供的资源比大多数应用程序所需的资源更多。

When working with Docker, a `Dockerfile` defines what goes on in the environment inside your container. Access to resources like networking interfaces and disk drives are virtualized inside this environment, which is isolated from the rest of your system. This means that you need to map ports to the outside world, and be specific about what files you want to “copy in” to that environment. However, after doing that, you can expect that the build of your app defined in this `Dockerfile` behaves exactly the same wherever it runs.

使用Docker时,`Dockerfile`定义了容器内部环境中发生的事情。 对网络接口和磁盘驱动器等资源的访问在此环境内虚拟化,与系统的其余部分隔离。 这意味着您需要将端口映射到外界,并具体说明要“复制”到该环境的文件。 但是,这样做之后,您可以期望在此Dockerfile中定义的应用程序构建无论在任何地方运行都具有完全相同的行为。

Docker命令 (Docker Commands)

To test that you have a running version of Docker, run the following command:

要测试您是否正在运行Docker版本,请运行以下命令:

docker --version

docker --version

To test that your installation is working perfectly, try running the simple Docker hello-world image:

要测试您的安装是否正常运行,请尝试运行简单的Docker hello-world映像:

docker run hello-world

docker run hello-world

If all is set up properly, you should see output similar to the following:

如果所有设置都正确,您应该会看到类似以下的输出:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

To see the hello-world Docker image that was downloaded to your computer, use the Docker image listing command:

要查看已下载到您的计算机的hello-world Docker映像,请使用Docker映像列表命令:

docker image ls

docker image ls

Awesome! You've already started developing containerized applications with Docker. Here are some helpful basic Docker commands:

太棒了! 您已经开始使用Docker开发容器化应用程序。 以下是一些有用的基本Docker命令:

## List Docker CLI commands
docker
docker container --help

## Display Docker version and info
docker --version
docker version
docker info

## Execute Docker image
docker run hello-world

## List Docker images
docker image ls

## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq
  • applications have no system dependencies

    应用程序没有系统依赖性
  • updates can be pushed to any part of a distributed application

    可以将更新推送到分布式应用程序的任何部分
  • resource density can be optimized.

    资源密度可以优化。
  • With Docker, scaling your application is a matter of spinning up new executables, not running heavy VM hosts.

    使用Docker,扩展应用程序仅需旋转新的可执行文件,而无需运行繁重的VM主机。

让我们使用Docker构建一个Node.js Web应用程序 (Let's build a Node.js web app using Docker)

The first thing we do is create a package.json file. We can do this quickly by simply running the following command:

我们要做的第一件事是创建一个package.json文件。 我们只需运行以下命令即可快速完成此操作:

npm init -y

This creates the file above with certain essential fields already filled in or left blank.

这将创建上面的文件,其中某些必填字段已经填写或留空。

Your file should look something like this:

您的文件应如下所示:

{
  "name": "app-name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Next, we install express.js, which according to the official website, is a "Fast, unopinionated, minimalist web framework for Node.js".

接下来,我们安装express.js ,根据官方网站 ,它是“ Node.js的快速,不受限制的,简约的Web框架 ”。

We do this by running the following command in a terminal:

为此,我们在终端中运行以下命令:

npm install express --save

The command above adds the express.js framework to our application, with the --save flag acting as an instruction to the application to use express.js as a dependency.

上面的命令将express.js框架添加到我们的应用程序中,其中--save标志用作对应用程序的指令,以使用express.js作为依赖项。

Now, go into your package.json, and change the "main": "index.js" key-value pair to the following:

现在,进入您的package.json ,并将"main": "index.js"键值对更改为以下内容:

"main": "app.js"

Next, create a .gitignore file using the following command:

接下来,使用以下命令创建一个.gitignore文件:

touch .gitignore

Then add the following line:

然后添加以下行:

node_modules/

This prevents the node_modules folder which is essential to node.js development from being tracked by git.

这可以防止git跟踪对node.js开发必不可少的node_modules文件夹。

Now add the following code to the app.js file:

现在,将以下代码添加到app.js文件:

const express = require('express');

const app = express();

const PORT = 8080;
const HOST = '0.0.0.0';

app.get('/', (req, res) => {
  res.send(
    `
    <h1>Home</h1>
    <p>Docker is awesome!</p>
    <a href="/more" alt="Next Page">Next Page</a>
    `
  )
});

app.get('/more', (req, res) => {
  res.send(
    `
    <h1>Page Two</h1>
    <p>Node.js is pretty great too!!</p>
    <a href="/" alt="Back Home">Back Home</a>
    `
  )
});

app.listen(PORT, HOST);
console.log(`Running on https://${HOST}:${PORT}`);

To have this run on your local machine, run the following command in the application folder:

要在本地计算机上运行此命令,请在应用程序文件夹中运行以下命令:

npm start

You will find the application running at http://0.0.0.0:8080/

您会在http://0.0.0.0:8080/找到运行的应用程序

太棒了! (Awesome!)

Congratulations
Congratulations on making it this far

恭喜!



进入Dockerverse (Into the Dockerverse)

Now create a Dockerfile with the following command:

现在,使用以下命令创建一个Dockerfile

touch Dockerfile

Then add in the following code:

然后添加以下代码:

# An official Docker image for Node.js
FROM node:10-alpine

# Working directory for the containerised application
WORKDIR /src/app

# This copies significant package.json files to the current directory
COPY package*.json ./
# Install essential Node.js dependencies
RUN npm install

COPY . .

# Opens up this port on the Docker container
EXPOSE 8080

# This starts the Docker application
CMD [ "npm", "start" ]

The comments above attempt to explain what each Dockerfile command does.

上面的注释试图解释每个Dockerfile命令的功能。

Also, add a dockerignore file to prevent the containerisation of certain components of the application.

另外,添加dockerignore文件以防止应用程序某些组件的容器化。

Place this inside of the dockerignore file:

将此放置在dockerignore文件中:

node_modules
npm-debug.log
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
README.md
LICENSE

如何部署 (How to Deploy)

The <image-name> is the name you assign to your Docker app, and <tag> is essentially just a version indicator for your Docker image.

<image-name>是您分配给Docker应用程序的名称,而<tag>本质上只是Docker映像的版本指示符。

  • docker build -t image-name:tag .

    docker build -t image-name:tag .

Run this to access your Docker account from your terminal.

运行此命令以从终端访问您的Docker帐户。

  • docker login

    docker login

Create a repository on Docker Hub.

在Docker Hub上创建一个存储库。

Tag <image> for upload to registry.

标记<image>传到注册表。

  • docker tag <image-name> username/repository:tag

    docker tag <image-name> username/repository:tag

Upload the tagged image to the registry.

将标记的图像上传到注册表。

  • docker push username/repository:tag

    docker push username/repository:tag

Run the deployed Docker container on your local machine by connecting its PORTS. Target the exposed 8080 port, and assign it to port 10203 on your machine.

通过连接其端口在本地计算机上运行已部署的Docker容器。 定位到暴露的8080端口,并将其分配给计算机上的端口10203。

  • docker run -p 10203:8080 username/repository:tag

    docker run -p 10203:8080 username/repository:tag



而已! 您已经构建并部署了容器化的Node.js Web应用程序。 (That's it! You have built and deployed a containerised Node.js web application.)

All the code above can be found in this Github repository.

上面的所有代码都可以在该Github存储库中找到。

Originally posted here on blog.ninte.dev

最初发布在这里 blog.ninte.dev

翻译自: https://www.freecodecamp.org/news/docker-101-creation-to-deployment/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值