Skip to content

Commit ca05035

Browse files
committed
add entrypoint script for drush site install and update readme
1 parent 2a17794 commit ca05035

File tree

5 files changed

+150
-8
lines changed

5 files changed

+150
-8
lines changed

.github/workflows/docker-build.yml

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ on:
1111
push:
1212
branches:
1313
- 'main'
14+
paths-ignore:
15+
- 'example/**'
16+
- 'README.md'
17+
- 'docs/**'
1418

1519
jobs:
1620
docker-build:

Dockerfile

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Start from the original Drupal image
22
FROM drupal:10.2.6-php8.2-apache-bookworm
3+
# Set default environment variables
4+
# Set default config sync directory
5+
ENV DRUPAL_CONFIG_SYNC_DIRECTORY /var/configsync
36
# Set working directory to root of drupal project set in base image
47
WORKDIR /opt/drupal
58
# Install drush and any additional Drupal extensions using Composer
@@ -14,12 +17,16 @@ RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
1417
RUN pecl install apcu \
1518
&& docker-php-ext-enable apcu
1619
# Configure APCu
17-
RUN echo "apc.enabled=1" >> /usr/local/etc/php/php.ini \
18-
&& echo "apc.shm_size=256M" >> /usr/local/etc/php/php.ini \
19-
&& echo "apc.ttl=7200" >> /usr/local/etc/php/php.ini \
20-
&& echo "apc.enable_cli=0" >> /usr/local/etc/php/php.ini
20+
RUN { \
21+
echo "apc.enabled=1"; \
22+
echo "apc.shm_size=256M"; \
23+
echo "apc.ttl=7200"; \
24+
echo "apc.enable_cli=0"; \
25+
} > /usr/local/etc/php/conf.d/apcu.ini
2126
# Copy default.settings.php to settings.php
2227
RUN cp /var/www/html/sites/default/default.settings.php /var/www/html/sites/default/settings.php
28+
# Create the DRUPAL_CONFIG_SYNC_DIRECTORY if it does not exist
29+
RUN mkdir -p $DRUPAL_CONFIG_SYNC_DIRECTORY && chown -R www-data:www-data $DRUPAL_CONFIG_SYNC_DIRECTORY
2330
# Append database configuration to settings.php
2431
# Append hash salt to settings.php
2532
# Append configuration sync directory to settings.php
@@ -39,6 +46,12 @@ RUN echo "\$databases['default']['default'] = array (" >> /var/www/html/sites/de
3946
# Set file permissions
4047
RUN chown -R www-data:www-data /var/www/html/sites/default/settings.php && \
4148
chmod 644 /var/www/html/sites/default/settings.php
49+
# Entrypoint script
50+
COPY entrypoint.sh /entrypoint.sh
51+
RUN chmod +x /entrypoint.sh
52+
ENTRYPOINT ["/entrypoint.sh"]
53+
# Run Apache in the foreground
54+
CMD ["apache2-foreground"]
4255
# Docs
43-
LABEL description="Single Dockerfile build of Drupal with drush and defaulted settings including Postgres"
44-
LABEL version="1.0"
56+
LABEL description="Dockerfile build of Drupal with drush and defaulted settings including Postgres"
57+
LABEL version="1.1"

README.md

+82-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,82 @@
1-
# docker-drupal-postgres
2-
Single Dockerfile build of Drupal with drush and defaulted settings including Postgres
1+
# Dockerized Drupal with Drush and Postgres
2+
3+
This Dockerfile creates a Docker image for a Drupal application with Drush and Postgres support. It is based on the official Drupal image and includes additional configurations and extensions to support a more robust Drupal development environment.
4+
5+
[![docker-build](https://github.com/garyGoDev/docker-drupal-postgres/actions/workflows/docker-build.yml/badge.svg)](https://github.com/garyGoDev/docker-drupal-postgres/actions/workflows/docker-build.yml)
6+
7+
[DockerHub cloudgrounds/drupal-postgres](https://hub.docker.com/r/cloudgrounds/drupal-postgres)
8+
9+
## Features
10+
11+
- Based on `drupal:10.2.6-php8.2-apache-bookworm` image from [the official Drupal image](https://hub.docker.com/_/drupal/)
12+
- Includes Drush, a command-line shell and scripting interface for Drupal
13+
- Configured to use Postgres as the database
14+
- Includes APCu PHP extension for caching
15+
- Configures PHP for output buffering
16+
- Sets up default Drupal settings and file permissions
17+
18+
## Usage
19+
20+
To build the Docker image, navigate to the directory containing the Dockerfile and run:
21+
22+
```bash
23+
docker build -t your-custom-image:1.0 .
24+
```
25+
26+
To run drupal from the image use `docker compose` with the example file
27+
28+
```bash
29+
HASH_SALT=yourreallyreallylongandrandomhash DRUPAL_ADMIN_PASS=yourfirstsupersafepassword POSTGRES_PASSWORD=yourothersupersafepassword docker-compose up -d
30+
```
31+
32+
## Environment Variables
33+
34+
The Dockerfile uses the following environment variables:
35+
36+
- `POSTGRES_DB`: The name of your Postgres database
37+
- `POSTGRES_USER`: The username for your Postgres database
38+
- `POSTGRES_PASSWORD`: The password for your Postgres database
39+
- `POSTGRES_HOST`: The host of your Postgres database
40+
- `DRUPAL_HASH_SALT`: The hash salt for your Drupal application
41+
- `DRUPAL_CONFIG_SYNC_DIRECTORY`: The directory for Drupal configuration synchronization files. This should be outside web root. This defaults to `/var/configsync`
42+
- `DRUPAL_ADMIN_USER`: The username for your Drupal admin account
43+
- `DRUPAL_ADMIN_PASS`: The password for your Drupal admin account
44+
- `DRUPAL_ADMIN_EMAIL`: The email for your Drupal admin account
45+
46+
## Volumes
47+
48+
The Dockerfile specifies volumes for the Drupal installation and files directories. This allows you to persist data across container restarts and share data between containers. See the **Volumes** section in the official Drupal image.
49+
50+
## Entrypoint Script
51+
52+
The `entrypoint.sh` script is executed when the Docker container starts. It performs several checks and operations to ensure the Drupal site is properly set up.
53+
54+
### Script Overview
55+
56+
The script performs the following operations:
57+
58+
1. Checks the status of the database connection and Drupal bootstrap using [Drush](https://www.drush.org/).
59+
2. If the database is not connected and Drupal is not bootstrapped, it installs the Drupal site using the `drush site-install` command.
60+
3. If the database is connected or Drupal is bootstrapped, it updates the database using the `drush updatedb` command and rebuilds the cache using the `drush cache-rebuild` command.
61+
4. Finally, it executes the command passed to the `docker run` command.
62+
63+
Environment variables are included above and should be set in your Docker environment or in your `docker-compose.yml` file.
64+
65+
### Drush
66+
67+
[Drush](https://www.drush.org/) is a command-line shell and Unix scripting interface for Drupal. Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes SQL queries and DB migrations, and misc utilities like run cron or clear cache.
68+
69+
For more information about the Drush commands used in this script, see the following links:
70+
71+
- [drush status](https://www.drush.org/13.x/commands/core_status/)
72+
- [drush site-install](https://www.drush.org/13.x/commands/site_install/)
73+
- [drush updatedb](https://www.drush.org/13.x/commands/updatedb/)
74+
- [drush cache-rebuild](https://www.drush.org/13.x/commands/cache_rebuild/)
75+
76+
## Running Apache
77+
78+
By default, the Docker container will run Apache in the foreground.
79+
80+
## Versions
81+
82+
Versions are based on the run number in the GitHub Actions publication workflow.

entrypoint.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# exit on error
3+
set -e
4+
# get drush status for checking if the site is installed
5+
DB_STATUS=$(drush status | grep "Database" | awk '{print $NF}')
6+
echo "Database connected: $DB_STATUS"
7+
DRUPAL_BOOTSTRAP_STATUS=$(drush status | grep "Drupal bootstrap" | awk '{print $NF}')
8+
echo "Drupal bootstrap: $DRUPAL_BOOTSTRAP_STATUS"
9+
# check if the site is installed
10+
if [ "$DB_STATUS" != "Connected" ] && [ "$DRUPAL_BOOTSTRAP_STATUS" != "Successful" ]; then
11+
drush site-install standard --db-url=pgsql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB --site-name=$DRUPAL_SITE_NAME --account-name=$DRUPAL_ADMIN_USER --account-pass=$DRUPAL_ADMIN_PASS --account-mail=$DRUPAL_ADMIN_EMAIL --no-interaction
12+
fi
13+
# if the site is installed, run drush updatedb and cache-rebuild
14+
if [ "$DB_STATUS" == "Connected" ] || [ "$DRUPAL_BOOTSTRAP_STATUS" == "Successful" ]; then
15+
drush updatedb --no-interaction
16+
drush cache-rebuild
17+
fi
18+
# run the command passed to the docker run
19+
exec "$@"

example/docker-compose.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Drupal with PostgreSQL
2+
version: '3.1'
3+
4+
services:
5+
6+
drupal:
7+
image: your-custom-image:1.0
8+
ports:
9+
- 8080:80
10+
environment:
11+
POSTGRES_DB: postgres
12+
POSTGRES_USER: postgres
13+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
14+
POSTGRES_HOST: postgres
15+
DRUPAL_HASH_SALT: ${HASH_SALT}
16+
DRUPAL_SITE_NAME: example
17+
DRUPAL_ADMIN_USER: admin
18+
DRUPAL_ADMIN_PASS: ${DRUPAL_ADMIN_PASS}
19+
DRUPAL_ADMIN_EMAIL: [email protected]
20+
restart: always
21+
22+
postgres:
23+
image: postgres:16
24+
environment:
25+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
26+
restart: always

0 commit comments

Comments
 (0)