docker-compose-Makefile
Manage Docker Compose projects efficiently with this Makefile. Automate container startup, cleanup, and log viewing for seamless development workflows.
Docker Compose Makefile
Makefile for Docker Compose Automation
This Makefile provides a streamlined way to manage your Docker Compose projects. It simplifies common Docker operations, allowing you to focus on development rather than complex command-line arguments. By defining targets for starting, stopping, and viewing logs, this Makefile enhances your development workflow and ensures consistency across different environments.
Key Docker Compose Makefile Commands
The following commands are defined within the Makefile to facilitate Docker Compose management:
help
: Display Available Commands
This target provides a comprehensive list of all available commands within the Makefile, along with their descriptions. It's the first command you should run to understand the Makefile's capabilities.
up
: Start Containers
The up
target is used to build and start your Docker containers in detached mode. This is typically the first step in running your application locally. It ensures that all services defined in your docker-compose.yml
file are running.
clean
: Stop and Remove Containers
Use the clean
target to gracefully stop and remove all containers, networks, and volumes associated with your Docker Compose project. This is essential for resetting your environment and freeing up resources.
logs
: View Container Logs
The logs
target allows you to follow the real-time output of your containers. This is invaluable for debugging and monitoring your application's behavior. The -f
flag ensures that the logs are streamed live.
open
: Access Application URL
This target is a convenience command to open a specific URL in your browser, typically used to access a web application running within a container. It assumes the application is accessible at http://localhost:3000/
.
Makefile Structure and Logic
The Makefile intelligently detects whether docker-compose
or the newer docker compose
command is available on your system. It sets the DOCKER_COMPOSE_BINARY
variable accordingly, ensuring compatibility. If neither command is found, it throws an error, prompting the user to install the necessary Docker tools.
# Thanks: https://gist.github.com/mpneuried/0594963ad38e68917ef189b4e6a269db
.PHONY: help
HAS_DOCKER_COMPOSE := $(shell command -v docker-compose 2> /dev/null)
HAS_DOCKER_COMPOSE_V2 := $(shell command -v docker 2> /dev/null)
ifeq ($(strip $(HAS_DOCKER_COMPOSE)),)
ifeq ($(strip $(HAS_DOCKER_COMPOSE_V2)),)
$(error No compatible command found)
else
DOCKER_COMPOSE_BINARY := docker compose
endif
else
DOCKER_COMPOSE_BINARY := docker-compose
endif
help: ## This help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help
# DOCKER TASKS
up: ## Runs the containers in detached mode
@$(DOCKER_COMPOSE_BINARY) up -d --build
clean: ## Stops and removes all containers
@$(DOCKER_COMPOSE_BINARY) down
logs: ## View the logs from the containers
@$(DOCKER_COMPOSE_BINARY) logs -f
open: ## Opens tabs in container
open http://localhost:3000/