issue with service comunicating

hello i have an issue with my project, i have created a docker-compose with multiple service in it, the service are:

  • chromium-service → a container with chromium installed that at the start of the container open a window of chromium in kiosk mode at a specified url
  • frontend → script python, not developed by me, but one of my colleagues, that host a webpage for the frontend of our project
  • elastic → container that run an instance of elasticsearch to which the frontend connect
    below a copy of my docker-compose file
version: "2"
services:
  chromium-service:
    build: 
      context: ./chromium
      dockerfile: Dockerfile
    privileged: true
    container_name: chromium
    volumes:
      - "resin-data:/data"
    labels:
      io.balena.features.dbus: "1"
    depends_on:
      - frontend
    environment:
      DBUS_SYSTEM_BUS_ADDRESS: "unix:path=/host/run/dbus/system_bus_socket"
      UDEV: 1
      BALENA_DEVICE_UUID: 7840854e8e0584ea8029d332cf0ce0b1
      URL_LAUNCHER_URL: http://frontend:5000
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - "8000:5000"
    container_name: frontend
    devices:
      - "/dev/video0:/dev/video0"
      - "/dev/video1:/dev/video1"
      - "/dev/video19:/dev/video19"
    depends_on:
      - elastic
  elastic:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.12.0
    ports:
      - "9200:9200"
      - "9300:9300"
    stdin_open: true
    container_name: elastic
    tty: true
    mem_limit: 1g
    environment:
      - ELASTICSEARCH_USERNAME=elastic
      - ELASTICSEARCH_PASSWORD=jhclitw460-27DjqIlD*
volumes:
  resin-data:

the problem is even thought I’m quite sure the value im passing as URL_LAUNCHER_URL is correct, when the python script start, from chromium I’m unable to see the frontend hosted by the frontend container, while if i type for example https://youtube.com as URL_LAUNCHER_URL chromium open without problem the web page.
Can someone help me understand the cause of the error and help me resolve it?

Hello @giacomo00 did you try with localhost:5000? What result do you get?

i tried and i got the usal page chromium show when it cannot reach a site
it says:
This site can’t be reached
localhost refused to connect

Do you get the same error message when you use frontend:500?

yes, if it could be of any help im going to share the 2 dockerfile im using in the docker-compose:
chromium service dockerfile:

FROM balenalib/raspberrypi4-64-debian-python:3.10-sid-build

ENV container docker

# Install necessary dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    apt-utils \
    curl \
    dbus \
    wget \
    xserver-xorg-core \
    xserver-xorg-legacy \
    xserver-xorg-input-all \
    xserver-xorg-video-fbdev \
    x11-xserver-utils \
    xorg \
    libxcb-image0 \
    libxcb-util0-dev \
    xdg-utils \
    libdbus-1-dev \
    libcap-dev \
    libxtst-dev \
    libxss1 \
    lsb-release \
    fbset \
    systemd-sysv \
    libexpat-dev \
    libgles2-mesa \
    libsdl2-dev \
    chromium \
    nodejs \
    npm \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

# Install serve globally
RUN npm install -g serve

# Set up X11 configuration
RUN echo "#!/bin/bash" > /etc/X11/xinit/xserverrc \
    && echo "" >> /etc/X11/xinit/xserverrc \
    && echo 'exec /usr/bin/X -s 0 dpms -nocursor -nolisten tcp "$@"' >> /etc/X11/xinit/xserverrc

# Mask unnecessary systemd services
RUN systemctl mask -- \
    dev-hugepages.mount \
    sys-fs-fuse-connections.mount \
    sys-kernel-config.mount \
    display-manager.service \
    getty@.service \
    systemd-logind.service \
    systemd-remount-fs.service \
    getty.target \
    graphical.target \
    systemd-journald.service \
    systemd-udevd.service

# Copy entry script and systemd service file
COPY entry.sh /usr/bin/entry.sh
COPY balena.service /etc/systemd/system/balena.service

# Remove executable permission from systemd service file
RUN chmod -x /etc/systemd/system/balena.service

# Enable the systemd service
RUN systemctl enable /etc/systemd/system/balena.service

# Set the stop signal
STOPSIGNAL 37

# Set the entry point
ENTRYPOINT ["/usr/bin/entry.sh"]

# Set the working directory
WORKDIR /usr/src/app

# Copy the application files
COPY ./app ./

# Install pm2 globally
#RUN npm install -g pm2

# Start app using pm2
CMD ["bash" "start.sh"]

frontend dockerfile

FROM python:3.10

# Install system dependencies
RUN apt-get update && apt-get install -y libgl1-mesa-glx libatomic1

RUN pip install --upgrade tensorflow

# Upgrade pip
RUN pip install --upgrade pip
RUN usermod -aG video root


# Copy requirements and install dependencies
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt

# Copy the rest of the application code
COPY . /app

# Expose the Flask port
EXPOSE 5000
EXPOSE 8000

# Set environment variable to avoid issues with TensorFlow
ENV LD_PRELOAD /usr/lib/aarch64-linux-gnu/libatomic.so.1

# Run the Flask application
CMD [ "flask", "run" ]

@giacomo00
Try putting the URL http://frontend:8000 or http://ip_address_of_your_balena_device:8000.
The only problem I can see is you have mapped your frontend container port 5000 to the host’s port 8000. I could be wrong though.
One thing you can do in your docker-compose is run the chromium container after your frontend container has started to ensure that when the chromium browser opens the URL, your website server has started executing.
If anyone finds a mistake in my answer please point it out.
Thank you

1 Like

same result, one question, because i dont know a thing about python, is it possible that even if it print this:

Directory /root/.deepface created
Directory /root/.deepface/weights created
* Ignoring a call to 'app.run()' that would block the current 'flask' CLI command.
Only call 'app.run()' in an 'if __name__ == "__main__"' guard.
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000

it isnt really running?

@giacomo00
Binding Address : The Flask server is running on http://127.0.0.1:5000 . This IP address (127.0.0.1 ) refers to localhost within the container, not the host machine. To make the Flask application accessible from outside the container, you need to bind it to 0.0.0.0 instead. This makes the server listen to all public IPs, allowing access from outside the container.
App.run() in Flask: The warning about “app.run() in a Flask CLI command” is important. Ensure that you start your Flask app properly. It should be something like this:
if name == “main”:
app.run(host=‘0.0.0.0’, port=5000)
This ensures that app.run() is only called when you’re running the script directly and not on import. Also, setting host=‘0.0.0.0’ makes the server accessible externally.

where do i have to specify the host=‘0.0.0.0’?
as i said i dont know a thing of python and the script wasnt done by me, but from one of my colleagues, i have found only a 127.0.0.1 in the frontend script and it is in app.js, is it there where i have to put 0.0.0.0 or its in another point?

@giacomo00
can you show me what’s inside of your app.js file and the folder structure inside of your frontend folder

this is the structure
Screenshot 2024-01-30 at 15.45.21

from app folder → static → js → app.js

$(document).ready(function() {
    count = 0; // set counter to get next page 
    user_name = "";
    console.log("ready");
    // sending a connect request to the server.
    var socket = io.connect('http://127.0.0.1:5000');
    
    const urlParams = new URLSearchParams(window.location.search);
    user_name = urlParams.get('user');
    $('#found_person').text(user_name);


    socket.on('device_found', function(msg) {
        // update UI with single person
        // TODO: add multiple people scanned
        html = "<div class=\"frame\">"
        + "<img class=\"vuesax-bold-profile\" src=\"./static/img/vuesax-bold-profile-circle.svg\" />"
        + "<div class=\"frame-2\">"
        + "<div class=\"text-wrapper-2\">" + msg['title_name'] + "</div>"
        +  "<div class=\"reach\">" + msg['distance'] + " m</div></div></div>"

        $('#person').html(html);

        
        if(msg['distance'] < 1){
            if(count > 3){
                count = 0; // reset count
                window.location.href = "/face_scan?user="+msg['title_name']; // move forward if still approaching
            } else{
                count++; // increment count if still in range
            }
        } else {
            count = 0; // reset count if moved away
        }
    });
    

    socket.on('person', function(name) {
        if(name == user_name){
            if(count > 3){
                count = 0; // reset count
                window.location.href = "/open?user="+user_name; // move forward if still approaching
            } else{
                count++; // increment count if still in range
            }
        } else {
            count = 0; // reset count if moved away
        }
    });
  });

@giacomo00
You can change this in your python dockerfile and also change every occurrence of 127.0.0.1:5000 to 0.0.0.0:5000 inside your frontend folder

change this to
CMD [“flask”, “run”, “–host=0.0.0.0”, “–port=8000”]
Before doing any changes please save a copy of your original file if something goes wrong

i made the change only in te code, not dockerfile, because returned an error, but still give frontend refused to connect

@giacomo00
what error the dockerfile is giving.
without change in the dockerfile it won’t work.