docker-compose
container orchestration
post-start script
container automation
DevOps

docker-compose, run a script after container has started?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Docker Compose is a tool specifically designed to streamline and simplify the process of managing multi-container Docker applications. It allows you to define and manage services, networks, and volumes in a single YAML file (docker-compose.yml). Working with Docker Compose offers various advantages, such as easy batch execution of tasks, the ability to restart services, and defining complex network systems. In this article, we will explore how you can run a script after a container has started using Docker Compose.

What is Docker Compose?

Docker Compose is a tool that helps define and run multi-container Docker applications. It leverages a YAML file to configure the application’s services, enabling you to configure all the services such as building images, linking containers, and specifying networks and volumes in a straightforward manner. The main command used with Docker Compose is docker-compose, which comes with subcommands like up, down, build, and logs.

Lifecycle of Docker Containers in a Compose Setup

  1. Build: Docker Compose can build images before starting the services.
  2. Create: It creates containers based on the service definitions.
  3. Start: It starts the containers.
  4. Connect: Containers are connected to specified networks.
  5. Run: Containers start executing defined commands and bound scripts.

Running a Script After a Container Starts

When managing complex applications, you might need to run initialization scripts after the container starts. This could include database migrations, seeding databases, or starting custom applications.

Using Entry Point Scripts

One approach is to use entry point shell scripts. Below is an example using a custom entry point in Docker Compose.

Directory Structure:

 
1.
2├── docker-compose.yml
3├── app
4│   ├── Dockerfile
5│   ├── entrypoint.sh
6│   └── script-to-run.sh

Dockerfile:

dockerfile
1FROM ubuntu:20.04
2
3WORKDIR /app
4
5COPY entrypoint.sh /app/
6COPY script-to-run.sh /app/
7RUN chmod +x entrypoint.sh script-to-run.sh
8
9ENTRYPOINT ["/app/entrypoint.sh"]

entrypoint.sh:

bash
1#!/bin/bash
2
3# Execute the main application in background
4your_app_command &
5
6# Post-start script
7/app/script-to-run.sh
8
9# Wait indefinitely to keep the container running
10tail -f /dev/null

script-to-run.sh:

bash
1#!/bin/bash
2
3echo "Running script after container has started."
4# Add additional command logic here

docker-compose.yml:

yaml
1version: '3.7'
2
3services:
4  app:
5    build: ./app
6    volumes:
7      - ./app:/app
8    environment:
9      - EXAMPLE_ENV_VAR=value

Breakdown of Workflow

  1. Build Image: The docker-compose.yml points to a directory with a Dockerfile.
  2. Copy and Grant Execution: The Dockerfile copies entrypoint.sh and script-to-run.sh to the Docker image and grants them execution permissions.
  3. Execute Entrypoint: entrypoint.sh is executed when the container starts. It runs the main application command in the background (substituting your_app_command with the actual command).
  4. Run Post-Start Script: Once the main application is running, script-to-run.sh executes any post-startup logic.
  5. Keep Container Running: The tail -f /dev/null command ensures that the container doesn't exit immediately.

Advantages of Script Execution After Start

  • Simplicity: Encapsulates logical/conditional execution.
  • Portability: Scripts can be reused across different environments and setups.
  • Automation: Automates routine initialization tasks.

Table: Key Points of Running Post-start Scripts

FeatureDescription
Entry PointConfigures a script executed on container startup.
Background ProcessesPrimary application runs in the background enabling script execution.
Script ExecutionScript is executed only after the container's entry point.
PersistenceTailing a null device to keep the container alive.
Environment CompatibilityUses environment variables from docker-compose.yml.

Docker Compose offers a robust way to build and manage complex multi-container applications. By using entry-point scripts, you can efficiently execute custom initialization logic, ensuring your application components are ready and appropriately configured after the containers launch. This ensures a smoother and more automated deployment process, saving time and reducing potential human errors.


Course illustration
Course illustration

All Rights Reserved.