Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port mapping in docker doesn't work for python server

I want to build a server with sagemath, it should take in code, execute it and send back the result. SageMath has a python interface with which I thought this could be achieved. I don't really know python that well but I found a good starting point here https://ask.sagemath.org/question/23431/running-sage-from-other-languages-with-higher-performance/. The problem is that I wanted to run this in a docker container so and just map the port but this doesn't seem to be working.

I changed the python file to adjust it to the newer version:

import socket
import sys
from io import StringIO
from sage.all import *
from sage.calculus.predefined import x
from sage.repl.preparse import preparse


SHUTDOWN = False
HOST = 'localhost'
PORT = 8888
MAX_MSG_LENGTH = 102400

# Create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print('Socket created')

# Bind socket to localhost and port
try:
  s.bind((HOST, PORT))
except (socket.error , msg):
  print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
  sys.exit()

print('Socket bind complete')

# Start listening on socket
s.listen(10)
print('Socket now listening')

# Loop listener for new connections
while not SHUTDOWN:
  # Wait to accept a new client connection
  conn, addr = s.accept()
  print('Connected with ' + addr[0] + ':' + str(addr[1]))

  # Receive message from client
  msg = conn.recv(MAX_MSG_LENGTH)
  if msg:
    if msg == "stop":
      SHUTDOWN = True
    else:
      parsed = preparse(msg)
      if parsed.startswith('load') or parsed.startswith('attach'):
        os.system('sage "' + os.path.join(os.getcwd(), parsed.split(None, 1)[1]) + '"')
      else:
        # Redirect stdout to my stdout to capture into a string
        sys.stdout = mystdout = io.StringIO()

        # Evalutate msg
        try:
          eval(compile(parsed,'<cmdline>','exec'))
          result = mystdout.getvalue() # Get result from mystdout
        except Exception as e:
          result = "ERROR: " + str(type(e)) + " " + str(e)

        # Restore stdout
        sys.stdout = sys.__stdout__

        # Send response to connected client
        if result == "":
          conn.sendall("Empty result, did you remember to print?")
        else:
          conn.sendall(result)

  # Close client connection
  conn.close()

# Close listener
s.close()

then I created a script to run it

#!/bin/bash

/usr/bin/sage -python /var/app/sage-server.py 

afterwards I created the Dockerfile to install SageMath

FROM ubuntu:20.04

COPY src/ /var/app

RUN apt-get update \
    && DEBIAN_FRONTEND="noninteractive" TZ="Europe/London" apt-get -y install tzdata \
    && apt-get install sagemath -y

WORKDIR /var/app
    
RUN ["chmod", "+x", "/var/app/server.sh"]

CMD ["/var/app/server.sh"]

and finally a docker-compose file to automate the mapping and to include it into the main project

version: '3'

services:
  sage-server:
    build: .
    volumes: 
      - ./src:/var/app
    ports:
      - "9999:8888"

All parts execute well and without any error (on an ubuntu). I even see that the port is used when I go into the container but the port 9999 is not used in the host system.

Does any of you have an idea?

like image 850
Philipp M Avatar asked May 07 '26 15:05

Philipp M


1 Answers

As suggested by @David Maze, you need to expose on 0.0.0.0 to be able to be reachable in the container.

HOST = '0.0.0.0'

Then you get some issues:

  • preparse is expecting a str so you need to decode the bytes received:
    msg = conn.recv(MAX_MSG_LENGTH).decode('utf-8')
    
  • StringIO was directly imported so no need to prefix with io.:
    sys.stdout = mystdout = StringIO()
    
  • you need to send back a bytes, but result is an str, so you need to encode:
    conn.sendall(result.encode('utf-8'))
    

The Dockerfile can be improved (https://docs.docker.com/develop/develop-images/dockerfile_best-practices/):

FROM ubuntu:20.04

# First install dependencies so next builds will reuse docker build cache
# Use `--no-install-recommends` to minimize the installed packages
# Clean apt data
RUN apt-get update \
    && DEBIAN_FRONTEND="noninteractive" TZ="Europe/London" apt-get -y install tzdata \
    && apt-get install --no-install-recommends sagemath -y \
    && rm -rf /var/lib/apt/lists/*

# Copy changing data as late as possible to use docker build cache
COPY src/ /var/app
WORKDIR /var/app

# Explicit the listening ports
EXPOSE 8888

# No need for a wrapper script for simple command (and workaround chmod issue)
ENTRYPOINT ["/usr/bin/sage", "-python", "/var/app/sage-server.py"]

As the build-time copy of the source code is overridden by the volume mount at run-time, the permissions have to be right at host side (the build RUN chmod +x has no effect on the run-time mounted files)

like image 135
zigarn Avatar answered May 10 '26 04:05

zigarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!