Tuesday, March 13, 2018

Define your container with Dockerfile

If you haven't installed Docker, then install it on your machine using the link (Installing Docker on Ubuntu).

Define a container with Dockerfile

Now its time to create your own custom environment using DockerFile. DockerFile behaves exactly the same wherever it runs enabling you to establish same standard configuration on dev, test and production environment.

Step 1: Create a folder (in my case i have created my-docker-img folder)
Step 2: Create a Dockerfile inside the folder (I have created Dockerfile.txt)
Dockerfile.txt
 # Use an official Python runtime as a parent image  
 FROM python:2.7-slim  
 # Set the working directory to /app  
 WORKDIR /app  
 # Copy the current directory contents into the container at /app  
 ADD . /app  
 # Set proxy server, replace host:port with values for your servers (If your are behind proxy server)  
 #ENV http_proxy host:port  
 #ENV https_proxy host:port  
 # Install any needed packages specified in requirements.txt  
 RUN pip install --trusted-host pypi.python.org -r requirements.txt  
 # Make port 80 available to the world outside this container  
 EXPOSE 80  
 # Define environment variable  
 ENV NAME World  
 # Run app.py when the container launches  
 CMD ["python", "app.py"]  

Step 3: Create app.py and requirement.txt specified in Docker file in the same folder.
app.py file is your python application that you want to deploy

app.py

 from flask import Flask  
 from redis import Redis, RedisError  
 import os  
 import socket  
 # Connect to Redis  
 redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)  
 app = Flask(__name__)  
 @app.route("/")  
 def hello():  
   try:  
     visits = redis.incr("counter")  
   except RedisError:  
     visits = "<i>cannot connect to Redis, counter disabled</i>"  
   html = "<h3>Hello {name}!</h3>" \  
       "<b>Hostname:</b> {hostname}<br/>" \  
       "<b>Visits:</b> {visits}"  
   return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)  
 if __name__ == "__main__":  
   app.run(host='0.0.0.0', port=80)  

requirements.txt
 Flask  
 Redis  

Note:
When the Dockerfile is built into an image, Dockerfile’s ADD command will add app.py into the image, and the app is served over HTTP with help the EXPOSE command which expose port 80. PIP command install python dependency libraries specified on requirements.txt file

Build the app

Now execute the build command from top of the directory. This creates a Docker image with tag using -t so it has a friendly name.
In my case directory is /Documents/my-docker-img and image name is my-app
 docker build -t my-app .  
To view the list images created on your machine
 docker image ls  

Run the app

Run the app, by mapping your machine’s port 8000 to the container’s published port 80 using -p
 docker run -p 4000:80 friendlyhello  
Then you can access your app deployed within the container using following URL
 http://localhost:8000/  
If you want to run the container in the background in detach mode
 docker run -d -p 4000:80 my-app  
To view all the running containers on your machine
 docker container ls  
If you want to stop the running container using CONTAINER ID
  sudo docker container stop c1081125c6e9   

Next Share your docker image

Useful links
https://docs.docker.com/get-started/part2/#log-in-with-your-docker-id

No comments:

Post a Comment

SQL INTERVIEW QUESTIONS AND ANSWERS