Docker Administration
Quick Start Guide to Docker DevOps Infrastructure
Overview
This guide provides a comprehensive quick start for administrators to set up and manage our Docker-based DevOps infrastructure. The system leverages Docker and a host machine, with each service folder containing a docker-compose.yml file that defines the necessary services.
Folder Structure
- /opt/servicename: This directory is used for persistent data storage for each service. Replace
servicenamewith the actual name of the service. - /secrets/${ENVIRONMENT}.env: This file contains environment-specific secrets. Replace
${ENVIRONMENT}with the appropriate environment name (e.g.,production,staging). - /secrets/${ENVIRONMENT}/${SERVICENAME}.env: This file contains service-specific secrets for a given environment. Replace
${ENVIRONMENT}with the environment name and${SERVICENAME}with the name of the service.
Prerequisites
- Docker: Ensure Docker is installed and running on your host machine. You can download and install Docker from here.
- Docker Compose: Basic knowledge of Docker and Docker Compose is required. Docker Compose should also be installed. You can find the installation instructions here.
- Utility Scripts: The scripts located in the
./utildirectory should be added to your system's PATH. Run theinstall-util.shscript to achieve this. This script will ensure that the utility scripts are accessible from any location in your terminal.
Setting Up Services
-
Create Persistent Data Folder:
- Create a directory in
/opt/servicenameto store persistent data for each service. Replaceservicenamewith the actual name of the service. - Ensure this folder is included in your backup script (
./backup.sh) to maintain data integrity.
- Create a directory in
-
Manage Secrets:
- Store environment-specific secrets in
/secrets/${ENVIRONMENT}/${SERVICENAME}.env. Replace${ENVIRONMENT}with the appropriate environment name (e.g.,production,staging) and${SERVICENAME}with the name of the service. - Reference these environment variables in your
docker-compose.ymlfile to securely pass secrets to your services.
- Store environment-specific secrets in
-
Configure Docker Compose:
- Use volumes in your
docker-compose.ymlfile to mount configuration files from/opt/servicename/to the appropriate locations within your containers. - Utilize Traefik labels in your
docker-compose.ymlfile to map URLs to the corresponding service IPs and ports for proper routing.
- Use volumes in your
-
Template Replacement:
- Files with a
.templateextension will undergo string substitution, replacing placeholders like{{ENV-VAR-NAME}}with the corresponding environment variable values. - Use the
setup-envscript to set environment variables and run necessary hooks. Thedccommand will handle this process for you.
- Files with a
Running Services
- Start/Stop Services:
- Use
dc upto start services. - Use
dc downto stop services. dcis an alias forcompose.sh.
- Use
Exiisting Services
- **/traefik: Web frontend and SSL proxy.
- **/rehash: Basic setup including MariaDB and Sphinx.
- /rehash-dev: Testing environment for rehash.
- /ircd: Solanum IRCD and Atheme services.
Environment Setup Details from compose.sh and setup-env.sh
-
Load Environment Variables:
- Environment variables are loaded from multiple sources in the following order of increasing priority:
./.env/secrets/${ENVIRONMENT}.env/secrets/${ENVIRONMENT}/${SERVICENAME}.env./.env(loaded again to override any previous values)
- Environment variables are loaded from multiple sources in the following order of increasing priority:
-
Execute Pre-Hook Script:
- The
pre-hook.shscript is run to perform any necessary setup before processing templates. This can include tasks such as generating changes to the templates based on the environment variables.
- The
-
Process and Replace Templates:
- Templates are processed and filled with the appropriate values, including any secrets. Once filled, these templates are moved to the
/optdirectory.
- Templates are processed and filled with the appropriate values, including any secrets. Once filled, these templates are moved to the
-
Execute Post-Hook Script:
- The
post-hook.shscript is run to perform any final setup tasks before the Docker container is started. This typically involves moving secret-filled files from the devops home folder to the/optdirectory to ensure they are in the correct location for the container.
- The
Commands
-
Start or Stop All Services:
-
To start all services, use the following command:
dc up -
This command is equivalent to:
docker compose up -d --force-recreate
-
It will start all services defined in the
docker-compose.ymlfile in detached mode and recreate containers if necessary. -
To stop all services, use the following command:
dc down -
This command is equivalent to:
docker compose down
-
It will stop and remove all containers defined in the
docker-compose.ymlfile.
-
-
Start or Stop a Specific Service:
-
To start a specific service, use the following command:
dc up [SERVICE_NAME] [EXTRA_ARGS]
-
This command is equivalent to:
docker compose up --force-recreate "$SERVICE_NAME" $EXTRA_ARGS
-
Replace
[SERVICE_NAME]with the name of the service you want to start and[EXTRA_ARGS]with any additional arguments. -
To stop a specific service, use the following command:
dc down [SERVICE_NAME] [EXTRA_ARGS]
-
This command is equivalent to:
docker compose down --force-recreate "$SERVICE_NAME" $EXTRA_ARGS
-
Replace
[SERVICE_NAME]with the name of the service you want to stop and[EXTRA_ARGS]with any additional arguments.
-
Conclusion
This guide provides a structured approach to setting up and managing services in our Docker-based infrastructure. Follow the steps and examples to get started quickly.