1 Containerize Applications¶
Objective:
- Review process of containerizing of applications
- Review creation of Docker Images
- Review build image process
- Review process to launch containers with docker cli
1 Prepare Lab Environment¶
This lab can be executed in you GCP Cloud Environment using Google Cloud Shell.
Open the Google Cloud Shell by clicking on the icon on the top right of the screen:
Once opened, you can use it to run the instructions for this lab.
2 Build and Deploy gowebapp application¶
2.1 Overview of the Sample Application¶
This package contains two application components which will be used throughout the course to demonstrate features of Kubernetes:
- gowebapp
This directory contains a simple Go-based note-taking web application. It includes a home page, registration page, login page, about page, and note management page.
Configuration for this application is stored in code/config/config.json. This file is used to configure the backing data store for the application, which in this course is MySQL.
Later in the course, we will externalize this configuration in order to take advantage of Kubernetes Secrets and ConfigMaps. This will include some minor modifications to the Go source code. Go programming experience is not required to complete the exercises.
For more details about the internal design and implementation of the Go web application, see code/README.md.
- gowebapp-mysql
This directory contains the schema file used to setup the backing MySQL database for the Go web application.
2.3 Build Dockers image for frontend application¶
Step 1 Locate and review the go source code:
cd ~/$student_name-notepad/Mod5_assignment/
Result
Two folders with go app and mysql config has been reviewed.
Step 2 Write Dockerfile
for your frontend application
cd ~/$student_name-notepad/Mod5_assignment/gowebapp
Modify a file named Dockerfile
in this directory for the frontend Go app.
Use Cloud Editor or editor of you choice
edit Dockerfile
The template below provides a starting point for defining the contents of this file. Replace TODO comments with the appropriate commands:
#TODO --- Define this image to inherit from the "golang" base image. Use version `1.15.11` or lower for `golang`
#https://hub.docker.com/_/golang/
#https://docs.docker.com/engine/reference/builder/#from
#TODO Set a label corresponding to the MAINTAINER field you could use, so that it wil be visible from docker inspect with the other labels.
#MAINTAINER should be you student e-mail.
#TODO --- Define a version label for this image
#https://docs.docker.com/engine/reference/builder/#label
EXPOSE 80
ENV GOPATH=/go
#TODO --- Copy source code in the local /code directory into $GOPATH/src/gowebapp
#https://docs.docker.com/engine/reference/builder/#copy
WORKDIR $GOPATH/src/gowebapp/
RUN go get && go install
#TODO --- Define an entrypoint for this image which executes the compiled application in $GOPATH/bin/gowebapp when the container starts
#https://docs.docker.com/engine/reference/builder/#entrypoint
Step 4 Build gowebapp Docker image locally
Build the image locally. Make sure to include "." at the end. Make sure the build runs to completion without errors. You should get a success message.
#TODO Build image `<your-github-user>/gowebapp:v1
2.4 Build Docker image for backend application¶
Step 1 Locate folder with mysql config
cd ~/$student_name-notepad/Mod5_assignment/gowebapp-mysql
Step 2 Write Dockerfile for your backend application
Create a file named Dockerfile
in this directory for the backend MySQL database
application. Use Cloud Editor or editor of you choice.
edit Dockerfile
The template below provides a starting point for defining the contents of this file. Replace TODO comments with the appropriate commands:
#TODO --- Define this image to inherit from the "mysql" version 8.0 base image
#https://hub.docker.com/_/mysql/
#https://docs.docker.com/engine/reference/builder/#from
#TODO Set a label corresponding to the MAINTAINER field you could use, so that it wil be visible from docker inspect with the other labels.
#MAINTAINER should be you student e-mail.
LABEL gowebapp-mysql "v1"
#TODO --- Investigate the "Initializing a Fresh Instance" instructions for the mysql parent image, and copy the local gowebapp.sql file to the proper container directory to be automatically executed when the container starts up
#https://hub.docker.com/_/mysql/
#https://docs.docker.com/engine/reference/builder/#copy
Step 2 Build gowebapp-mysql Docker image locally
#TODO Build image <your-github-user>/gowebapp-mysql:v1
Build the image locally. Make sure to include "." at the end. Make sure the build runs to completion without errors. You should get a success message. Run and test Docker images locally
2.5 Test application by running with Docker Engine.¶
Before putting our app in production let's run the Docker images locally, to ensure that the frontend and backend containers run and integrate properly.
Step 1 Create Docker user-defined network
To facilitate cross-container communication, let's first define a user-defined network in which to run the frontend and backend containers:
docker network create gowebapp \
-d bridge \
--subnet 172.19.0.0/16
Note
Default bridge only allows connecting container by IP addresses which is not viable solution as IP address of docker change at startup.
There for we creating a user defined bridge network gowebapp
Step 2 Launch backend
container
Next, let's launch a frontend and backend container using the Docker CLI. First, we launch the database container, as it will take a bit longer to startup, and the frontend container depends on it. Notice how we are injecting the database password into the MySQL configuration as an environment variable:
#TODO Launch `backend` container in background
#TODO Container needs to run on network: `gowebapp`
#TODO Use this settings: `--name gowebapp-mysql` `--hostname gowebapp-mysql`
#TODO Include following Env Variable in the command: `MYSQL_ROOT_PASSWORD=rootpasswd`
Step 3 Launch frontend
container
Now launch a frontend container, mapping the container port 80 - where the web application is exposed - to port 8080 on the host machine:
#TODO Launch `frontend` container in background
#TODO Container needs to run on network: `gowebapp`
#TODO Use this settings: `--name gowebapp` `--hostname gowebapp`
#TODO Map the container port 80 - to port 8080 on the host machine
Step 4 Test the application locally
Now that we've launched the application containers, let's try to test the web application locally.
You should be able to access the application at Google Cloud Web Preview
Console:
Note
Web Preview using port 8080
by default. If you application using other port, you can edit this as needed.
Once you can see the application loaded. Create an account and login. Write something on your Notepad and save it. This will verify that the application is working and properly integrates with the backend database container.
Task
Take a screenshot of running application.
Step 5 Inspect the MySQL database
Let's connect to the backend MySQL database container and run some queries to ensure that application persistence is working properly:
#TODO docker xxx
Step 6 Once inside the container, connect to MySQL database:
mysql -u root -p
password:
Note
Use password that has beed used in MYSQL_ROOT_PASSWORD
env variable.
Step 7 Once connected, run some simple SQL commands to inspect the database tables and persistence:
#Simple SQL to navigate
SHOW DATABASES;
USE gowebapp;
SHOW TABLES;
SELECT * FROM <table_name>;
exit;
2.6 Cleanup running applications¶
### TODO docker xxx