Jump to content

Run script when specific docker starts


Go to solution Solved by JonathanM,

Recommended Posts

19 hours ago, JonathanM said:

Not that I can think of, but you can certainly start a container from a script. Disable the auto start for that container on the docker page and script the startup separately.

schedule at array start

sleep however long you want to wait

docker start containername

run the rest of your commands.

Thanks, that is basically what I ended up doing. Running this on a cron every few minutes to check if the docker is running and if not it starts it up and executes the commands I need. Sharing the script in case anyone else ever needs this.

 

#!/bin/bash

# Replace CONTAINER_NAME with the actual name of your running Docker container
CONTAINER_NAME="CHANGEME"

# Specify the log file path
LOG_FILE="docker_exec_log.txt"

# Check if the container is already running
if docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; then
    echo "Container $CONTAINER_NAME is already running."
    exit 0
fi

# Start the container if it's not running
docker start $CONTAINER_NAME

# Wait for the container to start
echo "Waiting for container $CONTAINER_NAME to start..."
while ! docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; do
    sleep 1
done

echo "Container $CONTAINER_NAME is now running."

# Run commands inside the Docker container and log errors/output
docker exec $CONTAINER_NAME bash -c '
 REPLACE_THIS_WITH_THE_COMMANDS_YOU_WANT_TO_RUN
' 2>&1 > $LOG_FILE

# Display the log file contents
cat $LOG_FILE

 

Edited by pkoasne
added script
  • Thanks 1
Link to comment
  • 2 months later...
On 8/12/2023 at 8:19 PM, pkoasne said:

Thanks, that is basically what I ended up doing. Running this on a cron every few minutes to check if the docker is running and if not it starts it up and executes the commands I need. Sharing the script in case anyone else ever needs this.

 

#!/bin/bash

# Replace CONTAINER_NAME with the actual name of your running Docker container
CONTAINER_NAME="CHANGEME"

# Specify the log file path
LOG_FILE="docker_exec_log.txt"

# Check if the container is already running
if docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; then
    echo "Container $CONTAINER_NAME is already running."
    exit 0
fi

# Start the container if it's not running
docker start $CONTAINER_NAME

# Wait for the container to start
echo "Waiting for container $CONTAINER_NAME to start..."
while ! docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; do
    sleep 1
done

echo "Container $CONTAINER_NAME is now running."

# Run commands inside the Docker container and log errors/output
docker exec $CONTAINER_NAME bash -c '
 REPLACE_THIS_WITH_THE_COMMANDS_YOU_WANT_TO_RUN
' 2>&1 > $LOG_FILE

# Display the log file contents
cat $LOG_FILE

 

You are awesome for sharing the script! I greatly appreciate it, thank you

  • Thanks 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...