dockerNewsProfessionalTech News

How to write a startup script for your Ubuntu containers:

  1. Create a new bash script file, for example start_containers.sh:
#!/bin/bash
set -eou pipefail

# Log file for debugging
LOG_FILE="/var/log/docker_startup.log"

# Function to log messages
log_message() {
    echo "$(date): $1" >> "$LOG_FILE"
}

# Wait for Docker to start
while ! docker info >/dev/null 2>&1; do
    log_message "Waiting for Docker to start..."
    sleep 5
done

log_message "Docker is running. Starting containers..."

# Start your containers here
docker start container1
docker start container2
docker start container3

# Or use docker-compose if you have a docker-compose.yml file
# cd /path/to/your/docker/compose/directory
# docker-compose up -d

log_message "Containers started successfully"
  1. Make the script executable:
chmod +x start_containers.sh
  1. To ensure this script runs automatically after a server restart, you can create a systemd service. Create a new file named docker-startup.service in /etc/systemd/system/:
sudo nano /etc/systemd/system/docker-startup.service
  1. Add the following content to the file:
[Unit]
Description=Start Docker containers after boot
After=docker.service
Requires=docker.service

[Service]
Type=oneshot
ExecStart=/path/to/your/start_containers.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
  1. Save the file and exit the editor.
  2. Reload the systemd daemon to recognize the new service:
sudo systemctl daemon-reload
  1. Enable the service to run at boot:
sudo systemctl enable docker-startup.service
  1. You can start the service manually to test it:
sudo systemctl start docker-startup.service

This script and systemd service will:

  • Wait for the Docker daemon to start before attempting to start containers
  • Log its actions to a file for debugging purposes
  • Start your specified containers or run docker-compose
  • Run automatically after each system reboot

Remember to replace container1, container2, etc., with your actual container names or use docker-compose commands if you prefer that method[2][3][4].

Citations:
[1] https://help.ubuntu.com/community/Beginners/BashScripting
[2] https://www.reddit.com/r/selfhosted/comments/ovr175/how_do_you_auto_recreate_docker_containers_on/
[3] https://betterstack.com/community/questions/how-to-start-docker-containers-automatically-after-reboot/
[4] https://www.digitalocean.com/community/questions/how-to-start-docker-containers-automatically-after-a-reboot
[5] https://supportfly.io/docker-restart-policy/
[6] https://www.warp.dev/terminus/docker-run-bash
[7] https://stackoverflow.com/questions/30449313/how-do-i-make-a-docker-container-start-automatically-on-system-boot
[8] https://docs.docker.com/engine/containers/start-containers-automatically/
[9] https://jugmac00.github.io/blog/how-to-run-a-dockerized-service-via-systemd/

Bill

Bill is a passionate network engineer who loves to share his knowledge and experience with others. He writes engaging blog posts for itacute.com, where he covers topics such as home and small business networking, electronic gadgets, and tips and tricks to optimize performance and productivity. Bill enjoys learning new things and keeping up with the latest trends and innovations in the field of technology.

One thought on “How to write a startup script for your Ubuntu containers:

  • descargador de videos musicales

    Your blog is a treasure trove of valuable insights and thought-provoking commentary. Your dedication to your craft is evident in every word you write. Keep up the fantastic work!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *