Docker Community Forums

Share and learn in the Docker community.

Docker challenge

docker
#1

I have a challenge project.

A docker container is running Node.js program. It does 2 things.

  • It queries employee every 5 minutes
  • It queries staff every 10 mins

10 different docker container with same image.

The staff program should run only one docker container.
If one running the script is dead, another container will run it.

Is it possible to implement it?

(Gary Forghetti) #2

Change your Node.js code to check an environment variable which tells it what to query.

Example environment variable setting.

export QUERY_TYPE='EMPLOYEE'
or
export QUERY_TYPE='STAFF'

In your code retrieve the environment variable QUERY_TYPE.
If QUERY_TYPE=‘EMPLOYEE’ then your code queries employee every 5 minutes.
If QUERY_TYPE=‘STAFF’ then your code queries staff every 10 mins.

Then create a docker-compose.file with 2 services. You will need to add any other options you may need to the docker-compose.yml file (i.e. volumes, other environment variables, etc).

version: '3.4'
           
services:

  employee:
    image: your-docker-image-containing your code
     environment:
      - QUERY_TYPE='EMPLOYEE'

  staff:
    image: your-docker-image-containing your code
    environment:
      - QUERY_TYPE='STAFF'

Deploy this as a stack with Docker swarm.
Example:

docker stack deploy my-stack -c docker-compose.yml

Docker will start another container if a container fails automatically.
If you don’t run Docker Swarm, then you can add a restart: always to each service in the docker-compose.yml file and run docker-compose up -d. Docker will restart the containers if they fail.