Web Application Deployment With Docker

Reading time ~4 minutes

Docker is a platform for developers and sysadmins to develop, ship, and run applications. Docker lets you quickly assemble applications from components and eliminates the friction that can come when shipping code. Docker lets you get your code tested and deployed into production as fast as possible. In this workshop we will try hands-on the basic commands for building and managing docker containers and images for a web application.

(Available also in presentation format: http://info.btranslator.org/docker-workshop/ .)

1 Installation

  • Install Docker: https://docs.docker.com/installation/#installation
    docker version
    
  • Pull the Docker image ubuntu:14.04:
    docker pull ubuntu:14.04
    
  • Pull the Docker image ubuntu-upstart:14.04:
    docker search ubuntu-upstart
    docker pull ubuntu-upstart:14.04
    
  • Pull the Docker image btranslator/btr_client:v2.2:
    docker search btranslator
    docker pull btranslator/btr_client:v2.2
    
  • Install docker-enter:
    docker run -v /usr/local/bin:/target jpetazzo/nsenter
    

2 About Docker

3 Basic Concepts

  • What is a docker image.
  • What is the Docker Hub.
  • What is a docker container.
  • Running a command inside docker.

5 A real world example

Let's see how Docker is used by installing a B-Translator Client.

  • Create and start a docker container:
    docker images
    docker run -d --name=bcl --hostname=example.org \
               -p 80:80 -p 443:443 btranslator/btr_client:v2.2
    
  • Add this line on /etc/hosts.
    127.0.0.1 example.org
    
  • Open in browser: https://example.org
  • Stop and start the container:
    docker ps -a
    docker stop bcl
    docker start bcl
    docker restart bcl
    

6 Run a command in a docker container

  • Hello world:
    docker run ubuntu:14.04 /bin/echo 'Hello world'
    
  • An interactive container:
    docker run -t -i ubuntu:14.04 /bin/bash
    pwd
    ls
    exit
    
  • A daemonized hello world:
    docker run -d ubuntu:14.04 \
        /bin/sh -c "while true; do echo hello world; sleep 1; done"
    docker ps
    docker logs insane_babbage
    docker stop insane_babbage
    docker ps -a
    

7 Other docker commands

docker
docker inspect
docker inspect --help
docker inspect ubuntu-upstat:14.04
docker inspect bcl
docker inspect -f '' bcl
docker top bcl

8 Create a web application container

  • Create a new container from ubuntu-upstart:
    docker run -d --name=webapp --hostname=example.org \
               -p 8000:80 -p 44300:443 ubuntu-upstart:14.04
    
  • Update system packages of the container:
    docker-enter webapp
    apt-get update
    apt-get -y upgrade
    
  • Install apache2 and mysql:
    apt-get install apache2 mysql-server php5 php5-mysql
    
  • Create a test app:
    cd /var/www/html/
    apt-get install vim
    vim index.php
    exit
    
  • Try it in browser: http://example.org:8000

9 Create more web application containers

  • Save the webapp container as a new image (make a snapshot):
    docker stop webapp
    docker commit webapp lamp:v1.0
    docker images
    
  • Create new containers from this image:
    mkdir app01
    docker run -d --name=webapp01 --hostname=example.org \
               -v $(pwd)/app01:/var/www/html \
               -p 8001:80 -p 44301:443 lamp:v1.0
    mkdir app02
    docker run -d --name=webapp02 --hostname=example.org \
               -v $(pwd)/app02:/var/www/html \
               -p 8002:80 -p 44302:443 lamp:v1.0
    
  • Modify applications:
    vim app01/index.php
    vim app02/index.php
    
  • Test them in browser:

10 Upload image to Docker Hub

11 Building images

  • Create lamp/Dockerfile with a content like this:
    FROM ubuntu-upstart:14.04
    
    RUN apt-get update; apt-get -y upgrade
    RUN apt-get -y purge openssh-server openssh-client ; apt-get -y autoremove
    RUN apt-get update ; DEBIAN_FRONTEND=noninteractive apt-get -y install \
        vim apache2 mysql-server php5 php5-mysql
    
  • Build the image:
    docker build --tag=lamp:v1.1 lamp/
    docker images
    
  • Rebuild and notice that the cache will be used.

12 Using container wsproxy

  • See: https://registry.hub.docker.com/u/dashohoxha/wsproxy/
  • Get the code from GitHub:
    git clone https://github.com/dashohoxha/wsproxy
    
  • Build the image and create a container:
    wsproxy/build.sh
    wsproxy/run.sh
    
  • Create containers of webapps:
    docker stop webapp01 webapp02
    docker rm webapp01 webapp02
    
    docker run -d --name=webapp01 --hostname=example1.org \
               -v $(pwd)/app01:/var/www/html lamp:v1.0
    docker run -d --name=webapp02 --hostname=example2.org \
               -v $(pwd)/app02:/var/www/html lamp:v1.0
    
  • Add apache2 virtual domains for example1.org and example2.org
    cd wsproxy/config/etc/apache2/sites-available/
    
    cp bcl.conf xmp1.conf
    sed -i xmp1.conf -e 's/example.org/example1.org/'
    
    cp bcl-ssl.conf xmp1-ssl.conf
    sed -i xmp1-ssl.conf -e 's/example.org/example1.org/'
    
    cp bcl.conf xmp2.conf
    sed -i xmp2.conf -e 's/example.org/example2.org/'
    
    cp bcl-ssl.conf xmp2-ssl.conf
    sed -i xmp2-ssl.conf -e 's/example.org/example2.org/'
    
    cd ../sites-enabled/
    ln -s ../sites-available/xmp1.conf .
    ln -s ../sites-available/xmp1-ssl.conf .
    ln -s ../sites-available/xmp2.conf .
    ln -s ../sites-available/xmp2-ssl.conf .
    
    cd ../../../../../
    
  • Edit wsproxy/hosts.txt and add these lines:
    webapp01: example1.org
    webapp02: example2.org
    
  • Restart container wsproxy:
    wsproxy/restart.sh
    
  • Add these lines on /etc/hosts:
    127.0.0.1 example1.org
    127.0.0.1 example2.org
    
  • Try in browser:

13 Installing a Drupal Application

  • See: https://github.com/dashohoxha/dbox
  • Get the code of DBox from github:
    git clone --branch openatrium https://github.com/dashohoxha/dbox.git
    
  • Rename the project:
    dbox/rename-project.sh  # see usage
    dbox/rename-project.sh labdoo:webapp03 lbd:w03
    mv dbox webapp03
    
  • Initialize a git repository:
    cd webapp03/
    git init .
    git add $(git ls-files --others)
    git commit -a -m 'My new project.'
    cd ..
    
  • Build a docker image:
    cp webapp03/install/settings.sh cfg.sh
    vim cfg.sh
    webapp03/docker-build.sh cfg.sh
    
  • Create a docker container:
    docker run -d --name=webapp03 --hostname=example3.org webapp03:master
    
  • Add apache2 virtual domain for example3.org:
    cd wsproxy/config/etc/apache2/sites-available/
    
    cp bcl.conf xmp3.conf
    sed -i xmp3.conf -e 's/example.org/example3.org/'
    
    cp bcl-ssl.conf xmp3-ssl.conf
    sed -i xmp3-ssl.conf -e 's/example.org/example3.org/'
    
    cp bcl_dev.conf xmp3_dev.conf
    sed -i xmp3_dev.conf -e 's/example.org/example3.org/'
    
    cp bcl_dev-ssl.conf xmp3_dev-ssl.conf
    sed -i xmp3_dev-ssl.conf -e 's/example.org/example3.org/'
    
    cd ../sites-enabled/
    ln -s ../sites-available/xmp3.conf .
    ln -s ../sites-available/xmp3-ssl.conf .
    ln -s ../sites-available/xmp3_dev.conf .
    ln -s ../sites-available/xmp3_dev-ssl.conf .
    
    cd ../../../../../
    
  • Edit wsproxy/hosts.txt and add these lines:
    webapp03: example3.org
    webapp03: dev.example3.org
    
  • Restart container wsproxy:
    wsproxy/restart.sh
    
  • Add these lines on /etc/hosts:
    127.0.0.1 example3.org
    127.0.0.1 dev.example3.org
    
  • Try in browser:

Building SSH Tunnels

Building SSH TunnelsBuilding SSH TunnelsTable of Contents1. Bypass a firewall blocking2. Connect to a database behind a firewall3. Access...… Continue reading

How to Build a Twitter-Bot

Published on March 24, 2017

Linux Guest Account

Published on January 14, 2017