Skip to content

Commit 32350eb

Browse files
authored
Merge pull request #1 from GaetanOff/main
Optimize Docker Configuration for Improved Performance and Security
2 parents 2fbf7a1 + e10aa6e commit 32350eb

File tree

4 files changed

+65
-32
lines changed

4 files changed

+65
-32
lines changed

Diff for: Dockerfile

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
FROM php:8.3.11-apache
22

33
WORKDIR /var/www
4-
ENV DEBIAN_FRONTEND noninteractive
5-
ENV WWW_ROOT="/var/www"
4+
ENV DEBIAN_FRONTEND=noninteractive
65

7-
# Install dependencies
8-
RUN apt-get clean && apt-get update -y --allow-insecure-repositories
9-
RUN apt-get install -y \
6+
ARG WWW_ROOT="/var/www"
7+
ENV WWW_ROOT=$WWW_ROOT
8+
9+
# Updating packages and installing dependencies in a single layer with cache cleaning
10+
RUN apt-get update -y --allow-insecure-repositories && \
11+
apt-get install -y --no-install-recommends \
1012
git \
1113
libfreetype6-dev \
1214
libicu-dev \
@@ -17,11 +19,12 @@ RUN apt-get install -y \
1719
rsync \
1820
unzip \
1921
sudo \
20-
zip
22+
zip && \
23+
apt-get clean && rm -rf /var/lib/apt/lists/*
2124

22-
# Install PHP extensions
23-
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
24-
RUN docker-php-ext-install -j$(nproc) \
25+
# Installing PHP extensions with cache cleaning
26+
RUN docker-php-ext-configure gd --with-freetype --with-jpeg && \
27+
docker-php-ext-install -j$(nproc) \
2528
gd \
2629
intl \
2730
mbstring \
@@ -31,18 +34,16 @@ RUN docker-php-ext-install -j$(nproc) \
3134

3235
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
3336

34-
# Install Symfony CLI
35-
RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | sudo -E bash
36-
RUN apt-get install symfony-cli
37-
RUN symfony server:ca:install
37+
# Installation of Symfony CLI with cache cleaning
38+
RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | sudo -E bash && \
39+
apt-get install -y symfony-cli && \
40+
symfony server:ca:install && \
41+
apt-get clean && rm -rf /var/lib/apt/lists/*
3842

3943
# Configure Apache
4044
ADD docker/apache/entrypoint.sh /entrypoint.sh
41-
RUN chmod a+x /entrypoint.sh
42-
43-
RUN a2enmod rewrite
44-
RUN a2enmod remoteip
45-
RUN a2enmod ssl
45+
RUN chmod a+x /entrypoint.sh && \
46+
a2enmod rewrite remoteip ssl
4647

4748
CMD ["/entrypoint.sh"]
4849

Diff for: Makefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ init-symfony:
2020

2121
init: build up init-symfony
2222

23-
rm:
23+
# Command to remove Symfony project files only
24+
rm-symfony:
2425
docker compose exec apache sh -c \
2526
"rm -rf \
2627
./assets \
@@ -42,3 +43,11 @@ rm:
4243
./.env.test \
4344
./.gitignore \
4445
./importmap.php"
46+
47+
# Command to remove Docker containers and volumes
48+
rm-containers:
49+
docker compose down -v
50+
51+
# Command to prune unused Docker data
52+
prune:
53+
docker system prune -f --volumes

Diff for: compose.yaml

+31-10
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
11
name: project-name
22

33
services:
4-
###> doctrine/doctrine-bundle ###
4+
###> doctrine/doctrine-bundle ###
55
database:
66
image: bitnami/mariadb:10.6.19
7+
container_name: project-database
78
environment:
8-
- ALLOW_EMPTY_PASSWORD=yes
9-
- MARIADB_USER=app
10-
- MARIADB_DATABASE=app
9+
- MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD:-root_password}
10+
- MARIADB_USER=${MARIADB_USER:-app}
11+
- MARIADB_PASSWORD=${MARIADB_PASSWORD:-app_password}
12+
- MARIADB_DATABASE=${MARIADB_DATABASE:-app}
1113
volumes:
12-
- ./docker/database:/bitnami/mariadb:rw
14+
- database_data:/bitnami/mariadb
15+
networks:
16+
- project_network
1317
ports:
1418
- 3306:3306
15-
###< doctrine/doctrine-bundle ###
19+
restart: unless-stopped
20+
###< doctrine/doctrine-bundle ###
1621

1722
phpmyadmin:
18-
image: phpmyadmin:latest
23+
image: phpmyadmin:5.2.1
24+
container_name: project-phpmyadmin
1925
ports:
2026
- 8080:80
2127
environment:
2228
- PMA_ARBITRARY=1
2329
- PMA_HOST=database
24-
- PMA_USER=app
25-
- PMA_PASSWORD=
30+
- PMA_USER=${MARIADB_USER:-app}
31+
- PMA_PASSWORD=${MARIADB_PASSWORD:-app_password}
2632
depends_on:
2733
- database
34+
networks:
35+
- project_network
36+
restart: unless-stopped
2837

2938
apache:
3039
build:
3140
context: .
3241
dockerfile: ./Dockerfile
42+
container_name: project-apache
3343
volumes:
3444
- .:/var/www/
3545
- ./docker/apache/apache.conf:/etc/apache2/sites-enabled/000-default.conf
@@ -38,4 +48,15 @@ services:
3848
ports:
3949
- 8000:80
4050
depends_on:
41-
- database
51+
- database
52+
networks:
53+
- project_network
54+
restart: unless-stopped
55+
56+
networks:
57+
project_network:
58+
driver: bridge
59+
60+
volumes:
61+
database_data:
62+
driver: local

Diff for: docker/apache/entrypoint.sh

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
OWNER_UID="$(stat -c %u ${WWW_ROOT})"
44
OWNER_GID="$(stat -c %g ${WWW_ROOT})"
5+
56
if [[ "$OWNER_UID" != "0" ]]; then
6-
usermod -o --uid ${OWNER_UID} www-data
7+
usermod -o --uid ${OWNER_UID} www-data || echo "Warning: Failed to set UID for www-data"
78
fi
89
if [[ "$OWNER_GID" != "0" ]]; then
9-
groupmod -o --gid ${OWNER_GID} www-data
10+
groupmod -o --gid ${OWNER_GID} www-data || echo "Warning: Failed to set GID for www-data"
1011
fi
1112

12-
apache2-foreground
13+
echo "Starting Apache as www-data (UID: ${OWNER_UID}, GID: ${OWNER_GID})"
14+
exec apache2-foreground

0 commit comments

Comments
 (0)