Skip to content

Commit 65d6b82

Browse files
committed
add app
1 parent 5b419b9 commit 65d6b82

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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"]

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
11
# python-flask-docker
22
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+

app/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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')

app/templates/error.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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>

app/templates/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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>

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask==1.0.2

0 commit comments

Comments
 (0)