zoukankan      html  css  js  c++  java
  • Docker For Windows | Setting Up Docker On Windows

    Docker For Windows | Setting Up Docker On Windows

    7 / 11 Blog from Docker

    If you’re looking for simple and painless software deployment, Docker is the right tool for you. It is the best containerization platform and in this blog on Docker for Windows we’ll specifically focus on how Docker works on Windows.

    To get in-depth knowledge on Docker, you can enroll for live DevOps Certification Training by Edureka with 24/7 support and lifetime access.

    I’ll be covering the following topics in this blog:

    1. Why use Docker For Windows?
    2. Docker for Windows prerequisites 
    3. Components installed with Docker 
    4. What is Docker?
    5. Docker Terminologies
    6. Hands-On

    Why use Docker for Windows?

    • Avoids the work on my machine but doesn’t work on production problem: This problem occurs due to the inconsistent environment throughout the software development workflow. With Docker you can run an application within a container which contains all the dependencies of the application and the container can be run throughout the software development cycle. This practice provides a consistent environment throughout the software development life cycle
    • Improves productivity: By installing Docker on windows we’re running Docker natively. If you’ve been following Docker for a while, you know that Docker containers originally supported only Linux operating systems. But thanks to the recent release, Docker can now natively run on windows, which means that Linux support is not needed, instead the  Docker container will run on the windows kernel itself
    • Supports native networking: Not only the Docker container, the entire Docker tool set is now compatible with windows. This includes the Docker CLI (client), Docker compose, data volumes and all the other building blocks for Dockerizied infrastructure are now compatible with windows. But how is this advantageous? Since all the Docker components are locally compatible with windows, they can now run with minimal computational overhead.

    Docker For Windows Prerequisites

    The following requirements need to be met before installing Docker on windows:

    1. Check if you’re using Windows 10, either pro edition or enterprise edition, 64-bit system. Docker will not run on any other windows version. So if you’re running on an older windows version, you can install the Docker toolbox instead. 
    2. Docker for windows requires a Type-1 hypervisor and in the case of windows, it’s called the Hyper-V. Hyper-V is basically a lightweight virtualization solution build on top of the hypervisor framework. So you don’t need a virtual box, you just have to enable the hypervisor.
    3. And also you need to enable the virtualization in BIOS. Now when you install Docker for windows, by default of this is enabled. But in case you’re facing any issue during installation, please check if your Hyper-V and virtualization is enabled.

    https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
    

    Components Installed With Docker

    1. Docker Engine: When we say Docker, we actually mean Docker engine. The Docker engine contains the Docker daemon, REST API for interacting with the Docker daemon and a command line interface client that communicates with the daemon. Docker daemon accepts Docker commands such as Docker run, Docker build, etc, from the Docker client.
    2. Docker Compose: Docker compose is used to run multiple Docker containers at once by using a single command, which is docker-compose up.
    3. Docker Machine: Docker machine is used to install Docker engine. It is basically what you install on your local system. The Docker machine has it’s own CLI client known as the Docker machine and a Docker engine client called Docker.
    4. Kitematic: Kitematic is an open source project built to simplify the use of Docker on Windows. It helps to automate the installation of Docker and it provides a very interactive user interface for running Docker containers.

    What Is Docker?

    Docker is a containerization platform that runs applications within containers called Docker containers. Docker containers are light weighted when compared to virtual machines. When you install a Virtual machine on your system, it uses the guest operating system on top of your host operating system.  This obviously takes up a lot of resources like disk space, RAM, etc. On the other hand, Docker containers make use of the host operating system itself.

     

     In the above image, you can see that, there’s a host operating system on top of which the Docker engine is mounted. The Docker engine runs container #1 and container #2. Both of these containers have different applications and each application has its own libraries and packages installed within the container.

    I hope you have a good idea about what Docker is if you still have doubts check out this blog to learn more!

    Let’s discuss some Docker terminologies now:

    • Docker Images

    Docker images are read-only templates to build Docker images. Docker images are created from a file called Dockerfile. Within the Dockerfile, you define all the dependencies and packages that are needed by your application.

    • Docker Containers

    Every time you run a Docker image, it runs as a Docker container. Therefore, a Docker container is the run time instance of a Docker image.

    • Docker’s Registry

    Docker’s registry, known as DockerHub is used to store Docker images. These images can be pulled from the remote server and can be run locally. DockerHub allows you to have Public/Private repositories.

    Docker swarm is a technique to create and maintain a cluster of Docker engines. A cluster of Docker engines comprises of multiple Docker engines connected to each other, forming a network. This network of Docker engines is called a Docker swarm. A Docker manager initiates the whole swarm and the other Docker nodes have services running on them. The main goal of the Docker manager is to make sure that the applications or services are running effectively on these Docker nodes.

    Docker compose is used to run multiple containers at once. Let’s say that you have 3 applications in 3 different containers and you want to execute them at once. This is where Docker compose comes in, you can run multiple applications in different containers at once, with a single command, which is docker-compose up.

    Hands-On

    We’ll begin our demo by installing Docker for windows. But before we do that, check if you’ve met the prerequisites.

    One more thing to note is since Docker for Windows requires Microsoft’s Hyper-V and once it’s enabled, VirtualBox will no longer be able to run virtual machines. So you can’t run Docker for Windows and a VirtualBox on the same system, side by side.

    Enough of theory, let’s get our hands dirty and create a simple Python web application by using Docker Compose. This application uses the Flask framework and maintains a hit counter on Redis.

    Flask is a web development framework, written in Python and Redis is an in-memory storage component, used as a database. You don’t have to install Python or Redis, we’re simply going to use Docker images for Python and Redis.

    In this demo we’re going to use Docker compose to run two services, namely:

    1. Web service
    2. Redis service.

    What does the application do? It maintains a hit counter every time you access a web page. So each time you access the website the hit counter gets incremented. It’s simple logic, just increment the value of the hit counter when the web page is accessed.

    For this demo, you’ll need to create four files:

    1. Python file
    2. Requirements.txt
    3. Dockerfile
    4. Docker compose file

    Below is the Python file (webapp.py) that creates an application by using the Flask framework and it maintains a hit counter on Redis. 

     1.

    import time
    import redis
    from flask
    import Flask
    app = Flask(__name__)
    cache = redis.Redis(host = 'redis', port = 6379)
    def get_hit_count():
      retries = 5
    while True:
      try:
      return cache.incr('hits')
    except redis.exceptions.ConnectionError as exc:
      if retries == 0:
      raise exc
    retries -= 1
    time.sleep(0.5)
    @app.route('/')
    def hello():
      count = get_hit_count()
    return 'Hello World! I have been seen {} times.
    '.format(count)
    if __name__ == "__main__":
      app.run(host = "0.0.0.0", debug = True)

    2.The requirements.txt file has the name of the two dependencies, i.e. Flask and Redis that we’ll be installing in the Dockerfile.

    flask
    redis

    3.The Dockerfile is used to create Docker images. Here, I’m installing python and the requirements mentioned in the requirements.txt file.

    How to name Dockerfiles

    Don't change the name of the dockerfile if you want to use the autobuilder at hub.docker.com. Don't use an extension for docker files, leave it null. The filename should just be: (no extension at all)

    Dockerfile
    

    however, you can do like below too...

    dev.Dockerfile, uat.Dockerfile, prod.Dockerfile etc.

    On VS Code you can use <purpose>.Dockerfile and it works accordingly.

    FROM python:3.4-alpine
    ADD . /code
    WORKDIR /code
    RUN pip install -r requirements.txt
    CMD ["python", "webapp.py"]

    4.The Docker compose file or the YAML file contains two services:

    1. Web service: Builds the Dockerfile in the current directory
    2. Redis service: Pulls a Redis image from DockerHub

    务必要注意这里的排版,缩进不能错

    version: '3'
    services:
      web:
        build:
          context: .
          dockerfile: sample.Dockerfile
        ports:
        - "5000:5000"
      redis:
        image: "redis:alpine"

    You can now run these two services or containers by using the following command:

    docker-compose up

    To view the output you can use Kitematic. To open Kitematic, right click on the whale icon on the status bar.

    四个文件的文件名

    Directory: DockerSample

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 6/12/2020 6:16 PM 93 docker-compose.yml
    -a--- 6/12/2020 5:59 PM 12 requirements.txt
    -a--- 6/12/2020 6:11 PM 0 sample.Dockerfile
    -a--- 6/12/2020 5:58 PM 496 webapp.py

    PS C:workspaceEdenredDockerSample> docker-compose up
    Building web
    Step 1/5 : FROM python:3.4-alpine
    3.4-alpine: Pulling from library/python
    8e402f1a9c57: Pull complete cda9ba2397ef: Pull complete aafecf9bbbfd: Pull complete bc2e7e266629: Pull complete e1977129b756: Pull complete Digest: sha256:c210b660e2ea553a7afa23b41a6ed112f85dbce25cbcb567c75dfe05342a4c4b
    Status: Downloaded newer image for python:3.4-alpine
    ---> c06adcf62f6e
    Step 2/5 : ADD . /code
    ---> cf62e95b597f
    Step 3/5 : WORKDIR /code
    ---> Running in 530d310c94db
    Removing intermediate container 530d310c94db
    ---> d84245ac6307
    Step 4/5 : RUN pip install -r requirements.txt
    ---> Running in 2c9f33c97898
    DEPRECATION: Python 3.4 support has been deprecated. pip 19.1 will be the last one supporting it. Please upgrade your Python as Python 3.4 won't be maintained after March 2019 (cf PEP 429).

    Step 5/5 : CMD ["python", "webapp.py"]
    ---> Running in ab484f9d5462

    新版本里面Kitematic已经改名叫做Dashboard了

    redis服务started,但是web服务没有start,查看日志

    日志显示错误是,

    File "webapp.py", line 7

    retries = 5

    ^

    IndentationError: expected an indented block

    看起来是python脚本有问题。

    https://stackoverflow.com/questions/10238770/indentationerror-expected-an-indented-block/38032036#38032036

    发现是python脚本的格式化有问题,用下面的链接进行格式化

    https://codebeautify.org/python-formatter-beautifier   尝试格式化了,还是报错,看来需要简单学习一下python的语法

     

     

  • 相关阅读:
    ASP.NET 2.0的页面缓存功能介绍
    我对针对接口编程的浅解
    接口和抽象类的区别
    面向接口编程到底有什么好处
    泛型编程是什么
    方法的重写、重载及隐藏
    基于事件的编程有什么好处
    Socket Remoting WebService对比
    技术讲座:.NET委托、事件及应用兼谈软件项目开发
    ny589 糖果
  • 原文地址:https://www.cnblogs.com/chucklu/p/13100716.html
Copyright © 2011-2022 走看看