with-help-section-Makefile
Use this Makefile to easily manage Docker Compose tasks: pull dependencies, build, run, stop, and view logs. Includes a comprehensive help section.
Makefile Help
This Makefile provides a set of convenient commands for managing Docker Compose projects. It simplifies common development workflows such as pulling dependencies, building images, starting containers, stopping services, and viewing logs. The integrated help
target offers a clear overview of all available commands and their descriptions.
Makefile Commands Overview
The primary purpose of this Makefile is to streamline Docker-based development. It leverages docker-compose
to orchestrate multi-container applications. Below is a detailed explanation of each command:
help
: Display Available Commands
This command lists all targets defined in the Makefile, along with their descriptions. It's the first command to run when you need to understand the available automation scripts.
# Thanks: https://gist.github.com/mpneuried/0594963ad38e68917ef189b4e6a269db
.PHONY: help
help: ## This help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
deps
: Pull and Build Docker Dependencies
This target first pulls the latest images for your services defined in docker-compose.yml
and then builds any necessary images locally.
deps: ## pulls and builds
docker-compose pull
docker-compose build
up
: Run Docker Containers
Starts all services defined in your docker-compose.yml
file in detached mode (running in the background). It also rebuilds images if necessary.
up: ## Runs the containers in detached mode
docker-compose up -d --build
all
: Full Setup and Run
This is a convenience target that combines deps
and up
, ensuring your dependencies are pulled and built before starting the containers.
all: deps up ## Generate, Builds and Runs the Containers in detached mode
stop
: Stop and Remove Containers
Shuts down and removes all containers, networks, and volumes associated with your Docker Compose project.
stop: ## Stops and removes all containers
docker-compose down
logs
: View Container Logs
Follows and displays the logs from all running containers in real-time. Useful for debugging.
logs: ## View the logs from the containers
docker-compose logs -f
open-tabs
: Open Application in Browser
Opens a specific URL in your default web browser, typically used to access a web application running locally.
open-tabs: ## Opens tabs in container
open http://localhost:8080/
Getting Started with Docker Compose
To effectively use this Makefile, ensure you have Docker and Docker Compose installed on your system. Place this Makefile
in the root directory of your project, alongside your docker-compose.yml
file. You can then execute commands like make up
or make logs
from your terminal.
Best Practices for Makefiles
Makefiles are powerful tools for automating repetitive tasks. For more complex projects, consider organizing your targets logically and using descriptive names. Refer to the GNU Make Manual for advanced usage and best practices.
For more information on Docker Compose, consult the official Docker Compose documentation.