Follow along with the instructor as you explore Docker and configure a basic Flask application.
docker ps
see list of running containersdocker ps -a
see lisf of all containers, including ones that failed or were stoppeddocker start <container-name or id>
starts the containerdocker stop <container-name or id>
stops the containerdocker restart <container-name or id>
restarts the containerdocker rm <container-name or id>
removes the containerdocker images
shows list of images that you have downloaded. containers are created from imagesdocker image rm <image-name or id>
removes an imagedocker build --tag super-happy-fun-os .
builds a new image that containers can be created fromdocker-compose up -d
uses a docker-compose.yml file to configure and start containers$ docker --version
pip
is installed (package manager for python)$ pip --version
If pip
is not found, install it.
$ sudo easy_install pip
cd
into the folder of the repo you just clonded$ pip install --user -r requirements.txt
In this walkthrough we are going to run a simple Python web app. The app uses Python Flask as a web server. Flask may be new to you, but see if you see any similarities to Spring Boot.
In Visual Studio Code, or another general purpose editor, open the file simple_app.py
Review file simple_app.py
import time
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World!'
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
Run the simple web app by running the below command
$ python simple_app.py
Navigate to http://localhost:5000
, it should show our simple message.
Now stop this process in your terminal, using ctrl + c
as we will next get this running via a Docker container.
We need to setup a Dockerfile
that uses CentOS to run our webapp.
Paste in the below text into file /docker-flask-walkthrough/Dockerfile
(please review the content instead of only pasting it)
# start with the centos 7 base image
FROM centos:7
# ADD <source> <destination>, Adds the current directory to /code in the container
ADD . /code
# install and upgrade software we need on the container
RUN yum -y install epel-release
RUN yum -y update
RUN yum -y install python-pip
RUN pip install --upgrade pip
RUN pip install -r /code/requirements.txt
# Run the web app as the main process (there can only be one CMD per Dockerfile)
CMD ["python", "/code/simple_app.py"]
We need to build a Docker image that will run our simple web app.
/docker-flask-walkthrough
(this may take a few minutes)
$ docker build --tag my-centos-simple .
The Dockerfile
in the previous section along with the command docker build
created a new docker image that can be used to create containers with.
View the list of docker images
(your new image my-centos-simple should appear in the list)
$ docker images
Run these commands to create a container that using the new image. Remember an images don’t run, the are the OS and foundation used by running conatiners.
$ docker create -i -t -p 5000:5000 my-centos-simple
$ docker start <container_name/id>
(to see list of conatiners)
$ docker ps -a
Check the browser to see if the “Hello World” message shows up. http://localhost:5000
Now stop that docker container by running:
(the last number is the id for the docker container)
$ docker stop 8b54229210c9
In the next section of the walkthrough, we are going to stand up a more complex Flask app. In this app, we are going to integrate the key-value database Redis. In order to integrate Redis into the Flask web app, we will need to leverage Docker’s network capabilities.
Review counter_app.py
::
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/counter')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
We don’t want our users to have to install redis on their own. We need to create a container that runs redis. Then we can link the redis
and counter-app
containers using docker-compose
. Sounds fun right?
Find and Download the Redis Image
redis
.redis
result.redis:alpine
tag.redis:alpine
image to your computer by running$ docker pull redis:alpine
Change the last line in the Dockerfile
to be:
CMD ["python", "/code/counter_app.py"]
Build the centos-counter-app
image with this command:
$ docker build --tag centos-counter-app .
Note
The above command takes a while to run. After it completes you will see the below message:
Successfully built 8447bcee9c62
Successfully tagged centos-counter-app:latest
$ docker images
We are going to bring this all together by creating a docker-compose.yml
file, that will allow the Flask app to reference the Redis container.
Paste this text into docker-compose.yaml
version: '3'
services:
web:
image: "centos-counter-app"
ports:
- "5000:5000"
redis:
image: "redis:alpine"
Use the following command2 to stand up and verify the two containers
$ docker-compose up -d
Creating docker-flask-walkthrough_redis_1 ... done
Creating docker-flask-walkthrough_web_1 ... done
$ docker ps
http://localhost:5000/counter
Let’s look at these containers a bit more indepth. docker logs {container name}
will show all of the logs that have been written to STDOUT. (replace {container name} with the actual container name).:
$ docker logs {container name/id}
Let’s also take the container details. docker inspect {container name/id}
will show all of the details about the container including network information.:
$ docker inspect {container name/id}