Docker for (local) Development.

Justin King

 

@getafixx

 

Senior Backend Engineer at Billie

We are hiring..

Why choose Docker?

Ever run Vagrant? It sucks, doesn't it? It uses too much memory and connection to your local disk isn't very fast.

 

Docker really makes it easier to create, deploy, and run applications by using containers, and containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies

 

Docker provides this same capability without the overhead of a virtual machine. It lets you put your environment and configuration into code and deploy it. The same Docker configuration can also be used in a variety of environments.

Docker for Local

Development

Assumptions


Docker for Developers is installed - https://www.docker.com/get-started

 

If you are on a windows PC you will have to do some additional setup and BIOS configuration

 

 

 

Folder setup

2019-03-05-docker-for-development/
├── another-site.com/
│   ├── index.html
├── laravelberlin.de/
│   └── <laravel project>
└── docker-for-development
    │
    ├── docker-compose.yml
    │
    ├── php7.2-fpm
    │	└── <Dockerfile>
    │   
    ├── mysql-5.6.37
    │	└── <Dockerfile>
    │
    ├── nginx
    │	└── <Dockerfile>
    │   
    ├── conf
    │	└── <config files>   
    │
    └── data
    	├── <nginx logs>   
    	└── <mysql data>

Docker Compose

docker-compose.yml Sample 

version: '3'
services:
    phpserver:
        container_name: phpserver
        build: php7.2-fpm
        ports:
            - 127.0.0.1:9000:9000
        volumes:
            - ./../laravelberlin.de:/var/www/laravelberlin.de

Docker Compose Expanded

docker-compose.yml  

version: '3'
services:
    lb-talk-phpserver:
        container_name: lb-talk-phpserver
        build: php7.2-fpm
        ports:
            - 127.0.0.1:9000:9000
        networks:
            - appnet
        links:
            - lb-talk-db:mysqldb
        volumes:
            - ./../laravelberlin.de:/var/www/laravelberlin.de
            - ./../another-site.com:/var/www/another-site.local
            - ./data/logs/nginx/:/var/log/nginx
            - ./:/var/www/docker
        depends_on:
            - lb-talk-db

 

Docker Compose Expanded

docker-compose.yml  nginx 

    lb-talk-nginx:
        container_name: lb-talk-nginx
        build: nginx
        ports:
            - 127.0.0.1:80:80
        networks:
            appnet:
        links:
            - lb-talk-phpserver
        depends_on:
            - lb-talk-db
        volumes:
            - ./../laravelberlin.de:/var/www/laravelberlin.de
            - ./../another-site.com:/var/www/another-site.local
            - ./data/logs/nginx/:/var/log/nginx

Docker Compose Expanded

docker-compose.yml Mysql Database

    lb-talk-db:
        container_name: lb-talk-db
        build: mysql-5.6.37
        ports:
            - 127.0.0.1:3352:3306
        networks:
            - appnet
        volumes:
            - ./data/lb-talk-db:/var/lib/mysql
        environment:
            MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
            MYSQL_DATABASE: lb-talk-db
            MYSQL_USER: laravel
            MYSQL_PASSWORD: secret 

Docker Compose Expanded

docker-compose.yml Mysql Database for a different site 

    another-site-db:
        container_name: another-site-db
        build: mysql-5.6.37
        ports:
            - 127.0.0.1:3353:3306
        networks:
            - appnet
        volumes:
            - ./data/another-site-db:/var/lib/mysql
        environment:
            MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
            MYSQL_DATABASE: another-site-db
            MYSQL_USER: laravel
            MYSQL_PASSWORD: secret

PHP Container

PHP7.2-fpm Dockerfile

# https://hub.docker.com/r/phpdockerio/php72-fpm/dockerfile/
FROM phpdockerio/php72-fpm

RUN apt-get upgrade

RUN apt-get update && apt-get install -y \
git unzip vim wget bash-completion iputils-ping build-essential git-flow ssh python-dev python-setuptools python-pip \
libconfig-inifiles-perl libyaml-dev \
# required dependencies
php7.2-bcmath php7.2-bz2 php7.2-cli php7.2-common php7.2-curl php7.2-cgi php7.2-dev php7.2-fpm php7.2-gd php7.2-gmp \
php7.2-imap php7.2-intl php7.2-json php7.2-ldap php7.2-mbstring php7.2-mysql php7.2-odbc php7.2-opcache php7.2-pgsql \
php7.2-phpdbg php7.2-pspell php7.2-readline php7.2-recode php7.2-soap php7.2-sqlite3 php7.2-tidy php7.2-xml \
php7.2-xmlrpc php7.2-xsl php7.2-zip php7.2-xdebug php7.2-exif

# Fix user uid and gid 1000 not being in use
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
RUN sed -i 's/short_open_tag = Off/short_open_tag = On/' /etc/php/7.2/fpm/php.ini

<some more setup>

COPY ./conf/.bashrc /root/.bashrc
COPY ./conf/.gitconfig /root/.gitconfig

COPY ./.ssh /root/.ssh
RUN chmod -R 600  /root/.ssh

WORKDIR /var/www/laravelberlin.de

MySQL Container

mysql-5.6.37 Dockerfile

# https://hub.docker.com/_/mysql
FROM mysql:5.6.37

RUN apt-get upgrade

RUN apt-get update && apt-get install -y git unzip vim wget git bash-completion iputils-ping build-essential tcl

# Set timezone
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Berlin /etc/localtime

COPY ./container_conf/.bashrc /root/.bashrc
RUN touch /root/.bash_history

WORKDIR /var/www/

nginx Container

nginx Dockerfile

FROM debian:jessie

RUN apt-get update && apt-get install -y \
    nginx

RUN usermod -u 1000 www-data

RUN echo "upstream php-upstream { server lb-talk-phpserver:9000; }" > /etc/nginx/conf.d/upstream.conf

ADD nginx.conf /etc/nginx/
ADD ./conf/*.conf /etc/nginx/sites-available/

#setup nginx stuff
RUN ln -s /etc/nginx/sites-available/laravelberlin.local.conf /etc/nginx/sites-enabled/laravelberlin.local.conf

<other sites could be listed here>

CMD ["nginx"]

EXPOSE 80
EXPOSE 443

Other things

Host file changes 

127.0.0.1 	laravelberlin.local
127.0.0.1 	my-other-site.local

Changes to .env file 

DB_CONNECTION=mysql
DB_HOST=lb-talk-db
DB_PORT=3306
DB_DATABASE=lb-talk-db
DB_USERNAME=laravel
DB_PASSWORD=secret

Enough talking right?

Run the docker-compose command 

docker-compose up --build
Creating network "docker-for-development_appnet" with driver "bridge"
Building lb-talk-db
Step 1/10 : FROM mysql:5.6.37
5.6.37: Pulling from library/mysql
85b1f47fba49: Pull complete
27dc53f13a11: Pull complete
095c8ae4182d: Pull complete
0972f6b9a7de: Pull complete
a11a67190c24: Pull complete
908252bea0ce: Pull complete
ffe3d1ca6af8: Pull complete
e4a48a0019b4: Pull complete
3b57faa79614: Pull complete
55c3e5a390ed: Pull complete
7a6f5c78394b: Pull complete
Digest: sha256:2528302501835852673c6594a825c14894b16c41b46cdf036b81426700fdb469
Status: Downloaded newer image for mysql:5.6.37
 ---> 8a2bb11da158
Step 2/10 : RUN apt-get upgrade
 ---> Running in 5724dde09ec9
Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Removing intermediate container 5724dde09ec9
 ---> 96c85f149591
Step 3/10 : RUN apt-get update && apt-get install -y git unzip vim wget git bash-completion iputils-ping  build-essential tcl
 ---> Running in 8304fcf2368a
Get:1 http://repo.mysql.com jessie InRelease [28.8 kB]
Get:2 http://security.debian.org jessie/updates InRelease [44.9 kB]
Ign http://deb.debian.org jessie InRelease
Get:3 http://deb.debian.org jessie-updates InRelease [145 kB]
Get:4 http://deb.debian.org jessie Release.gpg [2420 B]
Get:5 http://deb.debian.org jessie Release [148 kB]
Ign http://repo.mysql.com jessie InRelease

On first run it could take 5-15 mins

Demo

enter into the phpserver container

docker exec -it lb-talk-phpserver bash
/docker-for-development>docker exec -it lb-talk-phpserver bash

[root@24f6c4714587:/var/www/laravelberlin.de]#
[root@24f6c4714587:/var/www/laravelberlin.de]#

Now you have access to a linux console that is the same as your production box

Questions

I will answer them as best as can!

 

And remember to try the "not that spicy" hot sauce.

 

Thanks to Billie for sponsoring us tonight.

Docker for Local Development - Laravel Berlin March 2019

By getafixx

Docker for Local Development - Laravel Berlin March 2019

Docker for Local Development - Laravel Berlin March 2019

  • 513