docker
docker-compose
entrypoint
shell-scripting
container-setup

entrypoint entrypoint.sh - docker compose

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding Entrypoint: entrypoint.sh

in Docker Compose

Docker Compose is a powerful tool for defining and running multi-container Docker applications. One of its essential components is the entrypoint , which you might often see implemented as entrypoint.sh . This script plays a critical role in setting up and configuring the environment within a Docker container. In this article, we will delve into the technical aspects of entrypoint.sh , explore how it integrates with Docker Compose, and provide useful examples to aid understanding.

What is an Entrypoint?

The entrypoint in Docker is the command that is executed when a container starts. In Docker Compose, it is specified within the service configuration. The entrypoint.sh script allows you to configure the container environment and execute necessary command(s) before the main application command runs.

Unlike the CMD instruction in Docker, which provides default arguments for the main command, ENTRYPOINT makes a container executable by default. This implies that any command-line arguments passed during runtime are appended to the ENTRYPOINT command.

When to Use entrypoint.sh

Using entrypoint.sh is beneficial when:

  • You need to set environment variables dynamically.
  • Initial configuration is required before starting the main application.
  • You need to handle signals or perform cleanup tasks.
  • The application needs pre-start activities, such as database migrations, to be executed.

How to Define Entry Point in Docker Compose

To define an entrypoint in Docker Compose, you can set it under a specific service in the docker-compose.yml file. Here's a simple example:

  • ./entrypoint.sh:/usr/local/bin/entrypoint.sh
  • "8080:8080"
  • The entrypoint specifies the path to the entrypoint.sh script.
  • The script is copied to the container using a volume mount, which ensures the latest version is used every time the container starts.
  • set -e : This causes the script to exit immediately if any command fails.
  • Environment Initialization: Initial setup tasks, such as connecting to external services, are performed.
  • Conditional Logic: Checks if an environment variable is set to decide on running database migrations.
  • exec "$@" : This line passes all arguments to the main process, thereby replacing the shell script with the command defined in the CMD or passed at runtime.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.