Lab 5 : Deploying Applications with Docker

POD parameters : OpenStack Group-1 user0 aio110 10.1.64.110 compute120 10.1.64.120 [email protected]
User aio110 computeY Network & Allocation Pool
user0
vnc  : lab.onecloudinc.com:5900
aio110
eth0            : 10.1.64.110
eth1            : 10.1.65.110
eth2            : ext-net
Netmask  : 255.255.255.0
Gateway  : 10.1.64.1
compute120
eth0            : 10.1.64.120
eth1            : 10.1.65.120
eth2            : ext-net
Netmask  : 255.255.255.0
Float Range  : 10.1.65.0010.1.65.00
Network         : 10.1.65.0/24
Gateway         : 10.1.65.1
DNS                   : 10.1.1.92

Introduction

Docker helps user to create a single deployable unit for user application. This unit, also called a container, contains everything required for the application. This includes the code (or binary), runtime, system tools and system libraries. Packing all the requirements into a single unit ensures an identical environment for the application, wherever it is deployed. This can also help in maintaining identical development and production setups that are difficult to track.

Once up, the creation and deployment of the container can be automated. This eliminates a whole class of issues. Most of these issues arise due to files being out of sync or due to differences in the development and production environments. Docker helps resolve these issues.

In this lab, we will learn how to deploy a web application with Docker, and how Docker can help improve development workflow and deployment process.

As with the previous labs, you will need to SSH the aio node.
If you have logged out, SSH into your AIO node:

ssh centos@aio110

If asked, the user password (as with the sudo password) would be centos, then become root via the sudo password:

sudo su –

1. Prerequisites

Why to use Docker with a Web Application?

  • Web applications typically have template and configuration files.
  • Docker ensures identical setups in development and production. There are times when an application works in development, but not in production. Using Docker frees user from having to worry about problems like these.
  • Machines, operating systems and installed software can vary significantly across a large team. Docker provides a mechanism to ensure a consistent development setup. This makes teams more productive and reduces friction and avoidable issues during development.

2. Static Web App

Let’s start by taking few-steps. We’re going to look at is how we can run a simple static website. We’re going to pull a Docker image from Docker Hub, run the container and see how easy it is to run a webserver.

  1. Let’s begin. The image that we are going to use is a single-page website we can pull from docker registry onecloudtest/static-site. We can download and run the image directly in one go using docker run.
  2. docker run onecloudtest/static-site

    Since the image doesn’t exist locally, the client will first fetch the image from the registry and then run the image. If all goes well, should see a Nginx is running… message in terminal. Now the serve is running.

    Note: Press ctrl+c to interrupt.
  3. Well in this case, the client is not exposing any ports so we need to re-run the docker run command to publish ports. While we’re at it, we should also find a way so that our terminal is not attached to the running container. This way, we can close terminal and keep the container running. This is called detached mode.
  4. docker run -d -P --name static-site onecloudtest/static-site

    Output:

    e61d12292d69556eabe2a44c16cbd54486b2527e2ce4f95438e504afb7b02810

    In the above command, -d will detach terminal, -P will publish all exposed ports to random ports and finally –name corresponds to a name we want to give. Now we can see the ports by running the docker port [CONTAINER] command.

    docker port static-site

    Output:

    80/tcp -> 0.0.0.0:32769
    443/tcp -> 0.0.0.0:32768
    Note: TCP guarantees delivery of data and also guarantees that packets will be delivered on port 32769 in the same order in which they were sent.
  5. Now need to do port forwarding
  6. Note: Go to putty terminal-> SSH-> Tunnels-> Add below ports.
    1. source port:8080
      destination: aio110:80
    2. source port:32769
      destination: aio110:32769
  7. Open http://localhost:32769in browser.
http://localhost:32769

Lab5-1.png

3. Deploying WordPress with Docker

The WordPress application we’re going to deploy is composed of two services:

  • wordpress: The container that runs Apache, PHP, and WordPress.
  • db: A MariaDB database used for data persistence.

WordPress is one of the most popular content management software (CMS) due to its multitude of features and ease of use. However, setting up a new web host environment can be time consuming especially if we need to do it often. Simplifying the installation process to a few fast commands greatly reduce the time and effort required, this is where Docker comes in.

  1. Run the usual update command for system to make sure have the latest source lists.
  2. yum update -y
  3. Check that have the curl command line utility.
  4. curl -V

    Output:

    curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.19.1 Basic ECC zlib/1.2.7 libidn/1.28 libssh2/1.4.3
    Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
    Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz
    
    Note: curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.
  5. If in case the curl command not found install it
  6. yum install curl -y

3.1 MariaDB in a container

Before installing WordPress with Docker need to have somewhere to store the data. MariaDB is a community-developed relational database management system and a drop-in replacement for MySQL.

  1. Start of by making a new directory where to store the files for WordPress and MariaDB for example in home directory.
  2. mkdir ~/wordpress && cd ~/wordpress
  3. Then run the command below with the password onecloud.
  4. docker run -e MYSQL_ROOT_PASSWORD=onecloud -e MYSQL_DATABASE=mariadb --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mariadb:latest
    Note: MariaDB Environment variables, these are marked in the Docker command with -e:
    -e MYSQL_ROOT_PASSWORD – Set password here as onecloud.
    -e MYSQL_DATABASE – Creates and names a new database e.g. mariadb:
    Docker Parameters:
    –name wordpressdb – Names the container.
    -v “$PWD/database” :/var/lib/mysql – Creates a data directory linked to the container storage to ensure data persistence.
    -d – Tells Docker to run the container in daemon.
    mariadb: latest – Finally defines what to install and which version.
  5. Output of mariadb installation.
  6. ...
    Status: Downloaded newer image for mariadb:latest
    23df0ec2e48beb1fb8704ba612e9eb083f4193ecceb11102bc91232955cccc54
  7. If Docker was successful at creating the container, then should see a code at the end of the output similar to the example above. We can confirm that the MariaDB container is running by using the following command: Use the ps command to display the running containers.
  8. docker ps
  9. WordPress is also made officially available on Docker Hub, pull the image using with the command below.
  10. docker pull wordpress
  11. Run the below command with the password onecloud and server IP aio110.
  12. docker run -e WORDPRESS_DB_PASSWORD=onecloud --name wordpress --link wordpressdb:mysql -p 10.1.64.110:80:80 -v "$PWD/html":/var/www/html -d wordpress
  13. Then open server’s domain name or IP address in a web browser to test the installation. We should be redirected to the initial WordPress setup page at http:///wp-admin/install.php. Go through the setup wizard and we are done.
  14. Open http://localhost:8080/ in browser.
  15. http://localhost:8080/

    wordpress.png

  16. Exit from the directory wordpress.
  17. cd

    Lab Cleanup

  18. To remove all the containers run the below commands.
  19. docker rm `docker ps -a -q` -f
  20. To remove all the images run the below commands.
  21. docker rmi `docker images -q` -f