Try docker: Create your own container image for your Python web app

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.

1) Pull the base image and start it

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.

2) Create a sample web app

Create a sample web app anywhere in the container

vi sampleweb.py
import SimpleHTTPServer
SimpleHTTPServer.test()

3) Save the edited container image (commit)

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

4) Start the web application when starting the container

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 ./

5) Set port forwarding and start the container

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.

6) Check connection

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

Try docker: Create your own container image for your Python web app
Create your own Big Data in Python for validation
Recommended container image for Python applications
[LLDB] Create your own command in Python
[For beginners] Try web scraping with Python
Try Python interactive mode inside a Docker container
python: Use your own class for numpy ndarray
[For recording] Keras image system Part 1: How to create your own data set?
The road to updating Splunkbase with your own Splunk app for Python v2 / v3
Create your own exception
Memo to create your own Box with Pepper's Python
Make your python CUI application an app for mac
Create your own Random Dot Stereogram (RDS) in Python.
Try to improve your own intro quiz in Python
[Python] 2 Create a risk-return map for your asset portfolio
[Blender × Python] Create your own function & summary so far
Create a Layer for AWS Lambda Python with Docker
Try sorting your own objects with priority queue in Python
Try a similar search for Image Search using the Python SDK [Search]
How to implement Python EXE for Windows in Docker container
Set up your own web server within your Pepper app project
Try using Jupyter's Docker image
Create your own Django middleware
Qt for Python app self-update
Create your own graph structure class and its drawing in python
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
Expose your .gitignore
Jupyter Docker image summary
Try using Jupyter's Docker image
Generate a Docker image using Fabric
Run TensorFlow Docker Image on Python3
Docker image that can use cx_Oracle
Try docker: Create your own container image for your Python web app
How to write Docker base image
Use cryptography library cryptography with Docker Python image
Dockerfile: Install Docker on your Linux server
[Golang] Create docker image with Github Actions
[Python] Make your own LINE bot
Azure Functions: Try Durable Functions for Python
Web teaching materials for learning Python
How to create your own Transform
Try implementing k-NN on your own
Run TensorFlow Docker Image on Python3
Personal notes for python image processing
Create your own name resolution service
[Django] Create your own 403, 404, 500 error pages
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
Create your own virtual camera with Python + OpenCV and apply original effects
A memo when setting up a Docker container for using JUMAN ++, KNP, python
How to access data with object ['key'] for your own Python class