- 1 year ago
How to automatically start docker container on windows boot ~ Wait for docker to be running
To automatically start a Docker container on Windows boot and wait for Docker to be running, you can utilize the Docker Compose tool along with the restart
and depends_on
options. Here's how you can achieve this:
Create a Docker Compose file: Create a Docker Compose YAML file (e.g.,
docker-compose.yml
) to define your container configuration. Specify the necessary services, volumes, networks, and other container settings.Configure container restart policy: In the Docker Compose file, set the
restart
policy for the container toalways
. This ensures that the container is automatically restarted if it stops or if the Docker service restarts.yamlversion: '3'
services:
my-container:
image: your-image:tag
restart: always
# Other container configuration
Set dependencies: If your container relies on other services (e.g., a database), you can use the
depends_on
option to specify the dependency. Docker Compose will ensure that the dependent service is started before the container.yamlversion: '3'
services:
db:
image: mysql:latest
# Database configuration
my-container:
image: your-image:tag
restart: always
depends_on:
- db
# Other container configuration
Create a startup script: Create a startup script (e.g.,
start-containers.bat
) to start the containers using Docker Compose and wait for Docker to be running before executing the command.bat@echo off
setlocal
:retry
docker info >nul 2>&1
if %errorlevel% neq 0 (
echo Docker is not running. Retrying in 5 seconds...
timeout /t 5 /nobreak >nul
goto retry
)
echo Starting containers...
docker-compose up -d
endlocal
The script continuously checks if Docker is running using the
docker info
command. If Docker is not running, it waits for 5 seconds and retries until Docker is running. Once Docker is running, it starts the containers usingdocker-compose up -d
.Configure script to run on Windows startup: To run the startup script on Windows boot, you can add it to the Startup folder or create a scheduled task.
To add the script to the Startup folder, press
Win + R
to open the Run dialog, then entershell:startup
to open the Startup folder. Copy the script file (start-containers.bat
) into this folder, and it will be executed on Windows startup.To create a scheduled task, open the Task Scheduler application (
taskschd.msc
), create a new task, and configure it to run the script (start-containers.bat
) on Windows startup.
By following these steps, your Docker container will be automatically started on Windows boot, and the startup script will wait for Docker to be running before executing the docker-compose up
command.