Emacs editing in a Drupal Docker container

Create a Drupal Docker image with support for Emacs editing set up (HTML, CSS, PHP files). With phpMyAdmin support. Under Debian Linux of course.

My Emacs configuration is used as an example to configure Emacs in the container.

Define the image via a Dockerfile

The Dockerfile (Dockerfile-web):

 1# Basic PHP container for Drupal PHP editing with Emacs.
 2#
 3# This Dockerfile is used from the Docker Compose file.
 4#
 5# Twitter: @maridonkers | Google+: +MariDonkers | GitHub: maridonkers.
 6#
 7# Docker image: https://hub.docker.com/_/drupal/
 8#
 9# BEWARE: firewall must allow docker interface for 3306 (otherwise connection errors).
10#       : use http://127.0.0.1:8081 to access Drupal GUI if localhost doesn't work;
11
12FROM drupal
13
14RUN sed -i "s#\smain\s*\$# main contrib non-free#" /etc/apt/sources.list
15
16RUN apt-get update && apt-get install -yq procps iproute2 sudo git-core zip curl gnupg
17RUN apt-get install -yq emacs25 vim silversearcher-ag
18
19ENV COMPOSER_ALLOW_SUPERUSER=1
20RUN mkdir -p /opt/php
21RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/opt/php --filename=composer
22RUN ln -s /opt/php/composer /usr/local/bin/composer
23
24RUN sed -i "s#^\(www-data:.*:\)/usr/sbin/nologin#\1/bin/bash#" /etc/passwd
25RUN echo "www-data ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
26# RUN usermod -u 1000 www-data
27# RUN usermod -G staff www-data
28RUN chown www-data /var/www
29WORKDIR /var/www
30
31ENV DISPLAY=:0

The Docker compose file

 1# Basic PHP container for Drupal PHP editing with Emacs.
 2#
 3# Twitter: @maridonkers | Google+: +MariDonkers | GitHub: maridonkers.
 4#
 5# Docker image: https://hub.docker.com/_/drupal/
 6
 7version: '3'
 8services:
 9  web:
10    # image: drupal
11    build: 
12      context: .
13      dockerfile: Dockerfile-web
14    environment:
15       DISPLAY:
16    network_mode: host
17    # ports:
18    #   - "8000:80"
19    stdin_open: true
20    tty: true
21  db:
22    image: mysql:5.7
23    # volumes:
24      # - ./db-backups:/var/mysql/backups:delegated
25    environment:
26      MYSQL_ROOT_PASSWORD: root
27      MYSQL_DATABASE: drupal1
28      MYSQL_USER: drupal1
29      MYSQL_PASSWORD: drupal1
30    networks:
31      - back
32    ports:
33      - "3306:3306"
34  pma:
35    image: phpmyadmin/phpmyadmin
36    environment:
37      PMA_HOST: db
38      PMA_USER: root
39      PMA_PASSWORD: root
40      PHP_UPLOAD_MAX_FILESIZE: 1G
41      PHP_MAX_INPUT_VARS: 1G
42    networks:
43      - back
44    ports:
45     - "8001:80"
46
47networks:
48  back:

Compose up

To build images and bring them up.

 1$ docker-compose up --build -d
 2Creating network "drupal1_default" with the default driver
 3Building web
 4Step 1/9 : FROM drupal
 5 ---> c362e86c8769
 6Step 2/9 : RUN sed -i "s#\smain\s*\$# main contrib non-free#" /etc/apt/sources.list
 7 ---> Using cache
 8 ---> 5624e4872798
 9Step 3/9 : RUN apt-get update && apt-get install -yq procps iproute2 sudo git-core zip curl gnupg
10 ---> Using cache
11 ---> 69d38cb8e0f2
12Step 4/9 : RUN apt-get install -yq emacs25 vim silversearcher-ag
13 ---> Using cache
14 ---> 64b319596a11
15Step 5/9 : RUN sed -i "s#^\(www-data:.*:\)/usr/sbin/nologin#\1/bin/bash#" /etc/passwd
16 ---> Using cache
17 ---> 24a125040132
18Step 6/9 : RUN echo "www-data ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
19 ---> Using cache
20 ---> 9ee3dddfffdb
21Step 7/9 : RUN chown www-data /var/www
22 ---> Using cache
23 ---> 10062f436b3c
24Step 8/9 : WORKDIR /var/www
25 ---> Using cache
26 ---> 73e32a4bb1f9
27Step 9/9 : ENV DISPLAY=:0
28 ---> Using cache
29 ---> 0757563ba445
30Successfully built 0757563ba445
31Successfully tagged drupal1_web:latest
32Creating drupal1_pma_1 ... done
33Creating drupal1_web_1 ... done
34Creating drupal1_db_1  ... done

Visit http://localhost:80 for the Drupal site. Visit http://localhost:8001 for the phpMyAdmin interface.

MySQL configuration

In the Drupal configuration screen use host 127.0.0.1 with port 3306 (plain localhost will not work).

Connect to development image

First enable access for X-Windows:

1xhost +LOCAL:

Execute a bash shell in the container and set up Emacs.

1$ docker exec -ti drupal1_web_1 /bin/bash

From the bash shell in the container:

 1# su - www-data
 2
 3www-data$ git clone https://github.com/maridonkers/emacs-config.git /var/www/.emacs.d
 4Cloning into '/var/www/.emacs.d'...
 5remote: Enumerating objects: 114, done.
 6remote: Counting objects: 100% (114/114), done.
 7remote: Compressing objects: 100% (80/80), done.
 8remote: Total 545 (delta 68), reused 79 (delta 34), pack-reused 431
 9Receiving objects: 100% (545/545), 136.94 KiB | 0 bytes/s, done.
10Resolving deltas: 100% (316/316), done.
11
12www-data$ emacs --daemon
13...
14Loading /var/www/.emacs.d/loader.el (source)...done
15Loaded /var/www/.emacs.d/loader.el
16No desktop file.
17Saving file /var/www/.emacs.d/init.el...
18Wrote /var/www/.emacs.d/init.el
19Wrote /var/www/.emacs.d/init.el
20Starting Emacs daemon.
21
22www-data$ emacsclient -nc html/index.php

If the emacsclient command doesn't work the first time then restart the Emacs daemon by repeating the emacs --daemon and emacsclient -nc html/index.php commands.

Emacs running

Posts in this Series