How to write a startup script for your Ubuntu containers:
To create an Ubuntu bash script that starts Docker containers automatically after a server power restart, you can follow these steps:
- 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"
- Make the script executable:
chmod +x start_containers.sh
- 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
- 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
- Save the file and exit the editor.
- Reload the systemd daemon to recognize the new service:
sudo systemctl daemon-reload
- Enable the service to run at boot:
sudo systemctl enable docker-startup.service
- 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].
By using this approach, you ensure that your Docker containers start automatically after a server reboot, without relying on Docker’s built-in restart policies. This gives you more control over the startup process and allows for additional logic if needed[5].
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/
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!