Put Docker in Windows Home and run a simple web server with Python

Introduction

I wanted to study Docker, so I did everything from Docker Install to running a simple web server with Python in the Docker container in the Windows 10 HOME environment (← important), so I will summarize it. There may be omissions in the procedure, so if something goes wrong, I'd appreciate it if you could comment.

-[Docker on Windows OS](# Docker on Windows-OS) -[Differences between Windows 10 Pro and Home when using Docker](# Differences between Windows 10-Pro and Home when using Docker) -[Procedure to run a simple web server in a Docker container](#Procedure to run a simple web server in a Docker container)

Docker on Windows OS

First of all, Docker runs on LINUX OS. Therefore, to run Docker on Windows, set up a LINUX virtual machine and run the Docker engine on it. This is common to both Windows 10 Pro and Windows 10 Home. The figure below. When running Dokcer on Linux on the left and Windows on the right.

image.png

(https://www.researchgate.net/figure/A-comparison-of-the-architecture-of-virtual-machines-and-Docker-software_fig4_299771559) "

Differences between Windows 10 Pro and Home when using Docker

The bottom line is that Windows 10 Pro uses "Docker Desktop for Windows" and Windows 10 Home uses "Docker Toolbox". The reason for using it properly is that "Docker Desktop for Windows" is premised on using Hyper-V as the hypervisor in the above figure, but Hyper-V cannot be used on Windows 10 Home. Therefore, in Windows 10 Home, install "Docker Toolbox". "Docker Toolbox" uses Oracle's "Virtual Box" as a hypervisor. In summary, it is as shown in the figure below.

Windows 10 Pro Windows 10 Home
Hypervisor Hyper-V Virtual Box (etc.)
Doker Docker Desktop for Windows Docker Toolbox

Steps to run a simple web server inside a Docker container

Based on the above, the procedure to run a simple web server that displays the following pages in the Docker container in the Windows 10 Home environment is described. (There may be omissions) image.png

Docker installation

First, install Docker Toolbox, but the procedure is all described in Note on installing docker on windows 10 home --Qiita, so it is omitted suddenly. To do.

Take a peek inside a VM running in Virtual Box

Next, let's see what's going on inside the VM that runs the Docker container.

python


C:\Users\hogehoge\workspace\pythondocker>docker-machine ssh default
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@default:~$ 

You are now in a VM named default.

Go to the root directory of the VM and view the folders / files stored in the root folder

Go to the root of the VM and view


docker@default:~$ cd /
docker@default:/$ ls
home          mnt           run           tmp
bin           init          opt           sbin          usr
c             lib           proc          squashfs.tgz  var
dev           lib64         sys           etc           linuxrc
root 

The C drive in the actual Windows OS is mounted in the above "c" folder. So, when accessing directly under C on the VM, do something like `` `cd / c / ```.

Go to C drive mounted on VM and view


docker@default:/$ cd /c/ 
docker@default:/c$ ls
Users

exit command when exiting the VM

Get out of the VM


docker@default:/c$ exit
logout
exit status 127

Prepare the structure of folders and files

Prepare folders and files in an appropriate working directory with the following structure.

pythondocker/  :Working directory on the Windows side
 ├ cgi-bin/    :Folder for storing python scripts executed inside Docker container
 │ └ index.py  :Set up a simple web server and publish the page
 └ Dockerfile  :Docker file

Write a Docker file

Create a Docker file as follows.

pythondocker/Dockerfile


# python3.6.Get 9 Docker images
#If you think you have python installed on your Docker image
FROM python:3.6.9
#Specify the working directory in the Docker container
WORKDIR /app
#Windows./cgi-bin folder, app in Docker container/cgi-Copy to bin
#By doing this, cgi-You can access files under bin within Docker
COPY ./cgi-bin /app/cgi-bin
#Inside Docker "python cgi-bin/index.Execute the command "py"
CMD ["python", "cgi-bin/index.py"]

The commands described in this Dockerfile are executed from above when building the Docker image.

Create a python script to publish a web page

Create an "index.py" file in the "python docker / cgi-bin" folder. This is a Python script for setting up a simple web server inside a Docker container and publishing a page on port 8080. When I access the published web server, it says "Hello Python Docker on Widows 10 Home!".

pythondocker/cgi-bin/index.py


import http.server
import socketserver

LISTEN_PORT = 8080

class ServerHandler(http.server.SimpleHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(b"<h1>Hello Python Docker on Widows 10 Home!</h1>")

if __name__ == "__main__":
    HOST, PORT = '', LISTEN_PORT

    with socketserver.TCPServer((HOST, PORT), ServerHandler) as server:
        server.serve_forever()

Build Docker image

Next, build a Docker image based on the created Docker file. Launch an appropriate console (command prompt, etc.), move to the pythondocker folder, and

#Move
C:\Users\hogehoge\workspace>cd pythondocker
#Check with dir
C:\Users\hogehoge\workspace\pythondocker>dir
The volume label on drive C is Windows
Volume serial number is 1273-2EFA

 C:\Users\hogehoge\workspace\pythondocker directory

2020/05/05  17:03    <DIR>          .
2020/05/05  17:03    <DIR>          ..
2020/05/05  17:00    <DIR>          cgi-bin
2020/05/06  16:05               581 Dockerfile
1 file 581 bytes
3 directories 151,054,426,112 bytes of free space

Run the docker build command to build the Docker image

Docker image build command


C:\Users\hogehoge\workspace\pythondocker>docker build --tag=hellopython .
# --tag :Image tag
# .     :Shows the path to the folder where the Docker files are located

Success if the following output is output

Output of build command


Sending build context to Docker daemon  4.608kB
Step 1/4 : FROM python:3.6.9
3.6.9: Pulling from library/python
16ea0e8c8879: Pull complete
50024b0106d5: Pull complete
ff95660c6937: Pull complete
9c7d0e5c0bc2: Pull complete
29c4fb388fdf: Pull complete
49a8841b38a3: Pull complete
c4a74b0ecce5: Pull complete
3e19e7d95a0c: Pull complete
c6c6b4054fe6: Pull complete
Digest: sha256:47e547af7ffcc2f5d1e5c76a96f4122943c4631d10bb3a0375e870fd95799107
Status: Downloaded newer image for python:3.6.9
 ---> 5bf410ee7bb2
Step 2/4 : WORKDIR /app
 ---> Running in 79bc620b5bcb
Removing intermediate container 79bc620b5bcb
 ---> 253f4f561100
Step 3/4 : COPY ./cgi-bin /app/cgi-bin
 ---> 2a4c59e6f250
Step 4/4 : CMD ["python", "cgi-bin/index.py"]
 ---> Running in f85f3c374389
Removing intermediate container f85f3c374389
 ---> 9f8b8e3b53d2
Successfully built 9f8b8e3b53d2
Successfully tagged hellopython:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

There is a security warning, but Ignore it for the time being.

Check if the built Docker image is created properly

Check docker image


C:\Users\hogehoge\workspace\pythondocker>docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hellopython         latest              8a31f9eb38e3        33 minutes ago      913MB

Run the built Docker image

Run the Docker image you just built and run your web server inside the Docker container. Run the Docker image hellopython built with the command `` `docker run```.

Run Docker image


docker run --rm -p 4000:8080 -v /c/Path from C drive to python docker folder/cgi-bin/:/app/cgi-bin/ -d hellopython

docker runThe command options are described below.

---- rm: Option to stop = delete --Option to automatically delete the container when the container is stopped --- p: Port forwarding options --Port forwarding. A web server published on port 8080 on Docker can be accessed from port 4000 on VM. --- v: Volume mount options --: format --The path on the VM is "/ c / Users / hogehoge / workspace / pythondocker / cgi-bin /" in my case. (By the way, it is "C: \ Users \ hogehoge \ workspace \ pythondocker \ cgi-bin" on Windows OS) --If you don't know the path on the VM, see [Peek inside the VM running in Virtual Box](# Peek inside the VM running in Virtual-Box) on this page. --- d: Specify the Docker image to execute --Nothing in particular

Check the running Docker Container for the time being

Docker container check


C:\Users\hogehoge\workspace\pythondocker>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES 
eb3bf047d160        9f8b8e3b53d2        "python cgi-bin/inde…"   About an hour ago   Up About an hour    0.0.0.0:4000->8080/tcp   epic_jang

Access a web server running inside a Docker container

Finally, access the web server running inside the Docker container and display it on your browser. First, check the IP of the VM running the Docker container.

VM IP confirmation


C:\Users\hogehoge\workspace\pythondocker>docker-machine ip
192.168.99.100

After that, if you access "http: // : 4000" on your browser, the web page should be displayed as shown below. (In my case http://192.168.99.100:4000/)

image.png

Referenced site

I referred to the following site.

Recommended Posts

Put Docker in Windows Home and run a simple web server with Python
Start a simple Python web server with Docker
Run a Python web application with Docker
Launch a web server with Python and Flask
Run a simple algorithm in Python
Create a simple Python development environment with VS Code and Docker
Rock-paper-scissors with Python Let's run on a Windows local server for beginners
Set up a simple HTTPS server in Python 3
Set up a simple SMTP server in Python
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Until you install Python with pythonbrew and run Flask on a WSGI server
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Make a simple Slackbot with interactive button in python
Let's make a simple game with Python 3 and iPhone
Launch a Python web application with Nginx + Gunicorn with Docker
Run a Python file with relative import in PyCharm
Create a fake Minecraft server in Python with Quarry
[Vagrant] Set up a simple API server with python
Run python with PyCharm (Windows)
Hello World is a simple web server that follows WSGI (Web Server Gateway Interface) in Python.
Until you create a machine learning environment with Python on Windows 7 and run it
[Building a CI environment in 2 hours] Procedure for building a Python Web server with CircleCI and passing an HTTP communication test
I made a simple typing game with tkinter in Python
Create a simple Python development environment with VSCode & Docker Desktop
Library for specifying a name server and dig with python
How to start a simple WEB server that can execute cgi of php and python
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
Hit a command in Python (Windows)
Put MeCab in "Windows 10; Python3.5 (64bit)"
Creating a Flask server with Docker
Until you put Python in Docker
Implementing a simple algorithm in Python 2
I made a simple circuit with Python (AND, OR, NOR, etc.)
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a detonation velocity website with Cloud Run and Python (Flask)
Make a simple OMR (mark sheet reader) with Python and OpenCV
Draw a watercolor illusion with edge detection in Python3 and openCV3
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
LaTeX and R (a little Python) environment construction with SublimeText3 (Windows)
Run the output code on the local web server as "A, pretending to be B" in python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Creating a simple PowerPoint file with Python
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Run servo with Python on ESP32 (Windows)
A simple HTTP client implemented in Python
I put Alpine Linux in Larkbox and made it my home server
Using venv in Windows + Docker environment [Python]
Set up a Samba server with Docker
Try drawing a simple animation in Python
Put OpenCV in OS X with Homebrew and input / output video with python
Create a simple GUI app in Python
Let's throw away JavaScript and write a web front end in Python!
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
[Python] [Windows] Take a screen capture in Python
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Daemonize a Python web app with Supervisor
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
Run the Python interpreter in a script
I set the environment variable with Docker and displayed it in Python
Find out with Python and Stan how old a baseball player can hit a home run the most