Flow to run your own Python web application with docker
Ideally, all docker images should be written in dockerfile, but try creating them with manually set images.
docker run -t -i centos /bin/bash
The -i -t option and / bin / bash are for logging in to the container started with standard input. Based on centos.
Put it in the centos container with this command
Make sure python is already installed in the container with the python -V command.
Create a sample web app anywhere in the container
vi sampleweb.py
import SimpleHTTPServer
SimpleHTTPServer.test()
You can output the running container as a docker image with the docker commit command.
docker commit [Edited container ID] [Container name to save]
Example)
docker commit aeijdire845 simpleweb
I was addicted to it here. In linux, if you write a command in rc.local, it seems that it will execute the intended command at startup, but it will not be reflected in the container. It is necessary to describe in ENTRYPOINT (or CMD) of dockerfile. At this point, I thought I should start over with the dockerfile, but I only need to add ENTRYPOINT based on the images created up to 3.
Example)
from simpleweb
ENTRYPOINT python /home/hoge/simpleweb.py
Create an image with docker build based on the docker file created continuously
docker build -t [Image name]:[Tag name] [Directory path with Dockerfile]
Example)
docker build -t simpleweb2:latest ./
Since it is a web application, it is necessary to set options and start it so that the web application in the container can be accessed from the outside.
docker run -p 80:8000 simpleweb2
With the -p option, specify port forwarding in [External port]: [Intra-container port]. In the example, if you access with 80 from the outside, it will be connected to 8000 in the container. The web application created in 2 waits at 8000, so set it like this.
If you access from local http: // localhost /, if you have an external IP, specify that IP and access it, Directory listing will be returned.
Recommended Posts