Skip to content

Instantly share code, notes, and snippets.

@sunnykrGupta
Last active August 14, 2017 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnykrGupta/1186735bfcc8916647323220f3c834b4 to your computer and use it in GitHub Desktop.
Save sunnykrGupta/1186735bfcc8916647323220f3c834b4 to your computer and use it in GitHub Desktop.
I have three file in my directory named 'flask' :
➜ flask
── app.py
── Dockerfile
── requirement.txt
#----------------------------------------------
$ cat app.py
from flask import Flask
#from flask import render_template, request
import json
app = Flask(__name__)
@app.route("/")
def hello():
return "Welcome to Python Flask!\n"
if __name__ == "__main__":
app.run(host='0.0.0.0', port='5009')
#----------------------------------------------
$ cat requirement.txt
Flask==0.10.1
#----------------------------------------------
$ cat Dockerfile
FROM frolvlad/alpine-python2
RUN mkdir /etc/flask
ADD app.py /etc/flask/app.py
ADD requirement.txt /etc/flask/requirement.txt
CMD ["python", "/etc/flask/app.py"]
#----------------------------------------------
> Lets build our docker image with name **flask-v1**
➜ docker build -t flask-v1 .
Sending build context to Docker daemon 4.096 kB
Step 1/5 : FROM frolvlad/alpine-python2
latest: Pulling from frolvlad/alpine-python2
627beaf3eaaf: Pull complete
79d39e719c2e: Pull complete
Digest: sha256:47e3f85dadf401d51c6f74a18d4f693c2157692292e6dae0a078f37499a183ee
Status: Downloaded newer image for frolvlad/alpine-python2:latest
---> 603e17608203
Step 2/5 : RUN mkdir /etc/flask
---> Running in 0d97b7a8986b
---> 9b2a858914e2
Removing intermediate container 0d97b7a8986b
Step 3/5 : ADD app.py /etc/flask/app.py
---> 1a4938be7722
Removing intermediate container f4d11b837e26
Step 4/5 : ADD requirement.txt /etc/flask/requirement.txt
---> 5d058851dd81
Removing intermediate container 08eef72a4051
Step 5/5 : CMD python /etc/flask/app.py
---> Running in 96490917e533
---> a081e6cbcf3c
Removing intermediate container 96490917e533
Successfully built a081e6cbcf3c
#----------------------------------------------
> Now, we have made some modification in app.py
$ cat app.py
from flask import Flask
#from flask import render_template, request
import json
app = Flask(__name__)
@app.route("/")
def hello():
return "Welcome to Python Flask!\n"
#Added utility
def utility():
return "something"
if __name__ == "__main__":
app.run(host='0.0.0.0', port='5009')
#----------------------------------------------
> Lets build our docker image with name **flask-v2**
➜ docker build -t flask-v2 .
Sending build context to Docker daemon 4.096 kB
Step 1/5 : FROM frolvlad/alpine-python2
---> 603e17608203
Step 2/5 : RUN mkdir /etc/flask
---> Using cache
---> 9b2a858914e2
Step 3/5 : ADD app.py /etc/flask/app.py
---> a7565514aab3
Removing intermediate container 360eb266a458
Step 4/5 : ADD requirement.txt /etc/flask/requirement.txt
---> 8441bca383f0
Removing intermediate container 15ebbb15d67d
Step 5/5 : CMD python /etc/flask/app.py
---> Running in 73e4bbcbe512
---> 0f4a5754f0d0
Removing intermediate container 73e4bbcbe512
Successfully built 0f4a5754f0d0
> You will see only step-2 was taken from cache, rest of the instructions \
ran again because change has been detected by Docker on \
step-3 command ie, ADD app.py , so all subsequent commands \
ran again to build layer on top of previous layer.
#----------------------------------------------
> Now we have made changes in Dockerfile, some reorder of commands.
$ cat Dockerfile
FROM frolvlad/alpine-python2
RUN mkdir /etc/flask
# add requirement file first, then commands which contains some changes.
ADD requirement.txt /etc/flask/requirement.txt
ADD app.py /etc/flask/app.py
CMD ["python", "/etc/flask/app.py"]
#----------------------------------------------
> Lets build our docker image with name flask-v3 and lets see build console.
➜ flask docker build -t flask-v3 .
Sending build context to Docker daemon 4.096 kB
Step 1/5 : FROM frolvlad/alpine-python2
---> 603e17608203
Step 2/5 : RUN mkdir /etc/flask
---> Using cache
---> 9b2a858914e2
Step 3/5 : ADD requirement.txt /etc/flask/requirement.txt
---> Using cache
---> db8fc95cebff
Step 4/5 : ADD app.py /etc/flask/app.py
---> 9fb8351616e1
Removing intermediate container 4e98534d5339
Step 5/5 : CMD python /etc/flask/app.py
---> Running in 139f8c4282d8
---> e3fae9852d94
Removing intermediate container 139f8c4282d8
Successfully built e3fae9852d94
> Now, you can see only step-2,3 was taken from cache, step-4 command ie, ADD app.py \
build new layer because of change detected in app.py file, and this build \
saved little bit of our time. This is really important in building big Docker images \
where we have bigger chain of command to build an app.
@rusrushal13
Copy link

rusrushal13 commented Aug 14, 2017

I guess, there is one typo in line 164; It should be docker build -t flask-v3 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment