Getting started with Docker container in AWS Cloud9
Overview
Containerization is not a new term, the idea of LXC (Linux Container) was first introduced by Linux. It was only after Docker came out that container technology was put up for discussion. Container technology refers to the application, running environment, library…, and so on, these things that are necessary to run the application, all wrapped together. Just as android studio packages the development environment into apk. Containerization will save developers time and space to install each guest OS compared to virtual machine technology, which means that you can perform more capacity on specified hardware than virtual machines device, but also less effective can significantly drop the criticized.
Docker is an open-source project that provides the deployment and automation of containerized applications, enabling developers to develop projects, and fast and easy to pack and deploy services. Docker has three basic concepts: Docker images, Docker containers, and Docker repository (registries), we will explain below: * Docker image file: Docker image file is the application environment in which the Docker container is started. Similar to the image file of a virtual machine, the virtual machine boots the operating system through the image file, while Docker stores the state of the file system in the image file. Where Docker image files are read-only templates. * Docker container: Docker container is an instance of application execution. Developers start the container through an image file to run the service on the container. > It is recommended not to execute too many programs in the same container. * Docker repository (registries): Docker repository is where image files are stored. Developers can upload their own image files to the repository for farewell people download for use. Images can also be easily moved on different devices through Docker warehouses.
Scenario
In this lab, you will learn how to display a simple “Hello World” web page on Docker container using AWS Cloud9.
Step by step
First, we need to create a Cloud9 environment. Follow the steps below.
-
On the service menu, select Cloud9, select Create environment.
-
On the Name Environment Page, type a Name for your environment. Optionally add a Description to your environment. Select Next Step.
-
On the Configure settings page :
Environment type : Choose Create a new instance for environment (EC2).
Instance type : Choose t2.micro (1 GiB RAM + 1 vCPU).
- (Optionally) In the middle of the Configure settings page is the Cost-saving setting, you can choose to stop your EC2 instance after how much time without inactivity, in case you forgot to stop your EC2 instance.
-
And leave the rest as default, select Next Step.
-
At Review page, select Create environment. It might take 30~60 seconds to create your environment.
Create a Docker Image
In this section, we create a Docker image of a simple web application and test it on an EC2 instance.
- Let’s introduce a little about Cloud9, the picture below is the Cloud9 console :
- Upper terminal or text file editor.
- Lower terminal or text file editor. > Both can be used to type commands or edit a text file.
- The files explorer of this environment.
- Tools panel, collaborators, functions plugin, debug system.
- After the console showed up, select Create File to create a file to your environment.
-
Paste the code below to the file. The script will be executed while building the Docker image and Apache web server will be set up. After your web page file and container built successfully, It will print out “Hello World!” on the website.
FROM PHP:7.2-apache
RUN echo “Hello World!” > /var/www/html/index.html
-
Press
CTRL+S
to save the file. -
Rename it to Dockerfile.
-
A Dockerfile is a manifest that describes the base image to use for the Docker image and what we want to be installed and running on it.
-
In the Cloud9 environment, we can use the terminal in the lower panel.
-
Verify that whether Docker is installed in the Cloud9 environment with the following command. In general, Cloud9 has installed Docker by default and therefore we don’t need to install ourselves.
$ docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a215d7133c34aa18e3b72b4a21fd0c6136
Built: Fri Oct 26 23:38:19 2018
OS/Arch: linux/amd64
Experimental: falseServer:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a/18.06.1-ce
Built: Fri Oct 26 23:39:46 2018
OS/Arch: linux/amd64
Experimental: false -
Build the Docker image from Dockerfile.
Note:
<your-image-name>
is the docker image name and.
means the Dockerfile can be found in any directory.
$ docker build -t <your-image-name> .
Sending build context to Docker daemon 18.43kB
Step 1/2 : FROM php:7.2-apache
7.2-apache: Pulling from library/php
5e6ec7f28fb7: Pull complete
cf165947b5b7: Pull complete
7bd37682846d: Pull complete
99daf8e838e1: Pull complete
ae320713efba: Pull complete
ebcb99c48d8c: Pull complete
9867e71b4ab6: Pull complete
936eb418164a: Pull complete
5d9617dfb66b: Pull complete
8dd7afaae109: Pull complete
8f207844da7e: Pull complete
adb3ae5e4987: Pull complete
44d7d07029db: Pull complete
fb91064652b0: Pull complete
Digest: sha256:b840f37df10893e6dd4f722bf40032ae072e6f63352e2e6c14370a79057fa2c9
Status: Downloaded newer image for php:7.2-apache
---> e2834342b873
Step 2/2 : RUN echo "Hello World!" > /var/www/html/index.html
---> Running in 6d3f2ea3aa85
Removing intermediate container 6d3f2ea3aa85
---> bf7eda5138c7
Successfully built bf7eda5138c7
Successfully tagged <your-image-name>:latest
-
List docker images to verify whether the image was created correctly. We should be able to see there is an image called <your-image-name>.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
latest bf7eda5138c7 About a minute ago 378MB
php 7.2-apache e2834342b873 2 days ago 378MB
lambci/lambda nodejs4.3 4f521df1e2d3 7 weeks ago 940MB
lambci/lambda python3.6 4a6785b2780d 7 weeks ago 1.07GB
lambci/lambda python2.7 9e29188f3f15 7 weeks ago 946MB
lambci/lambda nodejs6.10 fec3cfdc31b4 7 weeks ago 991MB
Run Docker container
-
Run the newly built image. The
–p 8080:80
option maps the exposed port80
on the container to port8080
on the host system.8080
is the default port of Cloud9.$ docker run –name
-d -p 8080:80
2e3cdb60da3b1476ba0f7009504b0a5ed06abeabe5ca4beae27e048f437bbfbf
The output is the container’s unique ID, it’s normal to be different.
-
List docker containers to verify whether the container was created correctly. We should be able to see there is a container’s CONTAINER NAME called <your-container-name>.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e3cdb60da3b“docker-php-entrypoi…” About a minute ago Up About a minute 0.0.0.0:8080->80/tcp
Preview the result
- Select Preview on the top of the page, then select Preview Running Application. You don’t need to open another web page to see the result page.
- You should be able to see a webpage like a picture below : > You can select the website bar:
/
, and you’ll see the endpoint which is mapping to Cloud9’s 8080 port. It will be like this: “https://.vfs.cloud9.us-east-1.amazonaws.com/”
-
And then, you can type :
$ docker top
UID PID PPID C STIME TTY TIME CMD
root 5128 5109 0 02:23 ? 00:00:00 apache2 -DFOREGROUND
33 5203 5128 0 02:23 ? 00:00:00 apache2 -DFOREGROUND
33 5204 5128 0 02:23 ? 00:00:00 apache2 -DFOREGROUND
33 5205 5128 0 02:23 ? 00:00:00 apache2 -DFOREGROUND
33 5206 5128 0 02:23 ? 00:00:00 apache2 -DFOREGROUND
33 5207 5128 0 02:23 ? 00:00:00 apache2 -DFOREGROUND
to display the running processes of your Docker container.
Conclusion
Now you’ve learned how to display a simple webpage on Docker container.
This is just a simple starting lab for beginners who wants to learn Docker container.
There are much more things that Docker can do, you can even pull images from Docker hub to try it yourself.
Reference
Tag:AWS, AWS Cloud9, Container, Containerization, Docker, Ec2