博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用远程Docker服务器加快工作流程
阅读量:2505 次
发布时间:2019-05-11

本文共 8075 字,大约阅读时间需要 26 分钟。

介绍 (Introduction)

Building CPU-intensive images and binaries is a very slow and time-consuming process that can turn your laptop into a space heater at times. Pushing Docker images on a slow connection takes a long time, too. Luckily, there’s an easy fix for these issues. Docker lets you offload all those tasks to a remote server so your local machine doesn’t have to do that hard work.

构建占用大量CPU的图像和二进制文件是一个非常缓慢且耗时的过程,有时会使您的笔记本电脑变成空间加热器。 将Docker映像推送到慢速连接也需要很长时间。 幸运的是,可以轻松解决这些问题。 Docker使您可以将所有这些任务卸载到远程服务器上,因此您的本地计算机不必进行繁琐的工作。

This feature was introduced in Docker 18.09. It brings support for connecting to a Docker host remotely via SSH. It requires very little configuration on the client, and only needs a regular Docker server without any special config running on a remote machine. Prior to Docker 18.09, you had to use Docker Machine to create a remote Docker server and then configure the local Docker environment to use it. This new method removes that additional complexity.

Docker 18.09中引入了此功能。 它带来了对通过SSH远程连接到Docker主机的支持。 它在客户端上只需要很少的配置,并且只需要一台常规的Docker服务器,而无需在远程机器上运行任何特殊的配置。 在Docker 18.09之前,您必须使用Docker Machine创建远程Docker服务器,然后配置本地Docker环境以使用它。 这种新方法消除了这种额外的复杂性。

In this tutorial, you’ll create a Droplet to host the remote Docker server and configure the docker command on your local machine to use it.

在本教程中,您将创建一个Droplet来托管远程Docker服务器,并在本地计算机上配置docker命令以使用它。

先决条件 (Prerequisites)

To follow this tutorial, you’ll need:

要遵循本教程,您需要:

  • A DigitalOcean account. You can if you don’t have one already.

    一个DigitalOcean帐户。 如果您还没有则可以 。

  • installed on your local machine or development server. If you are working with Ubuntu 18.04, follow Steps 1 and 2 of ; otherwise, follow the for information about installing on other operating systems. Be sure to add your non-root user to the docker group, as described in Step 2 of the linked tutorial.

    安装在本地计算机或开发服务器上。 如果您正在使用Ubuntu 18.04,请遵循步骤1和2; 否则,请遵循以获取有关在其他操作系统上安装的信息。 确保按照链接教程的第2步中所述将非root用户添加到docker组。

步骤1 –创建Docker主机 (Step 1 – Creating the Docker Host)

To get started, spin up a Droplet with a decent amount of processing power. The CPU Optimized plans are perfect for this purpose, but Standard ones work just as well. If you will be compiling resource-intensive programs, the CPU Optimized plans provide dedicated CPU cores which allow for faster builds. Otherwise, the Standard plans offer a more balanced CPU to RAM ratio.

首先,以相当大的处理能力启动一个Droplet。 为此,“ CPU优化”计划是完美的选择,但标准计划也可以使用。 如果要编译资源密集的程序,则“ CPU优化”计划将提供专用的CPU内核,这些内核可加快构建速度。 否则,标准计划将提供更加平衡的CPU与RAM比率。

The takes care of all of the setup for us. to create a 16GB/8vCPU CPU-Optimized Droplet with Docker from the control panel.

为我们完成了所有设置。 单击从控制面板使用Docker创建一个16GB / 8vCPU CPU优化的Droplet。

Alternatively, you can use doctl to create the Droplet from your local command line. To install it, follow the instructions in the .

或者,您可以使用doctl从本地命令行创建Droplet。 要安装它,请遵循 。

The following command creates a new 16GB/8vCPU CPU-Optimized Droplet in the FRA1 region based on the Docker One-click image:

以下命令基于Docker一键式映像在FRA1区域中创建新的16GB / 8vCPU CPU优化的Droplet:

  • doctl compute droplet create docker-host \

    doctl计算Droplet创建docker-host \
  • --image docker-18-04 \

    --image docker-18-04 \

  • --region fra1 \

    --region fra1 \

  • --size c-8 \

    --size c-8 \

  • --wait \

    --wait \
  • --ssh-keys $(doctl compute ssh-key list --format ID --no-header | sed 's/$/,/' | tr -d '\n' | sed 's/,$//')

    --ssh-keys $(doctl计算ssh-key列表--format ID --no-header | sed's / $ /,/'| tr -d'\ n'| sed's /,$ //' )

The doctl command uses the ssh-keys value to specify which SSH keys it should apply to your new Droplet. We use a subshell to call doctl compute ssh-key-list to retrieve the SSH keys associated with your DigitalOcean account, and then parse the results using the sed and tr commands to format the data in the correct format. This command includes all of your account’s SSH keys, but you can replace the highlighted subcommand with the fingerprint of any key you have in your account.

doctl命令使用ssh-keys值来指定应将SSH密钥应用于新的Droplet。 我们使用一个子外壳调用doctl compute ssh-key-list来检索与您的DigitalOcean帐户关联的SSH密钥,然后使用sedtr命令解析结果以将数据格式化为正确的格式。 该命令包括您帐户的所有SSH密钥,但是您可以用帐户中任何密钥的指纹替换突出显示的子命令。

Once the Droplet is created you’ll see its IP address among other details:

创建Droplet后,您将看到其IP地址以及其他详细信息:

Output   
ID Name Public IPv4 Private IPv4 Public IPv6 Memory VCPUs Disk Region Image Status Tags Features Volumes148681562 docker-host your_server_ip 16384 8 100 fra1 Ubuntu Docker 5:18.09.6~3 on 18.04 active

You can learn more about using the doctl command in the tutorial .

您可以在教程“ 了解有关使用doctl命令的更多信息。

When the Droplet is created, you’ll have a ready to use Docker server. For security purposes, create a Linux user to use instead of root.

创建Droplet后,您将可以使用Docker服务器。 为了安全起见,创建一个Linux用户而不是root用户

First, connect to the Droplet with SSH as the root user:

首先,以root用户身份使用SSH连接到Droplet:

  • ssh root@your_server_ip

    ssh root @ your_server_ip

Once connected, add a new user. This command adds one named sammy:

连接后,添加一个新用户。 此命令添加一个名为sammy的命令:

  • adduser sammy

    adduser sammy

Then add the user to the docker group to give it permission to run commands on the Docker host.

然后将用户添加到docker组,以授予其在Docker主机上运行命令的权限。

  • sudo usermod -aG docker sammy

    sudo usermod -aG码头工人

Finally, exit from the remote server by typing exit.

最后,通过键入exit从远程服务器exit

Now that the server is ready, let’s configure the local docker command to use it.

现在服务器已准备就绪,让我们配置本地docker命令以使用它。

第2步–配置Docker使用远程主机 (Step 2 – Configuring Docker to Use the Remote Host)

To use the remote host as your Docker host instead of your local machine, set the DOCKER_HOST environment variable to point to the remote host. This variable will instruct the Docker CLI client to connect to the remote server.

要将远程主机用作Docker主机而不是本地计算机,请设置DOCKER_HOST环境变量以指向远程主机。 该变量将指示Docker CLI客户端连接到远程服务器。

  • export DOCKER_HOST=ssh://sammy@your_server_ip

    导出DOCKER_HOST = ssh:// sammy @ your_server_ip

Now any Docker command you run will be run on the Droplet. For example, if you start a web server container and expose a port, it will be run on the Droplet and will be accessible through the port you exposed on the Droplet’s IP address.

现在,您运行的所有Docker命令都将在Droplet上运行。 例如,如果您启动一个Web服务器容器并公开一个端口,它将在Droplet上运行,并且可以通过您在Droplet的IP地址上公开的端口进行访问。

To verify that you’re accessing the Droplet as the Docker host, run docker info.

要验证您是否以Docker主机身份访问Droplet,请运行docker info

  • docker info

    码头工人信息

You will see your Droplet’s hostname listed in the Name field like so:

您将在“ Name字段中看到Droplet的主机名,如下所示:

Output   
…Name: docker-host…

One thing to keep in mind is that when you run a docker build command, the build context (all files and folders accessible from the Dockerfile) will be sent to the host and then the build process will run. Depending on the size of the build context and the amount of files, it may take a longer time compared to building the image on a local machine. One solution would be to create a new directory dedicated to the Docker image and copy or link only the files that will be used in the image so that no unneeded files will be uploaded inadvertently.

要记住的一件事是,当您运行docker build命令时,构建上下文(可从Dockerfile访问的所有文件和文件夹)将被发送到主机,然后运行构建过程。 根据构建上下文的大小和文件的数量,与在本地计算机上构建映像相比,可能需要更长的时间。 一种解决方案是创建一个专用于Docker映像的新目录,并仅复制或链接将在映像中使用的文件,以便不会无意中上传不需要的文件。

Once you’ve set the DOCKER_HOST variable using export, its value will persist for the duration of the shell session. Should you need to use your local Docker server again, you can clear the variable using the following command:

使用export设置DOCKER_HOST变量后,其值将在Shell会话持续时间内保持DOCKER_HOST 。 如果您需要再次使用本地Docker服务器,则可以使用以下命令清除变量:

unset DOCKER_HOST

结论 (Conclusion)

You’ve created a remote Docker host and connected to it locally. The next time your laptop’s battery is running low or you need to build a heavy Docker image, use your shiny remote Docker server instead of your local machine.

您已经创建了一个远程Docker主机并在本地连接到它。 下次笔记本电脑的电池电量不足或需要构建沉重的Docker映像时,请使用发光的远程Docker服务器而不是本地计算机。

You might also be interested in learning , or .

您可能还对学习或 。

翻译自:

转载地址:http://vnegb.baihongyu.com/

你可能感兴趣的文章
[转]为什么不去读顶级会议上的论文
查看>>
Android模拟按键——源码环境下开发应用程序 ---编译jar(android可执行程序)
查看>>
Android Bluetooth HID实现详解
查看>>
Android AudioRecord Demo[录音并且声称PCM文件]
查看>>
设计模式之---代理模式
查看>>
解决SpringMVC重复提交的问题
查看>>
使用SSH命令行远程登录运行在CloudFoundry上的应用
查看>>
Telnet实现windows远程登录ubuntu
查看>>
Jenkins安装
查看>>
java面试题之osi七层网络模型,五层网络模型,每层分别有哪些协议(阿里面试题)...
查看>>
计算机名词、论文用语
查看>>
RESTful-rest_framework认证组件、权限组件、频率组件-第五篇
查看>>
手机自带功能调用
查看>>
百度搜索引擎取真实地址-python代码
查看>>
java 多线程 Future callable
查看>>
字符串操作练习:星座、凯撒密码、99乘法表
查看>>
Java实现字符串转换十六进制MD5值
查看>>
MySQL数据库8(十七)数据库的备份还原
查看>>
tensorflow 梯度下降以及summary
查看>>
9、接口和抽象类
查看>>