Skip to content

Commit aed751a

Browse files
authored
updated docker (#1618)
* updated docker port * removed .env
1 parent 1aaa3bd commit aed751a

25 files changed

+955
-83
lines changed

.env

Lines changed: 0 additions & 11 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# Project files
88
etc/config/config.yml
9+
.env
910
etc/logs
1011

1112
# Extractor

Makefile

Lines changed: 114 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,135 @@
11

22
DOCKER_COMPOSE_FILE ?= docker-compose.yml
3+
DOCKER_COMPOSE ?= docker compose
4+
UTILITY := utility
35

46
# Well documented Makefiles
57
DEFAULT_GOAL := help
68

79
help:
8-
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-40s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
10+
@echo "Available commands:"
11+
@echo ""
12+
@echo "=== Setup ==="
13+
@echo " make config - Install default config to alpha-core"
14+
@echo " make setup-all - Run config, db-setup, and up"
15+
@echo " make external_db - Switch to external DB and disable local sql"
16+
@echo " make internal_db - Switch to local sql and enable localdb profile"
17+
@echo ""
18+
@echo "=== Database ==="
19+
@echo " make db-user - Create DB user (if not root)"
20+
@echo " make db-create - Create databases"
21+
@echo " make db-drop - Drop databases"
22+
@echo " make db-populate - Populate databases (schema only)"
23+
@echo " make db-update - Apply database updates"
24+
@echo " make db-setup - Create, populate, then update databases"
25+
@echo " make db-backup - Backup databases (optional BACKUP=folder)"
26+
@echo " make db-restore - Restore databases (optional BACKUP=folder)"
27+
@echo ""
28+
@echo "=== Docker ==="
29+
@echo " make up - Build and start all containers"
30+
@echo " make start - Start all containers"
31+
@echo " make stop - Stop all containers (not dev)"
32+
@echo " make restart - Restart all containers (not dev)"
33+
@echo " make down - Stop and remove containers (not dev)"
34+
@echo " make build-utility - Build utility image"
35+
@echo " make connect <name> - Connect to container"
36+
@echo " make list - List running containers"
37+
@echo ""
38+
@echo "=== Logs ==="
39+
@echo " make log <name> - Show container log"
40+
@echo " make logs - Show all containers logs"
941

1042
##@ [Run Firsttime]
43+
.PHONY: config
1144
config: ## Install default config to alpha-core
12-
cp etc/config/config.yml.dist etc/config/config.yml
45+
@if [ -e etc/config/config.yml ] && [ ! -w etc/config/config.yml ]; then \
46+
echo "etc/config/config.yml is not writable; run: sudo chown $$USER:$$USER etc/config/config.yml"; \
47+
exit 1; \
48+
fi
49+
@cp etc/config/config.yml.dist etc/config/config.yml
50+
@echo "Config installed to etc/config/config.yml"
51+
52+
setup-all: ## Run config, db-setup, and up
53+
$(MAKE) config
54+
$(MAKE) db-setup
55+
$(MAKE) up
56+
57+
external_db: ## Switch to external DB and disable local sql
58+
@set -a; . ./.env; set +a; python3 etc/docker/external_db.py
59+
60+
internal_db: ## Switch to local sql and enable localdb profile
61+
@set -a; . ./.env; set +a; python3 etc/docker/internal_db.py
62+
##@ [Database]
63+
db-create: build-utility db-start-local db-user ## Create databases
64+
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/create_databases.sh
65+
66+
db-drop: build-utility db-start-local ## Drop databases
67+
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/drop_databases.sh
68+
69+
db-populate: build-utility db-start-local ## Populate databases (schema only)
70+
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/populate_databases.sh
71+
72+
db-update: build-utility db-start-local ## Apply database updates
73+
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/update_databases.sh
74+
75+
db-setup: ## Create, populate, then update databases
76+
$(MAKE) db-create
77+
$(MAKE) db-populate
78+
$(MAKE) db-update
79+
80+
db-backup: build-utility db-start-local ## Backup databases (optional BACKUP=folder)
81+
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/backup_db.sh
82+
83+
db-restore: build-utility db-start-local ## Restore databases (optional BACKUP=folder)
84+
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/restore_db.sh
1385

1486
##@ [Docker]
87+
build-utility: ## Build utility image
88+
$(DOCKER_COMPOSE) build $(UTILITY)
89+
90+
db-user: build-utility db-start-local ## Create DB user (if not root)
91+
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/create_db_user.sh
92+
93+
db-start-local: ## Start local sql if MYSQL_HOST=sql
94+
@set -a; . ./.env; set +a; \
95+
if [ "$$MYSQL_HOST" = "sql" ] || [ "$$MYSQL_HOST" = "127.0.0.1" ] || [ "$$MYSQL_HOST" = "localhost" ]; then \
96+
$(DOCKER_COMPOSE) up -d sql; \
97+
echo "Waiting for sql to be ready..."; \
98+
host="$$MYSQL_HOST"; \
99+
port="$$MYSQL_PORT"; \
100+
if [ "$$MYSQL_HOST" = "sql" ]; then \
101+
host="127.0.0.1"; \
102+
port="$$MYSQL_HOST_PORT"; \
103+
fi; \
104+
i=0; \
105+
while [ $$i -lt 30 ]; do \
106+
if bash -c "</dev/tcp/$$host/$$port" >/dev/null 2>&1; then \
107+
echo "sql is ready."; \
108+
break; \
109+
fi; \
110+
i=$$((i+1)); \
111+
sleep 2; \
112+
done; \
113+
if [ $$i -ge 30 ]; then \
114+
echo "Timed out waiting for sql."; \
115+
exit 1; \
116+
fi; \
117+
fi
118+
15119
up: ## Build and start all containers
16-
docker compose -f $(DOCKER_COMPOSE_FILE) up -d
120+
$(DOCKER_COMPOSE) up -d
17121

18122
start: ## Start all containers
19-
docker compose -f $(DOCKER_COMPOSE_FILE) start
123+
$(DOCKER_COMPOSE) start
20124

21125
stop: ## Stop all containers (not dev)
22-
docker compose -f $(DOCKER_COMPOSE_FILE) stop
126+
$(DOCKER_COMPOSE) stop
23127

24128
restart: ## Restart all containers (not dev)
25-
docker compose -f $(DOCKER_COMPOSE_FILE) restart
129+
$(DOCKER_COMPOSE) restart
26130

27131
down: ## Stop and remove containers (not dev)
28-
docker compose -f $(DOCKER_COMPOSE_FILE) down
29-
30-
build: ## Just build all docker images
31-
docker compose -f $(DOCKER_COMPOSE_FILE) build
132+
$(DOCKER_COMPOSE) down
32133

33134
connect: ## Connect to container. usage: make connect <container>
34135
docker exec -it $(filter-out $@,$(MAKECMDGOALS)) /bin/sh
@@ -37,22 +138,8 @@ list: ## List all runnning containers
37138
docker ps -a --format="table {{.Names}}\t{{.Image}}\t{{.Status}}"
38139

39140
##@ [Logs]
40-
log: ## show one contaienr log. usage: make log <container>
141+
log: ## show one contaienr log. usage: make log <contai ner>
41142
docker logs $(filter-out $@,$(MAKECMDGOALS)) -f
42143

43-
all-logs: ## Show all containers logs
44-
docker-compose logs -f
45-
46-
##@ [Profiles: dev]
47-
up-dev: ## Build and start dev profile with PhpMyAdmin, Inotify (run it after make up)
48-
docker compose -f $(DOCKER_COMPOSE_FILE) --profile dev up -d
49-
50-
start-dev: ## Start built containers in dev profile
51-
docker start alpha-core-phpmyadmin-1 alpha-core-inotify-1
52-
53-
stop-dev: ## Stop built containers in dev profile
54-
docker stop alpha-core-phpmyadmin-1 alpha-core-inotify-1
55-
56-
down-dev: ## Stop and remove dev profile containers
57-
docker stop alpha-core-phpmyadmin-1 alpha-core-inotify-1
58-
docker rm alpha-core-phpmyadmin-1 alpha-core-inotify-1
144+
logs: ## Show all containers logs
145+
$(DOCKER_COMPOSE) logs -f

README.docker.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# ![logo](.github/logo-small.png) Alpha Core - Docker & Makefile Guide
2+
3+
---
4+
5+
## ✅ Requirements
6+
7+
- [Docker](https://www.docker.com/products/docker-desktop/) `19.03+`
8+
- Docker Compose v2 (`docker compose`)
9+
10+
---
11+
12+
## ⚡ Quick Start (Local DB)
13+
14+
1. Create `.env`:
15+
```bash
16+
cp env.dist .env
17+
```
18+
1. Create `config.yml`:
19+
```bash
20+
make config
21+
```
22+
2. Ensure local DB is selected:
23+
```bash
24+
make internal_db
25+
```
26+
3. Build and start everything:
27+
```bash
28+
make setup-all
29+
```
30+
31+
> [!NOTE]
32+
> If the database is still starting, `make db-setup` may fail the first time.
33+
> Just rerun it after a few seconds.
34+
>
35+
> If you run `docker compose down -v`, the local DB volume is removed and the databases are empty.
36+
> Run `make db-setup` or `make db-restore` after that.
37+
38+
---
39+
40+
## 🗄️ Database Workflow (Makefile)
41+
42+
- Create databases:
43+
```bash
44+
make db-create
45+
```
46+
- Drop databases:
47+
```bash
48+
make db-drop
49+
```
50+
- Populate schema:
51+
```bash
52+
make db-populate
53+
```
54+
- Apply updates:
55+
```bash
56+
make db-update
57+
```
58+
- Full setup (create → populate → update):
59+
```bash
60+
make db-setup
61+
```
62+
- Backup:
63+
```bash
64+
make db-backup
65+
```
66+
- Restore (use a specific backup folder name):
67+
```bash
68+
make db-restore BACKUP=<folder>
69+
```
70+
71+
> [!NOTE]
72+
> All database names use the prefix from `.env` (`DB_PREFIX`).
73+
74+
---
75+
76+
## 🔌 External Database
77+
78+
1. Set these values in `.env`:
79+
```
80+
EXTERNAL_DB_HOST=
81+
EXTERNAL_DB_PORT=3306
82+
EXTERNAL_DB_USERNAME=
83+
EXTERNAL_DB_PASSWORD=
84+
```
85+
2. Apply external DB config:
86+
```bash
87+
make external_db
88+
```
89+
3. Start containers:
90+
```bash
91+
make up
92+
```
93+
94+
This updates `etc/config/config.yml` with concrete values and disables the local `sql`
95+
service by clearing `COMPOSE_PROFILES`.
96+
97+
---
98+
99+
## 🔁 Switch Back to Local DB
100+
101+
```bash
102+
make internal_db
103+
make up
104+
```
105+
106+
This rewrites `etc/config/config.yml` with local values and enables the `localdb` profile.
107+
108+
---
109+
110+
## ⚙️ Important `.env` Values
111+
112+
- `DB_PREFIX=alpha_`
113+
Database names become `${DB_PREFIX}auth`, `${DB_PREFIX}realm`, `${DB_PREFIX}world`, `${DB_PREFIX}dbc`.
114+
- `MYSQL_PORT=3306`
115+
Port used *inside* the Docker network.
116+
- `MYSQL_HOST_PORT=3300`
117+
Port exposed on your host (useful when 3306 is already in use).
118+
- `COMPOSE_PROFILES=localdb`
119+
Enables the local MariaDB container.
120+
121+
---
122+
123+
## 🛠️ Permissions Tip
124+
125+
If `config.yml` is owned by root (often created by a container), fix it once:
126+
```bash
127+
sudo chown $USER:$USER etc/config/config.yml
128+
```

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
## ⚙️ Configuration
2121

22+
For Docker Compose + Makefile usage, see `README.docker.md`.
23+
2224
1. In `etc/config`, create a copy of `config.yml.dist` and rename it to `config.yml`.
2325
Edit the file as needed for your setup.
2426

@@ -47,19 +49,15 @@
4749
> host: 127.0.0.1
4850
> ```
4951

50-
5. In `etc/databases`, run `create_databases.sql` with a root (or equivalent) user. This creates:
51-
- User: `alphapython`
52-
- Databases: `alpha_auth`, `alpha_realm`, `alpha_world`, `alpha_dbc`
53-
54-
> [!NOTE]
55-
> If you're using Docker and want to use the `alphapython` user, you may need to change
56-
> `'alphapython'@'localhost'` to `'alphapython'@'IPv4OfYourDockerContainer'` in `create_databases.sql`
57-
> if you encounter permission errors.
52+
5. Create the databases using the utility scripts:
53+
- `make db-create`
54+
- Uses `.env` values (`MYSQL_USERNAME`, `MYSQL_PASSWORD`, `DB_PREFIX`)
55+
- Creates `${DB_PREFIX}auth`, `${DB_PREFIX}realm`, `${DB_PREFIX}world`, `${DB_PREFIX}dbc`
5856

5957
6. Each folder (`auth`, `dbc`, `realm`, `world`) in `etc/databases` contains:
6058
- Base SQL files
6159
- Updates in the `/updates` subfolder
62-
Example: `dbc/updates` should be applied to the `alpha_dbc` database.
60+
Example: `dbc/updates` should be applied to the `${DB_PREFIX}dbc` database.
6361

6462
---
6563

@@ -83,6 +81,8 @@
8381
- [Docker](https://www.docker.com/products/docker-desktop/) `19.03+`
8482
- `docker-compose` `1.28+` (install with `pip3 install docker-compose` if needed)
8583
84+
See `README.docker.md` for Docker Compose and Makefile workflows.
85+
8686
- Start the containers:
8787
```bash
8888
docker-compose up -d

0 commit comments

Comments
 (0)