1

I've got a python docker container that starts in port 8000. This process, after a user interaction, starts, in the same container, the h20 java process in background in the port 54321.

Both ports are exposed in the Dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
RUN apt update && apt install -y default-jre
EXPOSE 8000
EXPOSE 54321
COPY . /code/

Both ports are mapped in docker-compose.yml:

  backend:
    image: xxxxx/xxx-backend
    build: backend/xxx/.
    ports:
      - "8000:8000"
      - "54321:54321"
    environment:
      - "KEY: value"
    command: python manage.py runserver 0.0.0.0:8000
    depends_on:
      - db
    networks:
      - xxx

The problem is:

When the container runs, port 8000 is mapped, but when I start the h2o server (it's python h2o.init()), the server starts and it's up and running (and it is accesible from lynx inside the container to localhost:54321), but the mapping doesn't work. Seems that 54321 port is not mapped at all because it's not in the CMD or in the startup process.

Is there a solution for map ports even if the process is not started on container startup?

Update

This is my docker-compose ps:

     Name                   Command                State                          Ports                      
-------------------------------------------------------------------------------------------------------------
some_container_1   python manage.py runserver ...   Up         0.0.0.0:54321->54321/tcp, 0.0.0.0:8000->8000/tcp
Alfonso Tienda
  • 3,442
  • 1
  • 19
  • 34
  • Can you run that process in a separate container? When it starts, does it bind to the container-private localhost interface or to 0.0.0.0? (If it prints out any diagnostics, it will often say something like "listening on 0.0.0.0:54321" in the good case.) There's nothing special about the container process listening immediately or taking a minute or two. – David Maze Jun 08 '20 at 13:17
  • The process runs ok in a separate container. And the port is mapped (see update), but when the process starts doesn't works – Alfonso Tienda Jun 08 '20 at 13:25

1 Answers1

0

One thing you could try is cheating by bypassing the containerization for networking by using:

network_mode: "host"

Here is a different stackoverflow article with a discussion about that:

and a link to the compose documentation:

So, try this:

backend:
    image: xxxxx/xxx-backend
    build: backend/xxx/.
    network_mode: "host"
    environment:
      - "KEY: value"
    command: python manage.py runserver 0.0.0.0:8000
    depends_on:
      - db
    networks:
      - xxx
TomKraljevic
  • 3,661
  • 11
  • 14