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)
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.
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 |
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)
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.
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 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
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 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()
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 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 run
The 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
--
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
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: //
I referred to the following site.
Recommended Posts