Microservices

Build a microservice which provides a simple math function by using following steps:

  1. Here is the simple microservice using Python Flask (http://flask.pocoo.org/):
$ cat entry.py
from flask import Flask, request
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

@app.route("/power/<int:base>/<int:index>")
def power(base, index):
return "%d" % (base ** index)
@app.route("/addition/<int:x>/<int:y>") def add(x, y):
return "%d" % (x+y)

@app.route("/substraction/<int:x>/<int:y>") def substract(x, y):
return "%d" % (x-y)
if __name__ == "__main__":
app.run(host='0.0.0.0')
  1. Prepare a Dockerfile as follows in order to build the Docker image:
$ cat Dockerfile
FROM ubuntu:14.04

# Update packages RUN apt-get update -y

# Install Python Setuptools
RUN apt-get install -y python-setuptools git telnet curl

# Install pip
RUN easy_install pip

# Bundle app source
ADD . /src
WORKDIR /src


# Add and install Python modules
RUN pip install Flask

# Expose
EXPOSE 5000

# Run
CMD ["python", "entry.py"]
  1. Then, use the docker build command to build the Docker image as follows:
//name as “your_docker_hub_id/my-calc”
$ sudo docker build -t hidetosaito/my-calc .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM ubuntu:14.04
---> 6cc0fc2a5ee3
Step 2 : RUN apt-get update -y
---> Using cache


(snip)


Step 8 : EXPOSE 5000
---> Running in 7c52f4bfe373
---> 28f79bb7481f
Removing intermediate container 7c52f4bfe373
Step 9 : CMD python entry.py
---> Running in 86b39c727572
---> 20ae465bf036
Removing intermediate container 86b39c727572
Successfully built 20ae465bf036


//verity your image
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hidetosaito/my-calc latest 20ae465bf036 19 seconds ago 284 MB
ubuntu 14.04 6cc0fc2a5ee3 3 weeks ago 187.9 MB
  1. Then, use the docker login command to log in to Docker hub:
//type your username, password and e-mail address in Docker hub
$ sudo docker login
Username: hidetosaito
Password:
Email: [email protected]
WARNING: login credentials saved in /home/ec2-user/.docker/config.json
Login Succeeded
  1. Finally, use the docker push command to register to your Docker hub repository as follows:
//push to your docker index
$ sudo docker push hidetosaito/my-calc
The push refers to a repository [docker.io/hidetosaito/my-calc] (len: 1)
20ae465bf036: Pushed

(snip)

92ec6d044cb3: Pushed
latest: digest: sha256:203b81c5a238e228c154e0b53a58e60e6eb3d1563293483ce58f48351031a474 size: 19151
  1. Upon access to Docker hub, you can see your microservice in the repository:
Your microservice Docker image on Docker hub
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.199.112