Skip to content

Commit d814c13

Browse files
FIX (dockefile): Verify DB is not exists before creation in the image
1 parent 41fe554 commit d814c13

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

Dockerfile

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ COPY frontend/ ./
1313

1414
# Copy .env file (with fallback to .env.production.example)
1515
RUN if [ ! -f .env ]; then \
16-
if [ -f .env.production.example ]; then \
17-
cp .env.production.example .env; \
18-
fi; \
19-
fi
16+
if [ -f .env.production.example ]; then \
17+
cp .env.production.example .env; \
18+
fi; \
19+
fi
2020

2121
RUN npm run build
2222

@@ -58,9 +58,9 @@ ARG TARGETOS
5858
ARG TARGETARCH
5959
ARG TARGETVARIANT
6060
RUN CGO_ENABLED=0 \
61-
GOOS=$TARGETOS \
62-
GOARCH=$TARGETARCH \
63-
go build -o /app/main ./cmd/main.go
61+
GOOS=$TARGETOS \
62+
GOARCH=$TARGETARCH \
63+
go build -o /app/main ./cmd/main.go
6464

6565

6666
# ========= RUNTIME =========
@@ -71,22 +71,25 @@ ARG APP_VERSION=dev
7171
LABEL org.opencontainers.image.version=$APP_VERSION
7272
ENV APP_VERSION=$APP_VERSION
7373

74+
# Set production mode for Docker containers
75+
ENV ENV_MODE=production
76+
7477
# Install PostgreSQL server and client tools (versions 13-17)
7578
RUN apt-get update && apt-get install -y --no-install-recommends \
76-
wget ca-certificates gnupg lsb-release sudo gosu && \
77-
wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
78-
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
79-
> /etc/apt/sources.list.d/pgdg.list && \
80-
apt-get update && \
81-
apt-get install -y --no-install-recommends \
82-
postgresql-17 postgresql-client-13 postgresql-client-14 postgresql-client-15 \
83-
postgresql-client-16 postgresql-client-17 && \
84-
rm -rf /var/lib/apt/lists/*
79+
wget ca-certificates gnupg lsb-release sudo gosu && \
80+
wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
81+
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
82+
> /etc/apt/sources.list.d/pgdg.list && \
83+
apt-get update && \
84+
apt-get install -y --no-install-recommends \
85+
postgresql-17 postgresql-client-13 postgresql-client-14 postgresql-client-15 \
86+
postgresql-client-16 postgresql-client-17 && \
87+
rm -rf /var/lib/apt/lists/*
8588

8689
# Create postgres user and set up directories
8790
RUN useradd -m -s /bin/bash postgres || true && \
88-
mkdir -p /postgresus-data/pgdata && \
89-
chown -R postgres:postgres /postgresus-data/pgdata
91+
mkdir -p /postgresus-data/pgdata && \
92+
chown -R postgres:postgres /postgresus-data/pgdata
9093

9194
WORKDIR /app
9295

@@ -105,10 +108,10 @@ COPY --from=backend-build /app/ui/build ./ui/build
105108
# Copy .env file (with fallback to .env.production.example)
106109
COPY backend/.env* /app/
107110
RUN if [ ! -f /app/.env ]; then \
108-
if [ -f /app/.env.production.example ]; then \
109-
cp /app/.env.production.example /app/.env; \
110-
fi; \
111-
fi
111+
if [ -f /app/.env.production.example ]; then \
112+
cp /app/.env.production.example /app/.env; \
113+
fi; \
114+
fi
112115

113116
# Create startup script
114117
COPY <<EOF /app/start.sh
@@ -160,7 +163,8 @@ done
160163
echo "Setting up database and user..."
161164
gosu postgres \$PG_BIN/psql -p 5437 -h localhost -d postgres << 'SQL'
162165
ALTER USER postgres WITH PASSWORD 'Q1234567';
163-
CREATE DATABASE "postgresus" OWNER postgres;
166+
SELECT 'CREATE DATABASE postgresus OWNER postgres'
167+
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'postgresus')\gexec
164168
\q
165169
SQL
166170

0 commit comments

Comments
 (0)