File tree Expand file tree Collapse file tree 6 files changed +82
-0
lines changed Expand file tree Collapse file tree 6 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ FROM python:3.6
2
+ LABEL maintainer="lorenz.vanthillo@gmail.com"
3
+ COPY . /app
4
+ WORKDIR /app
5
+ RUN pip install -r requirements.txt
6
+ ENTRYPOINT ["python" ]
7
+ EXPOSE 5000
8
+ CMD ["app/app.py" ]
Original file line number Diff line number Diff line change 1
1
# python-flask-docker
2
2
Basic Python Flask app in Docker which prints the hostname and IP of the container
3
+
4
+ ### Build application
5
+ Build the Docker image manually by cloning the Git repo.
6
+ ```
7
+ $ git clone https://github.com/lvthillo/python-flask-docker.git
8
+ $ docker build -t lvthillo/python-flask-docker .
9
+ ```
10
+
11
+ ### Download precreated image
12
+ You can also just download the existing image from [ DockerHub] ( https://hub.docker.com/r/lvthillo/python-flask-docker/ ) .
13
+ ```
14
+ docker pull lvthillo/python-flask-docker
15
+ ```
16
+
17
+ ### Run the container
18
+ Create a container from the image.
19
+ ```
20
+ $ docker run --name my-container -d -p 5000:5000 lvthillo/python-flask-docker
21
+ ```
22
+
23
+ Now visit http://localhost:5000
24
+ Example of expected output:
25
+ ```
26
+ The hostname of the container is 6095273a4e9b and its IP is 172.17.0.2.
27
+ ```
28
+
29
+ ### Verify the running container
30
+ Verify by checking the container ip and hostname (ID):
31
+ ```
32
+ $ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-container
33
+ 172.17.0.2
34
+ $ docker inspect -f '{{ .Config.Hostname }}' my-container
35
+ 6095273a4e9b
36
+ ```
37
+
38
+
Original file line number Diff line number Diff line change
1
+ from flask import Flask ,render_template
2
+ import socket
3
+
4
+ app = Flask (__name__ )
5
+
6
+ @app .route ("/" )
7
+ def index ():
8
+ try :
9
+ host_name = socket .gethostname ()
10
+ host_ip = socket .gethostbyname (host_name )
11
+ return render_template ('index.html' , hostname = host_name , ip = host_ip )
12
+ except :
13
+ return render_template ('error.html' )
14
+
15
+
16
+ if __name__ == "__main__" :
17
+ app .run (debug = True ,host = '0.0.0.0' )
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > Error page</ title >
6
+ </ head >
7
+ < body >
8
+ Can not print the IP address of the container
9
+ </ body >
10
+ </ html >
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > Index page</ title >
6
+ </ head >
7
+ < body >
8
+ The hostname of the container is < b > {{ hostname }}</ b > and its IP is < b > {{ ip }}</ b > .
9
+ </ body >
10
+ </ html >
Original file line number Diff line number Diff line change
1
+ flask == 1.0.2
You can’t perform that action at this time.
0 commit comments