Introduction to the Dockerfile Part II

Intoduction to the Dockerfile for beginner

source image :
vim Dockerfile
# 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", ""]
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)
sudo docker build -t friendlyhello .
sudo docker image ls
sudo docker run -d -p 4000:80 friendlyhello
sudo docker container ls
Test app



Cloud DevOps Enthusiast

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