博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Windows 10 Home上安装Docker
阅读量:2505 次
发布时间:2019-05-11

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

If you’ve ever tried to install , you’ve probably came to realize that the installer won’t run on Windows 10 Home. Only Windows Pro, Enterprise or Education support Docker. Upgrading your Windows license is pricey, and also pointless, since you can still run Linux Containers on Windows without relying on technology, a requirement for Docker for Windows.

如果您曾经尝试安装 ,则可能已经意识到安装程序无法在Windows 10 Home上运行。 仅Windows Pro,Enterprise或Education支持Docker。 升级Windows许可证价格昂贵,而且毫无意义,因为您仍然可以在Windows上运行Linux容器,而无需依赖技术,这是Windows Docker的要求。

If you plan on running , you’ll need a specific version and build of Windows Server. Check out the for details.

如果计划运行 ,则需要特定版本的Windows Server。 查看以了解详细信息。

99.999% of the time, you only need a Linux Container, since it supports software built using open-source and .NET technologies. In addition, Linux Containers can run on any distro and on popular CPU architectures, including x86_64, ARM and IBM.

在99.999%的时间内,您仅需要Linux容器,因为它支持使用开源和.NET技术构建的软件。 另外,Linux容器可以在任何发行版和流行的CPU体系结构上运行,包括x86_64ARMIBM

In this tutorial, I’ll show you how to quickly setup a Linux VM on Windows Home running Docker Engine with the help of . Here’s a list of software you’ll need to build and run Docker containers:

在本教程中,我将向您展示如何在的帮助下在运行Docker Engine的Windows Home上快速设置Linux VM。 以下是构建和运行Docker容器所需的软件列表:

  • Docker Machine: a CLI tool for installing Docker Engine on virtual hosts

    Docker Machine :用于在虚拟主机上安装Docker Engine的CLI工具

  • Docker Engine: runs on top of the Linux Kernel; used for building and running containers

    Docker Engine :在Linux内核之上运行; 用于构建和运行容器

  • Docker Client: a CLI tool for issuing commands to Docker Engine via REST API

    Docker Client :一个CLI工具,用于通过REST API向Docker Engine发布命令

  • Docker Compose: a tool for defining and running multi-container applications

    Docker Compose :用于定义和运行多容器应用程序的工具

I’ll show how to perform the installation in the following environments:

我将展示如何在以下环境中执行安装:

  1. On Windows using

    在Windows上使用

  2. On Windows Subsystem for Linux 2 (running Ubuntu 18.04)

    在Linux 2的Windows子系统上(运行Ubuntu 18.04)

First, allow me to explain how the Docker installation will work on Windows.

首先,让我解释一下Docker安装将如何在Windows上运行。

Want to build more of a foundation in Docker knowledge? Read on SitePoint Premium.

是否想在Docker知识上建立更多基础? 在SitePoint Premium上阅读 。

这个怎么运作 (How it Works)

As you probably know, Docker requires a Linux kernel to run Linux Containers. For this to work on Windows, you’ll need to set up a Linux virtual machine to run as guest in Windows 10 Home.

您可能知道,Docker需要Linux内核才能运行Linux容器。 为了使其在Windows上正常运行,您需要设置一个Linux虚拟机以在Windows 10 Home中作为来宾运行。

docker windows home

Setting up the Linux VM can be done manually. The easiest way is to use Docker Machine to do this work for you by running a single command. This Docker Linux VM can either run on your local system or on a remote server. Docker client will use SSH to communicate with Docker Engine. Whenever you create and run images, the actual process will happen within the VM, not on your host (Windows).

可以手动设置Linux VM。 最简单的方法是使用Docker Machine通过运行一个命令为您完成这项工作。 该Docker Linux VM可以在本地系统或远程服务器上运行。 Docker客户端将使用SSH与Docker Engine通信。 每当您创建和运行映像时,实际过程将在VM内而不是在主机(Windows)上进行。

Let’s dive into the next section to set up the environment needed to install Docker.

让我们深入了解下一部分以设置安装Docker所需的环境。

最初设定 (Initial Setup)

You may or may not have the following applications installed on your system. I’ll assume you don’t. If you do, make sure to upgrade to the latest versions. I’m also assuming you’re running the latest stable version of Windows. At the time of writing, I’m using Windows 10 Home version 1903. Let’s start installing the following:

您可能会或可能不会在系统上安装以下应用程序。 我假设你没有。 如果这样做,请确保升级到最新版本。 我还假设您正在运行Windows的最新稳定版本。 在撰写本文时,我正在使用Windows 10家庭版1903。让我们开始安装以下内容:

  1. Install . This will be our primary terminal for running Docker commands.

    安装 。 这将是我们运行Docker命令的主要终端。

  2. Install , a package manager for Windows. It will make the work of installing the rest of the programs easier.

    安装 ,这是Windows的软件包管理器。 这将使安装其余程序的工作更加容易。

  3. Install and its extension. Alternatively, If you have finished installing Chocolatey, you can simply execute this command inside an elevated PowerShell terminal:

    安装及其扩展名。 另外,如果您已完成Chocolatey的安装,则只需在提升的PowerShell终端内执行以下命令:

    C:\ choco install virtualbox
  4. If you’d like to try running Docker inside the WSL2 environment, you’ll need to set up WSL2 first. You can follow this for step-by-step instructions.

    如果您想尝试在WSL2环境中运行Docker,则需要首先设置WSL2。 您可以按照本的逐步说明进行操作。

Docker引擎设置 (Docker Engine Setup)

Installing Docker Engine is quite simple. First we need to install Docker Machine.

安装Docker Engine非常简单。 首先,我们需要安装Docker Machine。

  1. Install Docker Machine by following instructions on this . Alternatively, you can execute this command inside an elevated PowerShell terminal:

    按照上的说明安装Docker Machine。 或者,您可以在提升的PowerShell终端中执行以下命令:

    C:\ choco install docker-machine
  2. Using Git Bash terminal, use Docker Machine to install Docker Engine. This will download a Linux image containing the Docker Engine and have it run as a VM using VirtualBox. Simply execute the following command:

    在Git Bash终端上,使用Docker Machine安装Docker Engine。 这将下载包含Docker Engine的Linux映像,并使用VirtualBox将其作为VM运行。 只需执行以下命令:

    $ docker-machine create --driver virtualbox default
  3. Next, we need to configure which ports are exposed when running Docker containers. Doing this will allow us to access our applications via localhost<:port>. Feel free to add as many as you want. To do this, you’ll need to launch Oracle VM VirtualBox from your start menu. Select default VM on the side menu. Next click on Settings > Network > Adapter 1 > Port Forwarding. You should find the ssh forwarding port already set up for you. You can add more like so:

    接下来,我们需要配置在运行Docker容器时公开哪些端口。 这样做将使我们能够通过localhost<:port>访问我们的应用程序。 随意添加任意数量。 为此,您需要从开始菜单启动Oracle VM VirtualBox 。 在侧面菜单上选择默认 VM。 接下来单击设置 > 网络 > 适配器1 > 端口转发 。 您应该找到已经为您设置的ssh转发端口。 您可以这样添加更多:

    docker vm ports
  4. Next, we need to allow Docker to mount volumes located on your hard drive. By default, you can only mount from the C://Users/ directory. To add a different path, simply go to Oracle VM VirtualBox GUI. Select default VM and go to Settings > Shared Folders. Add a new one by clicking the plus symbol. Enter the fields like so. If there’s an option called Permanent, enable it.

    接下来,我们需要允许Docker挂载位于您的硬盘驱动器上的卷。 默认情况下,您只能从C://Users/目录挂载。 要添加其他路径,只需转到Oracle VM VirtualBox GUI。 选择默认 VM,然后转到“设置” >“ 共享文件夹” 。 单击加号添加一个新的。 像这样输入字段。 如果有一个名为Permanent的选项,请启用它。

    docker vm volumes
  5. To get rid of the invalid settings error as seen in the above screenshot, simply increase Video Memory under the Display tab in the settings option. Video memory is not important in this case, as we’ll run the VM in headless mode.

    要消除上述屏幕截图中显示的无效设置错误,只需在设置选项的“显示”选项卡下增加“视频内存”即可。 在这种情况下,视频内存并不重要,因为我们将以无头模式运行VM。

  6. To start the Linux VM, simply execute this command in Git Bash. The Linux VM will launch. Give it some time for the boot process to complete. It shouldn’t take more than a minute. You’ll need to do this every time you boot your host OS:

    要启动Linux VM,只需在Git Bash中执行此命令。 Linux VM将启动。 给它一些时间来完成引导过程。 不应超过一分钟。 每次启动主机操作系统时,都需要执行以下操作:

    $ docker-machine start vbox
  7. Next, we need to set up our Docker environment variables. This is to allow the Docker client and Docker Compose to communicate with the Docker Engine running in the Linux VM, default. You can do this by executing the commands in Git Bash:

    接下来,我们需要设置我们的Docker环境变量。 这是为了允许Docker客户端和Docker Compose与default运行在Linux VM中的Docker Engine通信。 您可以通过执行Git Bash中的命令来做到这一点:

    # Print out docker machine instance settings$ docker-machine env default# Set environment variables using Linux 'export' command$ eval $(docker-machine env default --shell linux)

    You’ll need to set the environment variables every time you start a new Git Bash terminal. If you’d like to avoid this, you can copy eval output and save it in your .bashrc file. It should look something like this:

    每次启动新的Git Bash终端时,都需要设置环境变量。 如果要避免这种情况,可以复制eval输出并将其保存在.bashrc文件中。 它看起来应该像这样:

    export DOCKER_TLS_VERIFY="1"export DOCKER_HOST="tcp://192.168.99.101:2376"export DOCKER_CERT_PATH="C:\Users\Michael Wanyoike\.docker\machine\machines\default"export DOCKER_MACHINE_NAME="default"export COMPOSE_CONVERT_WINDOWS_PATHS="true"

    IMPORTANT: for the DOCKER_CERT_PATH, you’ll need to change the Linux file path to a Windows path format. Also take note that there’s a chance the IP address assigned might be different from the one you saved every time you start the default VM.

    重要信息 :对于DOCKER_CERT_PATH ,您需要将Linux文件路径更改为Windows路径格式。 另请注意,每次启动default VM时,分配的IP地址可能与您保存的IP地址不同。

In the next section, we’ll install Docker Client and Docker Compose.

在下一节中,我们将安装Docker Client和Docker Compose。

Docker工具设置 (Docker Tools Setup)

For this section, you’ll need to install the following tools using PowerShell in admin mode. These tools are packaged inside the Docker for Windows installer. Since the installer refuses to run on Windows 10 Home, we’ll install these programs individually using Chocolatey:

对于本节,您需要在管理员模式下使用PowerShell安装以下工具。 这些工具打包在Docker for Windows安装程序中。 由于安装程序拒绝在Windows 10 Home上运行,因此我们将使用Chocolatey单独安装以下程序:

C:\ choco install docker-cliC:\ choco install docker-compose

Once the installation process is complete, you can switch back to Git Bash terminal. You can continue using PowerShell, but I prefer Linux syntax to execute commands. Let’s execute the following commands to ensure Docker is running:

安装过程完成后,您可以切换回Git Bash终端。 您可以继续使用PowerShell,但是我更喜欢Linux语法来执行命令。 让我们执行以下命令以确保Docker正在运行:

# Start Docker VM$ docker-machine start default# Confirm Docker VM is running$ docker-machine ls# Configure Docker Envrionment to use Docker Vm$ eval $(docker-machine env default --shell linux)# Confirm Docker is connected. Should output Docker VM specs$ docker info# Run hello-world docker image. Should output "Hello from Docker"$ docker run hello-world

If all the above commands run successfully, it means you’ve successfully installed Docker. If you want to try out a more ambitious example, I have a small Node.js application that that I’ve configured to run on Docker containers. First, you’ll need to install using PowerShell with Admin privileges:

如果以上所有命令均成功运行,则表明您已成功安装Docker。 如果您想尝试一个更雄心勃勃的示例,那么我有一个小型的Node.js应用程序,该应用程序已配置为在Docker容器上运行。 首先,您需要使用具有Admin特权的PowerShell安装 :

C:\ choco install make

Next, execute the following commands. Running this Node.js example will ensure you have no problem with exposed ports and mounting volumes on the Windows filesystem. First, navigate to a folder that that you’ve already mounted in VirtualBox settings. Next, execute the following commands:

接下来,执行以下命令。 运行此Node.js示例将确保您在Windows文件系统上的公开端口和安装卷没有问题。 首先,导航到您已经在VirtualBox设置中装载的文件夹。 接下来,执行以下命令:

$ git clone git@github.com:brandiqa/docker-node.git$ cd docker-node/website$ make

When you hit the last command, you should expect a similar output:

当您按下最后一个命令时,您应该期望得到类似的输出:

docker volume create nodemodulesnodemodulesdocker-compose -f docker-compose.builder.yml run --rm installnpm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})audited 9731 packages in 21.405sdocker-compose upStarting website_dev_1 ... doneAttaching to website_dev_1dev_1  |dev_1  | > docker-node@1.0.0 start /usr/src/appdev_1  | > parcel src/index.html --hmr-port 1235dev_1  |dev_1  | Server running at http://localhost:1234

Getting the above output means that volume mounting occurred successfully. Open localhost:1234 to confirm that the website can be accessed. This will confirm that you have properly configured the ports. You can edit the source code, for example change the h1 title in App.jsx. As soon as you save the file, the browser page should refresh automatically. This means hot module reloading works from a Docker container.

获得以上输出意味着成功进行了卷安装。 打开localhost:1234以确认可以访问该网站。 这将确认您已经正确配置了端口。 您可以编辑源代码,例如,在App.jsx更改h1标题。 保存文件后,浏览器页面应自动刷新。 这意味着热模块重新加载可从Docker容器中进行。

I would like to bring your attention to the docker-compose.yml file in use. For hot module reloading to work from a Docker Container in Windows requires the following:

我想提醒您注意正在使用docker-compose.yml文件。 为了使热模块从Windows中的Docker容器重新加载 ,需要进行以下操作:

  1. When using parcel, specify HMR port in your package.json start script:

    使用宗地时,在package.json启动脚本中指定HMR端口:

    parcel src/index.html –hmr-port 1235

    包裹src / index.html –hmr-port 1235

  2. In the VM’s Port Forwarding rules, make sure these ports are exposed to the host system:

    在VM的端口转发规则中,确保这些端口公开给主机系统:

    • 1234

      1234
    • 1235

      1235
  3. inotify doesn’t work on vboxsf filesystems, so file changes can’t be detected. The workaround is to set polling for via environment variables in docker-compose.yml. Here’s the full file so that you can see how it’s set:

    inotifyvboxsf文件系统上不起作用,因此无法检测到文件更改。 解决方法是通过docker-compose.yml环境变量设置轮询。 这是完整的文件,以便您可以查看其设置:

    version: '3'services:  dev:    image: node:10-jessie-slim    volumes:      - nodemodules:/usr/src/app/node_modules      - ./:/usr/src/app    working_dir: /usr/src/app    command: npm start    ports:      - 1234:1234      - 1235:1235    environment:    - CHOKIDAR_USEPOLLING=1volumes:  nodemodules:    external: true

Now that we have a fully working implementation of Docker on Windows 10 home, let’s set it up on WSL2 for those who are interested.

既然我们已经在Windows 10 home上实现了Docker的完整工作实现,那么让我们在WSL2上为有兴趣的人设置它。

Windows子系统Linux 2 (Windows Subsystem for Linux 2)

Installing Docker on WSL2 is not as straightforward as it seems. Unfortunately, the latest version of Docker Engine can’t run on WSL2. However, there’s an older version, docker-ce=17.09.0~ce-0~ubuntu, that’s capable of running well in WSL2. I won’t be covering that here. Instead, I’ll show you how to access Docker Engine running in the VM we set up earlier from a WSL terminal.

在WSL2上安装Docker并不像看起来那样简单。 不幸的是,最新版本的Docker Engine无法在WSL2上运行。 但是,有一个较旧的版本docker-ce=17.09.0~ce-0~ubuntu ,可以在WSL2中正常运行。 我不会在这里覆盖。 相反,我将向您展示如何从WSL终端访问在我们先前设置的VM中运行的Docker Engine。

All we have to do is install Docker client and Docker compose. Assuming you’re running WSL2 Ubuntu Terminal, execute the following:

我们要做的就是安装Docker客户端和Docker compose。 假设您正在运行WSL2 Ubuntu Terminal,请执行以下操作:

  1. Install Docker using the official instructions:

    按照官方说明安装Docker:

    # Update the apt package list.sudo apt-get update -y# Install Docker's package dependencies.sudo apt-get install -y \    apt-transport-https \    ca-certificates \    curl \    software-properties-common# Download and add Docker's official public PGP key.curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -# Verify the fingerprint.sudo apt-key fingerprint 0EBFCD88# Add the `stable` channel's Docker upstream repository.## If you want to live on the edge, you can change "stable" below to "test" or# "nightly". I highly recommend sticking with stable!sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) \stable"# Update the apt package list (for the new apt repo).sudo apt-get update -y# Install the latest version of Docker CE.sudo apt-get install -y docker-ce# Allow your user to access the Docker CLI without needing root access.sudo usermod -aG docker $USER
  2. Install Docker Compose using this . An alternative is to use PIP, which will simply install the latest stable version:

    使用此安装Docker Compose。 一种替代方法是使用PIP,它将仅安装最新的稳定版本:

    # Install Python and PIP.sudo apt-get install -y python python-pip# Install Docker Compose into your user's home directory.pip install --user docker-compose
  3. Fix the Docker mounting issue in WSL terminal by inserting this content in /etc/wsl.conf. Create the file if it doesn’t exist:

    通过将此内容插入/etc/wsl.conf解决WSL终端中的Docker挂载问题。 创建文件(如果不存在):

    [automount]root = /options = "metdata"

    You’ll need to restart your machine for this setting to take effect.

    您需要重新启动计算机才能使此设置生效。

  4. Assuming that Linux Docker VM is running, you’ll need to connect the Docker tools in the WSL environment to it. If you can access docker-machine from the Ubuntu terminal, run the eval command. Otherwise, you can insert the following Docker variable in your .bashrc file. Here is an example of mine:

    假设Linux Docker VM正在运行,则需要将WSL环境中的Docker工具连接到它。 如果可以从Ubuntu终端访问docker-machine ,请运行eval命令。 否则,您可以在.bashrc文件中插入以下Docker变量。 这是我的一个例子:

    export DOCKER_HOST="tcp://192.168.99.101:2376"export DOCKER_CERT_PATH="/c/Users/Michael Wanyoike/.docker/machine/machines/vbox"export DOCKER_MACHINE_NAME="vbox"export COMPOSE_CONVERT_WINDOWS_PATHS="true"

    You’ll need to restart your terminal or execute source ~/.bashrc for the settings to take effect. Running Docker commands should work properly in WSL without a hitch.

    您需要重新启动终端或执行source ~/.bashrc才能使设置生效。 运行Docker命令应该可以在WSL中正常运行。

切换到Linux (Switching to Linux)

We’re now coming to the end of this article. The steps for setting up Docker in Windows 10 is a bit of a lengthy process. If you plan to reformat your machine, you’ll have to go through the same process again. It’s worse if your job is to install Docker on multiple machines running Windows 10 Home.

我们现在到本文结尾。 在Windows 10中设置Docker的步骤有点冗长。 如果您打算重新格式化计算机,则必须再次执行相同的过程。 如果您的工作是在运行Windows 10 Home的多台计算机上安装Docker,那会更糟。

A simpler solution is to switch to Linux for development. You can create a partition and set up dual booting. You can also use VirtualBox to install and run a full Linux Distro inside Windows. Check out which you’d like to try out. I use because it’s lightweight and is based on Ubuntu. With VirtualBox, you can try out as many distros as you wish.

一个更简单的解决方案是切换到Linux进行开发。 您可以创建一个分区并设置双重引导。 您还可以使用VirtualBox在Windows内安装和运行完整的Linux Distro。 查看您想尝试的 。 我使用是因为它轻巧并且基于Ubuntu。 使用VirtualBox,您可以尝试任意数量的发行版。

Linux Lite VM

If you’re using a distro based on Ubuntu, you can install Docker easily with these commands:

如果您使用的是基于Ubuntu的发行版,则可以使用以下命令轻松安装Docker:

# Install https://snapcraft.io/ package managersudo apt install snapd# Install Docker Engine, Docker Client and Docker Composesudo snap install docker# Run Docker commands without sudosudo usermod -aG docker $USER

You’ll need to log out and then log in for the last command to take effect. After that, you can run any Docker command without issue. You don’t need to worry about issues with mounting or ports. Docker Engine runs as a service in Linux, which by default starts automatically. No need for provisioning a Docker VM. Everything works out of the box without relying on a hack.

您需要先注销然后登录,以使最后一条命令生效。 之后,您可以运行任何Docker命令而不会出现问题。 您无需担心安装或端口问题。 Docker Engine在Linux中作为服务运行,默认情况下会自动启动。 无需预配Docker VM。 一切都可以直接使用而无需依赖黑客。

摘要 (Summary)

I hope you’ve had smooth sailing installing and running Docker on Windows 10 Home. I believe this technique should work on older versions such as Windows 7. In case you run into a problem, just go through the instructions to see if you missed something. Do note, however, that I haven’t covered every Docker feature. You may encounter a bug or an unsupported feature that requires a workaround, or may have no solution at all. If that’s the case, I’d recommend you just switch to Linux if you want a smoother development experience using Docker.

希望您在Windows 10 Home上安装和运行Docker顺利进行。 我相信该技术应可在Windows 7等较旧的版本上使用。如果遇到问题,请按照说明进行操作,看是否遗漏了一些东西。 请注意,但是,我还没有介绍所有Docker功能。 您可能会遇到需要解决方法的错误或不受支持的功能,或者可能根本没有解决方案。 如果是这种情况,如果您希望使用Docker拥有更流畅的开发经验,我建议您仅切换到Linux。

翻译自:

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

你可能感兴趣的文章
JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(四):自定义T4模板快速生成页面...
查看>>
百度地图的使用
查看>>
第7周进度条
查看>>
BurpSuite学习第六节--Intruder
查看>>
python基础知识
查看>>
PHP变量入门教程(1)基础
查看>>
VMware 关闭虚拟机 Ubuntu 12 的 3D 效果,提高性能
查看>>
数据库文件导出
查看>>
Python字典遍历的几种方法
查看>>
js原生实现三级联动下拉菜单
查看>>
[COGS 2066]七十和十七
查看>>
JS 中对变量类型的五种判断方法
查看>>
学习进度十五
查看>>
解决Android Studio启动项目后一直处于refreshing 'View' gradle project,快速解决亲测有效...
查看>>
4.12 | 学习笔记
查看>>
python开发【第一篇】:基础知识
查看>>
javascript的window.onload()方法和jQuery的$(document).ready()的对比
查看>>
mysql数据库维护(备份和还原)和性能提高
查看>>
第八章 springboot + mybatis + 多数据源
查看>>
Arab and North African Region,2002(Snakes & ladders)
查看>>