January 01, 2020
When developing applications, a common problem occurs again and again - and its when a teams development environment isn’t similar. This can mean a number of things, as in different operating system, configuration, infrastructure requirements, dependencies, etc. and the list goes on.
In the past, making use of Virtual Machines(VMs) eventually caused some other unbearable issues that was difficult to solve, such as:
And here’s where Docker comes in. Docker is an platform for developing, shopping, and running applocations. It enables the use of separating applications from infrastructure so delivering software can be seamless and quick.
A few terminology and concepts would need to be quickly understood in order to deploy a simply application. An image is a lightweight, stand-aline executable packages that includes everything needed to run a piece of software, code, library, environment variables and config files. A container is a runtime instance of an image. It runs completely isolates form the host environment by default, so accessing host files and ports will need to be configured to do so.
Let’s create a very simple Flask application so we can see how Docker and containerization works.
First, let’s create a directory and change into it so we can begin creating our flask application.
1 2 3 4 # make a directory for our flask application mkdir simple-flask # change into the directory cd simple-flask
Next, lets create two files, app.py for our flask application, and requirements.txt. If you don’t know what the requirements.txt file is, it is to specify specific python packages required to run the project.
1 2 3 # create an app.py and requirements.txt touch app.py touch requirements.txt
Next, we will edit both the app.py and requirements.txt file
1 2 3 4 5 6 7 8 9 10 11 12 13 # app.py from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
1 2 # requirements.txt Flask==1.1.1
Next, we will need to create a Dockerfile to help use automate the build process of our application.
A Dockerfile is needed to define what goes on in the environment inside our container. The Dockerfile will contain commands a typical user calls in the command line. Using the docker build command will automate and execute the build process of the Dockerfile.
Let’s create a Dockerfile within our simple-flask directory
1 2 # create Dockerfile touch Dockerfile
Next, we will edit the Dockerfile to build our application
1 2 3 4 5 6 7 8 9 FROM python:3.7.6-slim WORKDIR /app ADD . /app RUN pip install -r requirements.txt CMD ["python", "app.py"]
Your directory should now look like this:
1 2 ls Dockerfile app.py requirements.txt
Next, we run the build command to create a Docker image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 # using the build command to create a Docker image named *simple-helloworld* docker build -t simple-helloworld . Sending build context to Docker daemon 4.096kB Step 1/5 : FROM python:3.7.6-slim 3.7.6-slim: Pulling from library/python 8ec398bc0356: Pull complete 3db8034857a2: Pull complete 51dd1475ea95: Pull complete 809c3e0dba2a: Pull complete c0d438eb747a: Pull complete Digest: sha256:d70f7ffc05337be7191b0291505b2e3194de1d506e45655ed2ba686e4d2ebe19 Status: Downloaded newer image for python:3.7.6-slim ---> 1521a961d9c2 Step 2/5 : WORKDIR /app ---> Running in a119eeccfd12 Removing intermediate container a119eeccfd12 ---> ab0ffab5de98 Step 3/5 : ADD . /app ---> 6e8185524c23 Step 4/5 : RUN pip install -r requirements.txt ---> Running in d57e5e4cb672 ... ... ... ... Removing intermediate container d57e5e4cb672 ---> 0dffd38020f8 Step 5/5 : CMD ["python", "app.py"] ---> Running in fbf6530be992 Removing intermediate container fbf6530be992 ---> 47628b493751 Successfully built 47628b493751 Successfully tagged simple-helloworld:latest
Next, we will use the run command to isolate the container and run its own file system, networking, and isoated processes separate from the host.
1 2 3 4 5 6 7 docker run -p 5000:80 simple-helloworld * Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
If we go to 0.0.0.0:5000, this is what should be displayed.
Congratulations, you have succesfully created, ran, pushed, pulled with docker.
This is a very basic introduction to deploying a flask application using docker. It is a very efficient way in deploying an application that has fast startup time, easy to maintain, less resource usage, easier to update, etc, etc.
Just remember, don’t solely rely on containers, VMs still have their place in the world. Having an understanding of both containers and VMs and knowing when to use which one is crucial.