Skip to content

Commit 4d2dbc8

Browse files
committed
Refactor Docker configuration to use environment variables from .env file
- Modified dockerfiles to reduce hardcoded values and use environment variables - Updated docker-compose.yml to source ports and credentials from environment file - Enhanced check_services.sh to read from .env instead of using hardcoded values - Improved README.md with instructions for environment file setup - Fixed container health checks to use the correct service addresses
1 parent 88e1414 commit 4d2dbc8

File tree

5 files changed

+62
-53
lines changed

5 files changed

+62
-53
lines changed

Dockerfile.n8n

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
FROM n8nio/n8n:1.89.2
22

33
# Define build arguments
4-
ARG N8N_PORT=5678
54
ARG NODE_ENV=production
6-
7-
# Add custom environment variables
8-
ENV N8N_PORT=${N8N_PORT} \
9-
NODE_ENV=${NODE_ENV} \
10-
N8N_METRICS=true \
11-
N8N_HEALTH_CHECK_ENDPOINT=true
5+
# Environment variables are now defined in docker-compose.yml
126

137
# Create app directory
148
WORKDIR /home/node
159

1610
# Add custom healthcheck using exec form
1711
HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=30s \
18-
CMD ["/bin/sh", "-c", "wget -q --spider http://n8n:${N8N_PORT}/healthz || exit 1"]
12+
CMD ["/bin/sh", "-c", "wget -q --spider http://0.0.0.0:${N8N_PORT}/healthz || exit 1"]
1913

2014
# Explicitly set the user to the non-root 'node' user (which is already set up in the base image)
2115
USER node
2216

17+
EXPOSE ${N8N_PORT}
2318
# The entrypoint script is already defined in the base image
2419
# Don't override the CMD

Dockerfile.temporal

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
FROM temporalio/auto-setup:1.20.5
22

3-
# Define build arguments with defaults matching the .env file
4-
ARG POSTGRES_USER=temporal
5-
ARG POSTGRES_PASSWORD=temporal
6-
ARG POSTGRES_DB=temporal
7-
ARG DB_PORT=5432
8-
ARG POSTGRES_SEEDS=postgresql
9-
ARG ES_SEEDS=opensearch
10-
ARG ES_VERSION=v7
3+
# Build arguments are still needed for the temporal container setup
4+
# Keeping only those used in the HEALTHCHECK or other commands
5+
ARG HOST=temporal
6+
ARG TEMPORAL_PORT=7233
117

12-
# Set up environment variables for PostgreSQL and OpenSearch
13-
ENV DB=postgresql \
14-
DB_PORT=${DB_PORT} \
15-
POSTGRES_USER=${POSTGRES_USER} \
16-
POSTGRES_PWD=${POSTGRES_PASSWORD} \
17-
POSTGRES_SEEDS=${POSTGRES_SEEDS} \
18-
ENABLE_ES=true \
19-
ES_SEEDS=${ES_SEEDS} \
20-
ES_VERSION=${ES_VERSION}
8+
# Environment variables are now defined in docker-compose.yml
219

2210
# Add custom healthcheck using exec form
2311
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
24-
CMD ["/bin/sh", "-c", "temporal operator cluster health --address temporal:7233 | grep -q SERVING || exit 1"]
12+
CMD ["/bin/sh", "-c", "temporal operator cluster health --address ${HOST}:${TEMPORAL_PORT} | grep -q SERVING || exit 1"]
2513

2614
# Explicitly set the user to the non-root 'temporal' user (already defined in the base image)
2715
USER temporal
2816

2917
# Expose the gRPC port
30-
EXPOSE 7233
18+
EXPOSE ${TEMPORAL_PORT}
3119

3220
# The entrypoint script is already defined in the base image

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ This project uses custom Docker images built from the following Dockerfiles:
2727
Before starting the services, run the setup script to create the necessary volume directories:
2828

2929
```bash
30-
chmod +x scripts/setup_volumes.sh
3130
./scripts/setup_volumes.sh
3231
```
3332

3433
This prevents volume mount errors that may occur if the directories don't exist.
3534

35+
### Create environment file
36+
37+
Create a `.env` file in the root directory of the project with your environment variables:
38+
39+
```bash
40+
cp .env.example .env
41+
```
42+
43+
Then edit the `.env` file to set your specific configuration values.
44+
3645
### Starting the services
3746

3847
```bash

docker-compose.yml

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ services:
55
build:
66
context: .
77
dockerfile: Dockerfile.n8n
8-
args:
9-
- N8N_PORT=5678
108
restart: unless-stopped
119
ports:
12-
- "5678:5678"
10+
- "${N8N_PORT}:5678"
1311
environment:
1412
- WEBHOOK_URL=${N8N_WEBHOOK_URL}
1513
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
14+
- "N8N_RUNNERS_ENABLED=true"
15+
- "N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true"
16+
- "N8N_PORT=5678"
17+
- "NODE_ENV=production"
18+
- "N8N_METRICS=true"
19+
- "N8N_HEALTH_CHECK_ENDPOINT=true"
1620
volumes:
1721
- n8n_data:/home/node/.n8n
1822
networks:
@@ -31,7 +35,7 @@ services:
3135
- "DISABLE_SECURITY_PLUGIN=${DISABLE_SECURITY_PLUGIN}"
3236
- "DISABLE_INSTALL_DEMO_CONFIG=true"
3337
ports:
34-
- 9200:9200
38+
- ${OPENSEARCH_PORT}:9200
3539
cap_add:
3640
- IPC_LOCK
3741
ulimits:
@@ -43,7 +47,7 @@ services:
4347
networks:
4448
- app-network
4549
healthcheck:
46-
test: ["CMD-SHELL", "curl -sSf http://localhost:9200/ || exit 1"]
50+
test: ["CMD-SHELL", "curl -sSf http://localhost:${OPENSEARCH_PORT}/ || exit 1"]
4751
interval: 30s
4852
timeout: 10s
4953
retries: 5
@@ -58,7 +62,7 @@ services:
5862
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
5963
POSTGRES_DB: ${POSTGRES_DB}
6064
ports:
61-
- 5432:5432
65+
- ${POSTGRES_PORT}:5432
6266
volumes:
6367
- postgresql-data:/var/lib/postgresql/data
6468
networks:
@@ -77,11 +81,8 @@ services:
7781
context: .
7882
dockerfile: Dockerfile.temporal
7983
args:
80-
- POSTGRES_USER=${POSTGRES_USER}
81-
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
82-
- POSTGRES_DB=${POSTGRES_DB}
83-
- ES_SEEDS=opensearch
84-
- ES_VERSION=v7
84+
- HOST=temporal
85+
- TEMPORAL_PORT=${TEMPORAL_PORT}
8586
restart: unless-stopped
8687
depends_on:
8788
postgresql:
@@ -91,8 +92,16 @@ services:
9192
environment:
9293
- ES_SEEDS=opensearch
9394
- ES_VERSION=v7
95+
- DB=postgresql
96+
- DB_PORT=${POSTGRES_PORT}
97+
- POSTGRES_USER=${POSTGRES_USER}
98+
- POSTGRES_PWD=${POSTGRES_PASSWORD}
99+
- POSTGRES_SEEDS=postgresql
100+
- ENABLE_ES=true
101+
- HOST=temporal
102+
- TEMPORAL_PORT=7233
94103
ports:
95-
- 7233:7233
104+
- ${TEMPORAL_PORT}:7233
96105
networks:
97106
- app-network
98107
user: temporal
@@ -105,14 +114,14 @@ services:
105114
temporal:
106115
condition: service_healthy
107116
environment:
108-
- TEMPORAL_ADDRESS=temporal:7233
117+
- TEMPORAL_ADDRESS=temporal:${TEMPORAL_PORT}
109118
- TEMPORAL_PERMIT_WRITE_API=true
110119
ports:
111-
- 8080:8080
120+
- ${TEMPORAL_UI_PORT}:8080
112121
networks:
113122
- app-network
114123
healthcheck:
115-
test: ["CMD", "wget", "-O", "/dev/null", "-q", "http://localhost:8080"]
124+
test: ["CMD", "wget", "-O", "/dev/null", "-q", "http://temporal-ui:${TEMPORAL_UI_PORT}"]
116125
interval: 10s
117126
timeout: 5s
118127
retries: 5

scripts/check_services.sh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#!/bin/bash
22

3+
# Source environment variables from .env file
4+
if [ -f .env ]; then
5+
source .env
6+
else
7+
echo "Error: .env file not found. Please make sure it exists in the root directory."
8+
exit 1
9+
fi
10+
311
echo "Checking service availability..."
412

513
# Check if docker-compose is running
@@ -23,31 +31,31 @@ check_http_service() {
2331
}
2432

2533
# Check n8n
26-
check_http_service "n8n" "http://localhost:5678/healthz"
34+
check_http_service "n8n" "http://localhost:${N8N_PORT}/healthz"
2735

2836
# Check temporal-ui
29-
check_http_service "temporal-ui" "http://localhost:8080"
37+
check_http_service "temporal-ui" "http://localhost:${TEMPORAL_UI_PORT}"
3038

3139
# Check opensearch
32-
check_http_service "opensearch" "http://localhost:9200"
40+
check_http_service "opensearch" "http://localhost:${OPENSEARCH_PORT}"
3341

3442
# Check temporal service
35-
echo -n "Checking temporal at localhost:7233... "
36-
if nc -z localhost 7233 >/dev/null 2>&1; then
43+
echo -n "Checking temporal at localhost:${TEMPORAL_PORT}... "
44+
if nc -z localhost ${TEMPORAL_PORT} >/dev/null 2>&1; then
3745
echo "ACCESSIBLE ✅"
3846
else
3947
echo "NOT ACCESSIBLE ❌"
4048
fi
4149

4250
# Check PostgreSQL
43-
echo -n "Checking postgresql at localhost:5432... "
44-
if docker compose exec postgresql pg_isready -h localhost -p 5432 -U temporal >/dev/null 2>&1; then
51+
echo -n "Checking postgresql at localhost:${POSTGRES_PORT}... "
52+
if docker compose exec postgresql pg_isready -h localhost -p ${POSTGRES_PORT} -U ${POSTGRES_USER} >/dev/null 2>&1; then
4553
echo "ACCESSIBLE ✅"
4654
else
4755
echo "NOT ACCESSIBLE ❌"
4856
fi
4957

5058
echo -e "\nService URLs:"
51-
echo "- n8n: http://localhost:5678"
52-
echo "- Temporal UI: http://localhost:8080"
53-
echo "- OpenSearch: http://localhost:9200"
59+
echo "- n8n: http://localhost:${N8N_PORT}"
60+
echo "- Temporal UI: http://localhost:${TEMPORAL_UI_PORT}"
61+
echo "- OpenSearch: http://localhost:${OPENSEARCH_PORT}"

0 commit comments

Comments
 (0)