Public | Automated Build

Last pushed: 4 months ago
Short Description
Using Docker with Drupal - Introduction
Full Description

Using Docker with Drupal - Introduction

DrupalCamp 2016, 1-3 July 2016

TheodorosPloumis.com / @theoploumis

Get them: online presentation / source code / docker image / Video
Under Attribution 4.0 International license.

What is Docker

Docker is an open platform for developing, shipping, and running applications.


Docker History

When Who License GH stars
03/2013 Solomon Hykes Apache 37k

Docker Benefits

  • Fast (deployment, migration, restarts)
  • Secure
  • Lightweight (save disk & CPU)
  • Open Source
  • Portable software
  • Microservices and integrations (APIs)
  • Simplify DevOps
  • Version control capabilities

Common Docker usages

  • Scaling apps
  • Development collaboration
  • Infrastructure configuration
  • Sandbox (develop, test, debug, educate etc)
  • Local development
  • Continuous Integration & Deployment
  • Multi-tier applications
  • PaaS, SaaS
See the survey results for 2016

Technology stack

See more at Understanding docker

Docker components

  • (Docker) client
  • daemon
  • engine
  • machine
  • compose
  • swarm
  • registry

Docker client

It is the primary user interface to Docker. It accepts commands from the user
and communicates back and forth with a Docker daemon.


Docker daemon

It runs on a host machine. The user does not directly interact with the daemon,
but instead through the Docker client with the RESTful api or sockets.


Docker engine

A Client with a Daemon as also as the docker-compose tool. Usually referred simply as "docker".


Docker machine

It creates servers (locally, on cloud etc), installs Docker on them, then configures the Docker client to talk to them.


Docker compose

A tool for defining and running complex applications with Docker
(eg a multi-container application) with a single file.


Docker swarm

Swarm pools together several Docker hosts and exposes them as a single virtual Docker host (cluster).
It scale up to multiple hosts.


Docker distribution

A (hosted) service containing repositories of images which responds to the Registry API.


Steps of a Docker workflow

docker run -i -t ubuntu:15.04 /bin/bash

Screencast: Steps of a Docker workflow.


The Docker Components diagram


The docker image


The docker container


The Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to create an image.


Docker examples

  • SSH into a container
  • Common Docker commands
  • Linked containers
  • Docker Volume
  • Using docker-compose
  • Advanced usage of docker-compose
  • Scale containers with docker-compose

Example: SSH into a container

docker pull ubuntu
docker run -it --name ubuntu_example ubuntu /bin/bash

Screencast: SSH into a container


Example: Common Docker Commands

// General info
docker help // docker help run
docker info
docker version
docker network ls

// Images
docker images // docker [IMAGE_NAME]
docker pull [IMAGE] //
docker push [IMAGE]
docker save [IMAGE] // commit, tag, export, load, rmi

// Containers
docker run ...
docker ps // docker ps -a, docker ps -l
docker stop/start/restart [CONTAINER]
docker stats [CONTAINER]
docker top [CONTAINER]
docker port [CONTAINER]
docker inspect [CONTAINER]
docker inspect -f "{{ .State.StartedAt }}" [CONTAINER]
docker rm [CONTAINER]

Screencast: Common Docker commands


Example: Docker link containers

Let's create a Drupal app (apache, php, mysql, drupal)

docker pull drupal:8.2.3-apache
docker pull mysql:8

// Start a container for mysql
docker run --name mysql_example \
           -e MYSQL_ROOT_PASSWORD=root \
           -e MYSQL_DATABASE=drupal \
           -e MYSQL_USER=drupal \
           -e MYSQL_PASSWORD=drupal \
           -d mysql:8

// Start a Drupal container and link it with mysql
// Usage: --link [name or id]:alias
docker run -d --name drupal_example \
           -p 8280:80 \
           --link mysql_example:mysql \
           drupal:8.2.3-apache

// Open http://localhost:8280 to continue with the installation
// On the UI for the db host use: mysql

// There is a proper linking
docker inspect -f "{{ .HostConfig.Links }}" drupal_example

Screencast: Docker: Drupal and MySQL containers linked


Example: Docker volume

Let's mount local files to a docker container

cd ~/drupalcamp2016
drush dl drupal-8.2.3
cd ~/drupalcamp2016/drupal-8.2.3

// Start a container for mysql
docker run --name mysql_container \
           -e MYSQL_ROOT_PASSWORD=root \
           -e MYSQL_DATABASE=drupal \
           -e MYSQL_USER=drupal \
           -e MYSQL_PASSWORD=drupal \
           -d mysql:8

// Start drupal container with volume
// Folder "modules" locally is mounted to the container
docker run -d --name drupal_with_mysql_volumed \
           -p 8290:80 \
           --link mysql_container:mysql \
           -v $(pwd)/modules:/var/www/html/modules \
           drupal:8.2.3-apache

// Locally download devel module (assuming you have drush on host)
drush dl devel
// Devel module is available on the container

Example: Using Docker Compose

Let's create a Drupal app using docker-compose.yml

git clone git@github.com:theodorosploumis/drupalcamp2016.git \
          ~/drupalcamp2016/presentation
cd ~/drupalcamp2016/presentation/examples/docker-compose/simple

// Run docker-compose using the docker-compose.yml
docker-compose up -d

Screencast: Docker compose simple example


Example: More advanced docker-compose

Drupal + friends together.

cd ~/drupalcamp2016
git clone git@github.com:theodorosploumis/drupal-docker.git
cd ~/drupalcamp2016/drupal-docker

docker-compose up -d

// Open http://localhost:8082 (web)
// Open http://localhost:8090 (phpmyadmin)

// Prepare Drupal for installation
docker exec drupal_8082 bash /scripts/prepare-install.sh

// Install with Drush
docker exec drupal_8082 drush \
    site-install -y standard \
    --site-name="Drupal 8 with Docker - Drush" \
    --db-url=mysql://drupal:drupal@mysql/drupal \
    --site-mail=admin@example.com \
    --account-name=admin \
    --account-pass=admin \
    --account-mail=admin@example.com

Screencast: Drupal & PhpMyAadmin, Drush, Console etc


Example: Scale containers with docker-compose

cd ~/drupalcamp2016
git clone git@github.com:theodorosploumis/drupal-docker.git
cd ~/drupalcamp2016/drupal-docker

docker-compose up -d
// Continue as before and install Drupal with drush...

// Scale the Drupal slave service
docker-compose scale drupal_slave=10

// Show the new slave containers IP Addresses
docker exec drupaldocker_drupal_slave_1 hostname -I

// Show the IP Address using Docker
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
               drupaldocker_drupal_slave_1
...

// Slaves share the same mysql and files
docker exec drupaldocker_drupal_slave_1 drush status
docker exec drupaldocker_drupal_slave_2 drush status
...

Screencast: Scale Drupal with docker-compose


Docker for Drupal recipes

The Drupal "parts"

  • server
  • code
  • database
  • public/private files
  • helper software (eg drush, composer etc)

Docker for Drupal - Combinations and Options

A/a Part Docker options
1 Server Image
2 Code Image, Volumes, Inside 1
3 Files None (stage_file_proxy), Image, Inside 2
4 Database Image, Inside 1
5 Helpers Image, Inside 1, Volumes, None

From Monolithic apps


To Containerized apps


Docker tips

There are known best practices (see a list at examples/tips)

  • Optimize containers (see imagelayers.io)
  • Create your own, tiny base images
  • Containers are not Virtual Machines
  • Full stack Images VS 1 process per Container
  • Use files/scripts to setup container
  • Prefer using docker-compose
  • Production needs orchestration

The Docker war

Type Software
Cluster & <br>orchestrate Swarm, Kubernetes, Marathon, MaestroNG, decking, shipyard
Registry Portus, Docker Distribution, docker hub, quay.io, Google Container Reg., Artifactory, projectatomic.io, Treescale.com, Canister.io
PaaS Rancher, Tsuru, dokku, flynn, Octohost

Docker alternatives


Instead of Resources


Questions?

$this->send('feedback');


Tools used: docker 1.12.3 / docker compose 1.9.0 / asciinema / oh my zsh / reveal.js /
Docker Pull Command
Owner
tplcom
Source Repository

Comments (0)