Get started with Docker Windows 10 & Hyper-V note

写在前面。

本文是Docker 初学者在阅读官方文档并尝试在本机安装Docker过程的笔记,相当于复习了。 希望能减少初学者在阅读官网文档耗费的时间。

1. Read the documentation!!! 仔细阅读官方文档。 如果遇到问题,将官方文档多阅读几次。或者暂时放下问题将文档通读后再次尝试。 


2. 笔者安装环境为Win 10 Pro 英文版, 且有翻墙工具。如果遇墙可能不处理方式不同。 请百度怎么翻墙。

官方文档地址: 

https://docs.docker.com/get-started


2. 下载 Docker for Windows,下载地址如下:

https://store.docker.com/editions/community/docker-ce-desktop-windows


3. 安装 Docker for Windows

双击安装下载好的 .exe file. 如果系统不曾安装过Hyper-V。 在安装Docker 成功后请按照提示安装Hyper-V。期间系统会重启,请保存好个人文件。

 

3. 注册官方的Docker Cloud。在官方的文档中会需要账号。

注册地址: 

https://hub.docker.com/


4. 安装成功后,打开Hyper-V Manager 将看到一个虚拟机“MobyLinuxVM”。


5. 建议将PowerShell 快捷方式固定在开始菜单中, 每次运行使用管理员权限运行  


5. 以下文档复制于官网文档, 只会在遇到问题的地方特殊标注遇到的问题和解决办法。 


Part 1

Welcome! We are excited you want to learn how to use Docker.

In this six-part tutorial, you will:

  1. Get set up and oriented, on this page.
  2. Build and run your first app
  3. Turn your app into a scaling service
  4. Span your service across multiple machines
  5. Add a visitor counter that persists data
  6. Deploy your swarm to production

The application itself is very simple so that you are not too distracted by what the code is doing. After all, the value of Docker is in how it can build, ship, and run applications; it’s totally agnostic as to what your application actually does.

Prerequisites

While we’ll define concepts along the way, it is good for you to understand what Docker is before we begin.

We also need to assume you are familiar with a few concepts before we continue:

  • IP Addresses and Ports
  • Virtual Machines
  • Editing configuration files
  • Basic familiarity with the ideas of code dependencies and building
  • Machine resource usage terms, like CPU percentages, RAM use in bytes, etc.

Finally, though we’ll remind you again when you need these things, you can save yourself some distraction at that time by signing up for a Docker ID and using it on your local machine by running the following command:

docker login

A brief explanation of containers

An image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

container is a runtime instance of an image—what the image becomes in memory when actually executed. It runs completely isolated from the host environment by default, only accessing host files and ports if configured to do so.

Containers run apps natively on the host machine’s kernel. They have better performance characteristics than virtual machines that only get virtual access to host resources through a hypervisor. Containers can get native access, each one running in a discrete process, taking no more memory than any other executable.

Containers vs. virtual machines

Consider this diagram comparing virtual machines to containers:

Virtual Machine diagram

Virtual machine stack example

Virtual machines run guest operating systems—note the OS layer in each box. This is resource intensive, and the resulting disk image and application state is an entanglement of OS settings, system-installed dependencies, OS security patches, and other easy-to-lose, hard-to-replicate ephemera.

Container diagram

Container stack example

Containers can share a single kernel, and the only information that needs to be in a container image is the executable and its package dependencies, which never need to be installed on the host system. These processes run like native processes, and you can manage them individually by running commands like docker ps—just like you would run ps on Linux to see active processes. Finally, because they contain all their dependencies, there is no configuration entanglement; a containerized app “runs anywhere.”

Setup

Before we get started, make sure your system has the latest version of Docker installed.

Install Docker

Note: version 1.13 or higher is required

You should be able to run docker run hello-world and see a response like this: [打开PowerShell并执行docker run hello-world 以后文中所有命令行都运行在PowerShell 中且用管理员权限执行。]

Note: You may need to add your user to the docker group in order to call this command without sudo. Read more

Note: If there are networking issues in your setup, docker run hello-world may fail to execute successfully. In case you are behind a proxy server and you suspect that it blocks the connection, check the next part of the tutorial.

$ docker run hello-world

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

To generate this message, Docker took the following steps:
...(snipped)...

Now would also be a good time to make sure you are using version 1.13 or higher. Run docker --version to check it out.

$ docker --version
Docker version 17.05.0-ce-rc1, build 2878a85

If you see messages like the ones above, you are ready to begin your journey.

Part 2

Prerequisites

Introduction

It’s time to begin building an app the Docker way. We’ll start at the bottom of the hierarchy of such an app, which is a container, which we cover on this page. Above this level is a service, which defines how containers behave in production, covered in Part 3. Finally, at the top level is the stack, defining the interactions of all the services, covered in Part 5.

  • Stack
  • Services
  • Container (you are here)

Your new development environment

In the past, if you were to start writing a Python app, your first order of business was to install a Python runtime onto your machine. But, that creates a situation where the environment on your machine has to be just so in order for your app to run as expected; ditto for the server that runs your app.

With Docker, you can just grab a portable Python runtime as an image, no installation necessary. Then, your build can include the base Python image right alongside your app code, ensuring that your app, its dependencies, and the runtime, all travel together.

These portable images are defined by something called a Dockerfile.

Define a container with Dockerfile

Dockerfile will define what goes on in the environment inside your container. Access to resources like networking interfaces and disk drives is virtualized inside this environment, which is isolated from the rest of your system, so you have 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 will behave exactly the same wherever it runs.

Dockerfile

Create an empty directory. Change directories (cd) into the new directory, create a file called Dockerfile, copy-and-paste the following content into that file, and save it. Take note of the comments that explain each statement in your new Dockerfile.

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Are you behind a proxy server?[本人使用代理服务器翻墙 因此在dockfile中需要添加以下的部分。]

Proxy servers can block connections to your web app once it’s up and running. If you are behind a proxy server, add the following lines to your Dockerfile, using the ENV command to specify the host and port for your proxy servers:

# Set proxy server, replace host:port with values for your servers
ENV http_proxy host:port
ENV https_proxy host:port

Add these lines before the call to pip so that the installation succeeds.

This Dockerfile refers to a couple of files we haven’t created yet, namely app.py and requirements.txt. Let’s create those next.

The app itself

Create two more files, requirements.txt and app.py, and put them in the same folder with the Dockerfile. This completes our app, which as you can see is quite simple. When the above Dockerfile is built into an image, app.py and requirements.txt will be present because of that Dockerfile’s ADD command, and the output from app.py will be accessible over HTTP thanks to the EXPOSE command.

requirements.txt

Flask
Redis

app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

Now we see that pip install -r requirements.txt installs the Flask and Redis libraries for Python, and the app prints the environment variable NAME, as well as the output of a call to socket.gethostname(). Finally, because Redis isn’t running (as we’ve only installed the Python library, and not Redis itself), we should expect that the attempt to use it here will fail and produce the error message.

Note: Accessing the name of the host when inside a container retrieves the container ID, which is like the process ID for a running executable.

That’s it! You don’t need Python or anything in requirements.txt on your system, nor will building or running this image install them on your system. It doesn’t seem like you’ve really set up an environment with Python and Flask, but you have.

Build the app

We are ready to build the app. Make sure you are still at the top level of your new directory. Here’s what ls should show:

$ ls
Dockerfile		app.py			req
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值