Introduction to the Dockerfile Part II

source image :

Create Dockerfile

vim Dockerfile

Dockerfile content:

# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install — trusted-host -r requirements.txt
# Make port 80 available to the world outside this container
# Define environment variable
# Run when the container launches
CMD ["python", ""]

Create requirements.txt file


Create file

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():
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__":’', port=80)

Build image from Dockerfile

sudo docker build -t friendlyhello .

See image friendlyhello

sudo docker image ls

Run image friendlyhello

sudo docker run -d -p 4000:80 friendlyhello

See container :

sudo docker container ls

Test app using curl

curl http://localhost:4000

Test app



Cloud DevOps Enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store