0

Good day! I have a microservice that runs in a windower and a registry that stores the address of the microservices. I also have a script that runs when the container is turned on. The script gets its local ip and sends it to another server using curl. After executing the script, code 0 is returned and the container exits. How can you fix this problem?

#docker-compose realtime logs

nginx_1        | "code":"SUCCESSFUL_REQUEST" nginx_1 exited with code 0

My bash script

#!/bin/bash
address=$(hostname -i)
curl -X POST http://registry/service/register -H 'Content-Type: application/json' -d '{"name":"'"$MICROSERVICE_NAME"'","address":"'"$address"'"}'

The script runs fine and no problem, but unfortunately it breaks the container process. Is it possible to somehow intercept this code so that it does not shut down the container?

I would be grateful for any help or comment!

EDIT:

Dockerfile here the script is called after starting the container

FROM nginx:1.21.1-alpine
WORKDIR /var/www/
COPY ./script.sh /var/www/script.sh
RUN apk add --no-cache --upgrade bash && \
    apk add nano 

#launch script
CMD /var/www/script.sh

EDIT 2:

my docker-compose.yml

version: "3.9"

services:
    #database
    pgsql:
        hostname: pgsql
        build: ./pgsql
        ports:
            - 5432:5432/tcp
        volumes:
            - ./pgsql/data:/var/lib/postgresql/data
      

    #registry
    registry_fpm:
        build: ./fpm/registry
        depends_on:
            - pgsql
        volumes:
            - ./microservices/registry:/var/www/registry

    registry_nginx:
        hostname: registry
        build: ./nginx/registry
        depends_on:
            - registry_fpm
        volumes:
            - ./microservices/registry:/var/www/registry
            - ./nginx/registry/nginx.conf:/etc/nginx/nginx.conf

    
    #server
    nginx:
        build: ./nginx
        environment:
            MICROSERVICE_NAME: Microservice_1
        depends_on:
            - registry_nginx
        ports:
            - 80:80/tcp

MiyRon
  • 406
  • 7
  • 15
  • 1
    How **exactly** do you run that script? – Nico Haase Jan 04 '22 at 09:47
  • @NicoHaase I edited the post, please take a look – MiyRon Jan 04 '22 at 09:50
  • 4
    this is the expected behaviour. docker containers run only one command and exit when the command exits. refer [this SO thread](https://stackoverflow.com/q/44884719/13982210). if i am not mistaken you are looking for a separate container that monitors the [docker socket](https://stackoverflow.com/a/35110344/13982210) and registers the start/stop/status of the containers. please share your compose file (redacted) and we could suggest something – kevinnls Jan 04 '22 at 09:55
  • some clarifications needed: is logging the IP the **sole** purpose of registry_* and pgsql? is there any other data (e.g logs) you need from the containers? – kevinnls Jan 04 '22 at 10:04
  • 2
    I'm confused as to what you expect the Docker container to continue doing after the curl. – daniu Jan 04 '22 at 10:06
  • @kevinnls Yes, the purpose of the registry is to store only the ip of all microservices. If you are familiar with microservices, then it is quite possible that you know that the registry is like the custodian of all addresses of microservices. The registry is used by other microservices to obtain microservice addresses so that microservices can communicate over http. – MiyRon Jan 04 '22 at 10:11
  • @daniu in the config of this nginx_1 there is a fast sgi setting that sends php files, I just didn't specify it too large. But below there is container fpm_1. – MiyRon Jan 04 '22 at 10:12

1 Answers1

0

the purpose of the registry is to store only the ip of all microservices. If you are familiar with microservices, then it is quite possible that you know that the registry is like the custodian of all addresses of microservices. The registry is used by other microservices to obtain microservice addresses so that microservices can communicate over http.

there is no need for these addresses as far as i can tell. the microservices can easily use each other's hostnames.

you already do this with your curl: the POST request goes to the server registry; and so on

docker compose may just be all the orchestration you require for you microservices.


regarding IPs and networking

if you prefer, for more isolation and consistency, you can configure in your compose.yaml


regarding heartbeat

keeping track of a heartbeat shouldn't be necessary.

but if you really need one, doing it from within the container is a no-no. a container should be only one running process. and creating a new record is redunduant as the docker daemon is already keeping track of all IP and state (and loads of others).

the function of registry (keeping track of lifecycle) is instead played by the docker daemon. try docker compose ps

however, you can configure the container to restart automatically when it fails using the restart tag

if you need a way to monitor these without the CLI, listening on the docker socket is the way to go.

you could make your own dashboard that taps into the Docker API whose endpoints are listed here. NB: the socket might need to be protected and if possible, ought to be mounted as read-only

but better solution would be a using an image that already does this. i cannot give you recommendations unfortunately; i have not used any.

kevinnls
  • 728
  • 4
  • 19
  • Yes, I understand this, but microservices also make a request for the address / heartbeat every minute, the purpose of which is to say "the microservice is alive and continues to work" if you know the best way to divert the life of microservices, please tell me! – MiyRon Jan 04 '22 at 10:21
  • hmm a heartbeat, huh? lemme think. hold on; i don't see a controller: is that supposed to be a function of the registry? – kevinnls Jan 04 '22 at 10:31
  • yes, all microservices send requests to the registry and the registry updates the status of the registry to "Online" If the microservice did not send a "heartbeat" request for more than 5 minutes, then it becomes "Offline" – MiyRon Jan 04 '22 at 10:34
  • This answer does not explain why the container is shutdown - as explained in some comments to the question that is because the container command has completed. This answer is more about the 'why' of what the question is about. – JohnXF Jan 04 '22 at 15:01
  • would these "some comments" happen to be mine as well @JohnXF? the question title needs to be edited. the queue is full so i can't do it. if would be so kind – kevinnls Jan 04 '22 at 15:12
  • Yes, @kevinnls your comment pinpoints the reason the container shuts down - and is the answer to the question asked both in the title and the body of the question. Your answer starts talking about registries, IP and networking which - to me - are outside the scope of the question hence I don't think it is useful. If anything, the question is not targeted enough - there's the direct problem and then the incorrect thinking leading to the setup the OP is trying to achieve. – JohnXF Jan 04 '22 at 15:51
  • "incorrect thinking" - and yet your response to someone who wishes to help guide toward the correct thinking is... – kevinnls Jan 04 '22 at 15:55
  • notheless. i had a conversation with OP in comments on the question as well as in my answer. i stand by my "divergent" answer. you have my gratitude for pinpointing that it is in fact divergent. – kevinnls Jan 04 '22 at 15:56