Ship a simple web Application on Docker

Spread the love


In this post of devOpsJourney , you will see how to Ship a simple web Application on Docker

We’ll go step by step to demonstrate this .

Step 1 : First of all you need to setup docker environment either on your local machine (Window/Linux/Mac) or on a VM on cloud computing provider .

To have a overview of Supported platform for docker installation, follow below link

https://docs.docker.com/engine/installation/#supported-platforms

For  Centos : https://docs.docker.com/engine/installation/linux/centos/

For Windows : https://docs.docker.com/docker-for-windows/install/

In this tutorial I am going to use local windows machine environment .

 

Step 2 : I ‘ll follow this link to install docker for windows

https://docs.docker.com/docker-for-windows/install/

 

Step 3 : Open the Windows Powershell and validate your installation by typing this command.

docker run hello-world

You should get output as shown below

Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world

b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

Step 4 : Now we will write a Dockerfile , build image from that file and will run container using that image.

i) Create a directory on your local machine e.g. dockerapp and change the current directory to dockerapp.

mkdir dockerapp

cd dockerapp

 

ii) Create 3 files (Dockerfile, requirements.txt and myapp.py) inside it with contents as mentioned

File name :  Dockerfile

 

FROM python:2.7-slim

WORKDIR /myapp

ADD . /myapp

RUN pip install -r requirements.txt

EXPOSE 80

ENV NAME World

CMD [“python”, “myapp.py”]

File name : requirements.txt

Flask

File name :  myapp.py

from flask import Flask
import os
import socket

app = Flask(__name__)

@app.route(“/”)
def hello():

html = “<h3>Hello {name}!</h3>” \
“<b>Hostname:</b> {hostname}<br/>”

return html.format(name=os.getenv(“NAME”, “DevOpsJounrey”), hostname=socket.gethostname())

if __name__ == “__main__”:
app.run(host=’0.0.0.0′, port=80)

iii) Run below command to create an image out of docker file and app

docker build -t <some-suitable-name> .

iv) To validate if image has been create successfully

 docker images

v) Now run the container from this image.

docker run -d -p 8000:80 <some-suitable-name-given-above-for-image>

Step 5 : Open your web browser and hit http://localhost:8000 .

So in this way we can create and Ship a simple web Application on Docker. In the next post we’ll talk about how you can upload this to Cloud and reuse on another system/environments.

 

Stay tuned and share this with your friends.

Leave a Reply

Your email address will not be published. Required fields are marked *