0

How can I restart docker containers in custom order when the server restarts?

Hi guys, so I'm trying to start all my docker containers if my server restarts, I have around 30 containers, I've read about using

docker update --restart unless-stopped <container>

but the problem is that 7 containers uses a lot of RAM when compiling, so if the server restarts and all container start at the same time my server is going to crash because it is out of RAM, so I need to start one container, wait until it is completely ready (because here RAM is free again) and start the other container...

I've read about dockerize but I'm not sure how to use it. Any ideas?

Thanks in advance and sorry for my english.

Viv Bass
  • 3
  • 1

1 Answers1

0

You could do this with docker depend_on and make next container dependent on the previous container. Here is sample docker compose file:

version: '3'
services:
  container1:
    image: image1
    restart: always
  container2:
    image: image2
    restart: always
    depends_on:
      - container1
  container3:
    image: image3
    restart: always
    depends_on:
      - container2
  ...
  container30:
    image: image30
    restart: always
    depends_on:
      - container29

Here is official documentation and other possibilities: Docker Documentation

or second solution would be to write a script which starts the container one by one after system reboot. for that you have to remove restart: unless-stopped

#!/bin/bash

# Define an array of your containers
containers=("container1" "container2" "container3")

# Define an array of the words you want to wait for in each container's logs
log_words=("word1" "word2" "word3")

for i in "${!containers[@]}"
do
    container=${containers[$i]}
    log_word=${log_words[$i]}

    echo "Starting $container..."

    # Start the container
    docker start $container

    echo "Waiting for $log_word in $container logs..."

    # Follow the container's logs and wait for the specified word
    while true
    do
        log=$(docker logs --tail 1 $container)
        
        if [[ $log == *"$log_word"* ]]; then
            echo "$log_word found in $container logs. Continuing to the next container..."
            break
        else
            # Wait for a second before checking the logs again
            sleep 1
        fi
    done
done

  • Thank you so much! I think depends_on only checks if container is started but no if it's ready, I need it to be completely ready to start the other. I also see this in docker documentation: "Docker recommends that you use restart policies, and avoid using process managers to start containers." So, writing a script to start one by one every container won't give some errors? – Viv Bass May 24 '23 at 18:16
  • @VivBass how can we know that the thing, what you want to do in the container, is done and next container should start ? Do you echo something on the logs ? if yes, we can check the logs and as soon as that specific line exists in the logs of the container, we move to the next container. – Micheal Choudhary May 24 '23 at 18:19
  • @VivBass i have updated the solution. feel free to ask. – Micheal Choudhary May 24 '23 at 18:24
  • I totally forgot to answer but thaaaaaaaaaaaank you sooo much!!! The script worked like a charm!!!! So many thanks! – Viv Bass Jun 02 '23 at 01:53