I want to containerize python programs that launch web servers in Docker

Asked 2 years ago, Updated 2 years ago, 47 views

Python has a module called Blockext that generates an S2e file on Scratch 2.0, which can generate extended blocks.You can then start a local server where you can download the s2e file.Here's the source code:

[test.py]

 from blockext import run, reporter, command
message=""
@ command("set message%s")
default_message(m):
    global message
    message = m
@ reporter ("get message")
default_message():
    return message
if__name__=="__main__":
    run("Ex Test", "ex_test", 5678)

I would like to containerize this python program with docker so that several people can use this expansion block through the container.
However, I think I can connect to the container server, but I can't connect to the Python program server.The Python program works well within the container.It looks like the picture below.

Enter a description of the image here

How can I connect the browser to the python program server?
Please give me some advice.

Dockerfile

 FROM python: 3.7.3
COPY test.py/app/
WORKDIR/app
RUN apt-get update
RUN apt-get install-y vim
RUN apt-get install-y curl
EXPOSE 80
CMD ["python", "test.py"]

python docker

2022-09-30 16:42

2 Answers

For socket communication with HTTP communication,

  • Port number to listen to
  • Listening IP Address

The information in is required.The port number can be any number that is not currently used by the machine.
For the following IP addresses, this sets which network interface the machine has to allow communications:Most machines have two network interfaces.

  • lo.127.0.0.1 called localhost or loopback address.
  • eth0.Network interface on the side that is connected to another computer.I think it's good to think of it as a LAN port.This interface is usually class B private network address such as 172.17.x.x.

You can think of docker containers as having these two network interfaces. I don't think lo needs any specific explanation. eth0 is the interface for communicating outside the container.

In order to create a socket and be able to communicate from the outside (called bind or listen, etc.), it is necessary to decide which network interface to allow communication.You can either listen from one or all network interfaces.If so, specify 0.0.0.0 as the standby IP address.

If you specify a standby address with localhost, you can only accept communication from the computer itself.The same goes for containers, which only accepts communication from themselves.
If you want to accept communications from outside the container, you will often listen with 0.0.0.0.This setting and the -p option when docker run allows the container to communicate with the host by linking the port number of the container to the port number of the host.This is the basis of docker.

Now, the blockext used in this program starts the web server on the specified port with the run method, but the standby IP address is hardcoded with localhost.Therefore, we only accept communications from non-containers themselves.There seems to be no way to set the IP address from the module user side.
Therefore, it is unfortunately impossible to use docker to move blockext unless you modify the module

If you look at the blockext's Github repository, it says DEPRECATED.In other words, the development has been abandoned and should no longer be used.

Unfortunately, I think it would be more productive to look for other ways.
Or will you fork the repository and take over the development yourself?


2022-09-30 16:42

Since the program is waiting for 5678, the in-container port used for communication must also be 5678.

See if 8080:5678 connects.


2022-09-30 16:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.