Web Application Deployment With Docker

Reading time ~4 minutes

Web Application Deployment With Docker

Web Application Deployment With Docker

1 Installing Docker

  • Install: From https://docker.github.io/engine/getstarted/linux_install_help/

    curl -fsSL https://get.docker.com/ | sh
    
  • Verify installation:

    docker version
    docker run hello-world
    
  • Get some images from the registry:

    docker pull ubuntu:14.04
    docker pull ubuntu:16.04
    docker run ubuntu:14.04 /bin/echo 'Hello world'
    docker run ubuntu:16.04 /bin/echo 'Hello world'
    

2 Create a web application container

  • Create a new container from ubuntu-upstart:

    docker run -d --name=webapp --hostname=example.org \
               -p 8000:80 ubuntu-upstart:14.04
    
  • Install apache2 and mysql inside the container:

    alias docker-webapp-exec='docker exec -it webapp env TERM=xterm'
    docker-webapp-exec bash
    apt-get update
    apt-get -y upgrade
    apt-get -y install apache2 mysql-server php5 php5-mysql
    a2enmod ssl
    a2ensite default-ssl
    service apache2 restart
    exit
    
  • Add this line on /etc/hosts.

    127.0.0.1 example.org
    
  • Try it in browser: http://example.org:8000/

3 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 /opt/test/
    cd /opt/test/
    mkdir -p app01
    docker run -d --name=webapp01 --hostname=example.org \
               -v $(pwd)/app01:/var/www/html \
               -p 8001:80 lamp:v1.0
    mkdir -p app02
    docker run -d --name=webapp02 --hostname=example.org \
               -v $(pwd)/app02:/var/www/html \
               -p 8002:80 lamp:v1.0
    
  • Modify applications:

    vim app01/index.php
    vim app02/index.php
    
  • Test them in browser:

4 Building images

  • Create /opt/test/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
    RUN a2enmod ssl && \
        a2ensite default-ssl
    
  • Build the image:

    cd /opt/test/
    docker build --tag=lamp:v1.1 lamp/
    docker images
    
  • Rebuild and notice that the cache will be used.

5 Upload image to Docker Hub

6 Using container wsproxy

  • Get the code from GitHub:

    cd /opt/test/
    git clone https://github.com/docker-build/wsproxy
    
  • Create a workdir:

    mkdir wsproxy1
    cd wsproxy1
    ln -s ../wsproxy .
    
  • Build the image and create a container:

    cp wsproxy/utils/config.sh .
    vim config.sh
    
    wsproxy/docker/build.sh
    wsproxy/docker/create.sh
    wsproxy/docker/start.sh
    
  • Create containers of webapps:

    docker stop webapp01 webapp02
    docker rm webapp01 webapp02
    
    docker run -d --name=webapp01 --hostname=app01.example.org \
               -v $(pwd)/../app01:/var/www/html lamp:v1.1
    docker run -d --name=webapp02 --hostname=app02.example.org \
               -v $(pwd)/../app02:/var/www/html lamp:v1.1
    

    Note that no HTTP ports are exposed to the host (for example using options -p 80:80 -p 443:443).

  • Add domains app01.example.org and app02.example.org:

    wsproxy/domains-add.sh webapp01 app01.example.org
    wsproxy/domains-add.sh webapp02 app02.example.org
    
    cat containers.txt
    cat sites-enabled/app01.example.org.conf
    cat sites-enabled/app02.example.org.conf
    
  • Add these lines on /etc/hosts:

    127.0.0.1 app01.example.org
    127.0.0.1 app02.example.org
    
  • Try in browser:
  • Try to get a free SSL cert from letsencrypt.org:

    wsproxy/get-ssl-cert.sh info@app01.example.org app01.example.org --test
    

    It will not work because app01.example.org is not a real domain owned by you and info@app01.example.org is not a real address.

7 Install SchoolTool

  • Get scripts from GitHub:

    cd /opt/test/
    git clone https://github.com/docker-build/SchoolTool
    cd SchoolTool/
    
  • Edit settings.sh and comment out the ports:

    #PORTS="-p 7080:7080 -p 80:80 -p 443:443"
    
  • Build image, create the container, and start it:

    ./build.sh
    ./create.sh
    ./start.sh
    docker ps
    
  • Make some configurations and install apache2:

    ./exec.sh ./config.sh
    ./exec.sh ./install-apache2.sh
    
  • Add the domain to wsproxy:

    cd /opt/test/wsproxy1/
    wsproxy/domains-add.sh schooltool school1.example.org
    
  • Add to /etc/hosts the line 127.0.0.1 school1.example.org and try http://school1.example.org/ in browser.
  • Try to get a free SSL cert from letsencrypt.org:

    wsproxy/get-ssl-cert.sh info@school1.example.org school1.example.org --test
    

    It will not work because school1.example.org is not a real domain owned by you and info@school1.example.org is not a real address.

8 Install Moodle

  • Get scripts from GitHub:

    cd /opt/test/
    git clone https://github.com/docker-build/moodle
    
  • Create a working directory for the container:

    mkdir moodle1
    cd moodle1/
    ln -s ../moodle .
    cp moodle/utils/settings.sh .
    
  • Edit settings.sh and comment out the ports:

    IMAGE=moodle
    CONTAINER=moodle1
    DOMAIN="moodle1.example.org"
    
    MYSQL_ROOT_PASSWD=random
    DBNAME=moodle1
    DBUSER=moodle1
    DBPASS=moodle1
    
    #PORT_HTTP=80
    #PORT_HTTPS=443
    #PORT_SSH=2222
    
  • Build image, create the container, and start it:

    moodle/docker/build.sh
    moodle/docker/create.sh
    moodle/docker/start.sh
    docker ps
    
  • Configure the new container:

    moodle/config.sh
    
  • Add the domain to wsproxy:

    cd /opt/test/wsproxy1/
    wsproxy/domains-add.sh moodle1 moodle1.example.org
    
  • Add to /etc/hosts the line 127.0.0.1 moodle1.example.org and try http://moodle1.example.org/ in browser.
  • Try to get a free SSL cert from letsencrypt.org:

    wsproxy/get-ssl-cert.sh info@moodle1.example.org moodle1.example.org --test
    

    It will not work because moodle1.example.org is not a real domain owned by you and info@moodle1.example.org is not a real address.

9 Install B-Translator Client

See: http://info.btranslator.org/install.html

  • Get the image:

    docker search btranslator
    docker pull btranslator/btr_client:v3.0
    docker images
    
  • Create and start a container:

    docker create --name=bcl_fr --hostname=fr.example.org btranslator/btr_client:v3.0
    docker start bcl_fr
    
  • Add the domain to wsproxy:

    cd /opt/test/wsproxy1/
    wsproxy/domains-add.sh bcl_fr fr.example.org
    
  • Add to /etc/hosts the line 127.0.0.1 fr.example.org and open in browser https://fr.example.org/ .

10 Installing a Drupal Application

  • See: https://github.com/dashohoxha/dbox
  • Get the code of DBox from github:

    cd /opt/test/
    git clone --branch ubuntu-14.04 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 -A
    git commit -a -m 'My new project.'
    cd ..
    
  • Build a docker image and create a container:

    mkdir webapp03-workdir
    cd webapp03-workdir/
    ln -s ../webapp03/docker .
    cp docker/settings.sh .
    vim settings.sh
    docker/build.sh settings.sh
    vim config     # comment out ports
    docker/create.sh
    docker/start.sh
    docker ps
    
  • Add the domain to wsproxy:

    cd /opt/test/wsproxy1/
    wsproxy/domains-add.sh webapp03-master example.org
    
  • Add to /etc/hosts the line 127.0.0.1 example.org and open in browser https://example.org/ .

Date: 2016-10-19

Author: Dashamir Hoxha

Created: 2016-10-28 Fri 05:33

How to Build a Twitter-Bot

How to Build a Twitter-BotHow to Build a Twitter-BotTable of Contents1. Accessing twitter from the terminal2. Getting random proverbs wit...… Continue reading

Linux Guest Account

Published on January 14, 2017

LTSP Server with Ubuntu 16.04

Published on January 07, 2017