Home > Mobile >  How to make docker container running continuously?
How to make docker container running continuously?

Time:10-05

I have a Docker image that is actually a server for a device. It is started from a Python script, and I made .sh to run it. However, whenever I run it, it says that it is executed and it ends (server exited with code 0). The only way I made it work is via docker-compose when I run it as detached container, then enter the container via bin/bash and execute the run script (beforementioned .sh) from it manually, then exit the container.

After that everything works as intended, but the issue arises when the server is rebooted. I have to do it manually all over again.

Did anyone else experience anything similar? If yes how can I fix this?

File that starts server (start.sh):

#!/bin/sh
python source/server/main.pyc &
python source/server/main_socket.pyc &
python source/server/main_monitor_server.pyc &
python source/server/main_status_server.pyc &
python source/server/main_events_server.pyc &

Dockerfile:

FROM ubuntu:trusty

RUN mkdir -p /home/server

COPY server /home/server/

EXPOSE 8854

CMD [ /home/server/start.sh ] 

Docker Compose:

version: "3.9"
services:
  server:
    tty: yes
    image: deviceserver:latest
    container_name: server
    restart: always
    ports:
      - "8854:8854"
    deploy:
      resources:
        limits:
          memory: 3072M

CodePudding user response:

It's not a problem with docker-compose. Your docker container should not return (i.e block) even when launched with a simple docker run.

For that your CMD should run in the foreground.

I think the issue is that you're start.sh returns instead of blocking. Have you tried to remove the last '&' from your script (I'm not familiar with python and what these different processes are)?

CodePudding user response:

You can add sleep command in the end of your start.sh.

#!/bin/sh
python source/server/main.pyc &
python source/server/main_socket.pyc &
python source/server/main_monitor_server.pyc &
python source/server/main_status_server.pyc &
python source/server/main_events_server.pyc &
while true
do
  sleep 1;
done
  • Related