Docker, lessons learned

@MrDanack

Press 'c' to toggle code style
Press 's' for speaker notes

Developer time is expensive

Not using docker already

Adding files to containers

  • Other languages: default to no file, then add
  • PHP: default to all file, then exclude

Docker too slow for npm/yarn/webpack on OSX

Dockerfile tips

# Use repos, not the 'official' php image
FROM debian:9-slim

# root is technically bad
USER root

# This thing is going to change more often:
RUN apt-get install imagick_version_3.2.1

# Than this thing.
RUN apt-get install php_and_everything_else

WORKDIR /var/app

# Running commands through bash scripts is
# far easier to maintain
CMD sh /var/app/php_fpm/entrypoint.sh

Dockerfile tips

# Use repos, not the 'official' php image
FROM debian:9-slim

# root is technically bad
USER root

# Having them this way round
RUN apt-get install php_and_everything_else

# means the previous step can be cached
RUN apt-get install imagick_version_3.2.2

WORKDIR /var/app

# Running commands through bash scripts is
# far easier to maintain
CMD sh /var/app/php_fpm/entrypoint.sh

Bootstrap container

php composer.phar install

# Waits for DB to be available
php cli.php misc:wait_for_db

php vendor/bin/phinx migrate

sh buildSassToCss.sh

php cli.php seed:initial

echo "Bootstrap is finished"

Forget Docker networking

 

  • Setup loopback address on host machine
  • Available both from the host and the docker boxes.
  • com.ralphschindler.docker_10254_alias.plist
  • https://gist.github.com/ralphschindler/535dc5916ccbd06f53c1b0ee5a868c93

Not setting WORKDIR


WORKDIR /var/www

Dependencies between containers

Except xdebug enabled

Docker file


FROM php_backend:latest

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y \
  --no-install-recommends php7.2-xdebug

COPY xdebug.ini /etc/php/7.2/fpm/conf.d/20-xdebug.ini
xdebug.ini
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_autostart=1

; Remote connect back doesn't work in docker
; as the incoming request IP doesn't map back
; to the host properly.
xdebug.remote_connect_back=0

; Loopback ftw
xdebug.remote_host=10.254.254.254

Xdebug always available locally

80 Varnish ➔ Nginx ➔ PHP
8000 Nginx ➔ PHP
8001 Nginx ➔ PHP with Xdebug

One process per container?

This is a case of failing to tell what is good, from what is convenient in a new tech.

Script Two things at the same time
https://gist.github.com/Danack/15e88f28e2b3504223c06582d5650bcc

Domain names

Bad: api.local.foobar.com

Good: local.api.foobar.com

Domain names

Good
server_name
  *.api.phpimagick.com
  api.phpimagick.com;
Tedious
server_name
  api.local.phpimagick.com
  api.staging.phpimagick.com
  api.test.phpimagick.com
  api.daninvestigatingbug.phpimagick.com
  api.phpimagick.com;

Docker bugs

Stop everything starting

docker update --restart=no $(docker ps -a -q)

Clean everything

docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
docker network rm $(docker network ls -q)

Config files belong in repo for containers

No config used from /etc


/usr/sbin/php-fpm7.2 \
  --fpm-config=/var/php_backend/php_fpm.conf \
  --daemonize \
  -c /var/php_backend/php.ini

Supervisord is still rather awesome.

Both Docker and Kubernetes are reinventing the wheel for this tool.

Fin

github.com/danack/example
https://joind.in/talk/d1add