Docker for developement
What is docker and why should I care?
Docker is a containerization technology. So, what is a container? A container is a lightweight, modular virtual machine. Now, virtual machines are usually complex and resource hungry. Docker solves this problem by directly basing itself on Linux kernel. Let me explain.
Let us discuss a virtual machine’s architecture. It starts a hypervisor(Bridge between virtual machines and the operating system) on the operating system and upon that runs the new operating system. Upon that, it runs all the libraries and upon that the application. Now, let us discuss how docker works. Docker just runs a docker engine on top of which the libraries and the application runs. Since it does not host a complete virtual machine, it does not make a Hardware level isolation instead makes an OS level process isolation. So Docker uses fewer resources, take less space, boot way faster and can be created and destroyed easily and almost instantaneously. This image explains these architectures beautifully. On left is a normal virtual machine, and on the right is the docker infrastructure.
Docker containers can be really small. For example, the most famous docker, alpine Linux, is just about 5 Mb. It comes with a complete package manager(called apk) too!
How does this help a developer?
Developers always had a problem with configurations and conflicts. Sometimes, Programs work locally but fail on the server. This may happen for many reasons, which are usually hard and irritating. Even for local development, multiple projects may require multiple versions of packages and sometimes even multiple versions of programming languages. Every language has its own solution. Node js developers use nvm(node version manager). Python developers use pipenv. Everyone has their own approach, but it does not have to be this complicated.
Suppose you develop using a docker image. Let us see if there are any advantages. Docker is completely isolated from the system, so no more configuration or conflict problems. Docker images can be copied and shared. So develop in an environment and just share the docker details and another teammate can reproduce docker image with same settings and can use the same development environment you had. You can even deploy this docker image on Amazon web services. It even has the added benefit of sand boxing!
Sounds Great! How do I get started?
Before starting on this journey, There are some things you need to know. Dockerhub is a repository of docker images. You will need to install docker on your system. We will run a simple flask application in our alpine Linux docker. Let us get started!
Get alpine Linux
To get alpine Linux docker, we just have to use the pull command from docker client to retrieve the image from dockerhub.
docker pull alpine
To list all the containers our machine has, we have to query
docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 3fd9065eaf02 8 weeks ago 4.15MB
The Flask application
Since we have an alpine, let us make a simple flask application. This application will just say ‘Hello from docker!’ when the home page is queried.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello from docker!' if __name__ == '__main__': app.run(debug=True,host='0.0.0.0')
To summarize, we just initialized flask, made and application and told it to return Hello from docker when queried to a home page. And made the app run on 0.0.0.0.
Make sure pipenv is installed and install flask using pipenv.
pipenv install flask
Run the application to make sure everything works
pipenv run python main.py
And open 0.0.0.0. If you see “Hello from Docker!”, All is working well! Let us jump into docker!
Now, let’s create a Dockerfile, A Dockerfile is a sequence of commands a docker should run. We will customize this according to our need.
Create a Dockerfile, and edit it as follows
FROM alpine:latest RUN apk update RUN apk add python3 RUN pip3 install pipenv COPY . /app WORKDIR /app RUN pipenv install EXPOSE 5000 CMD ["pipenv","run","python","main.py"]
Let’ break it down, In Line 1: we pull in the latest alpine docker, after that, we run apk update and add python3. apk is alpine package manager. Then we install pipenv in the docker, because pipenv is awesome! Now is the important steps.
In line 5, we ask docker to copy the current working directory to /app inside the docker.And set the working directory as /app. This is the same as cd /app. Now we run pipenv install to install all dependencies. Later we ask docker to expose port 5000 and run the command
pipenv run python main.py
Build and Run
Now since we have the Dockerfile ready and flask up and running, let us first build our container.
docker build -t flask_simple .
The dot at the end denotes that docker should use the Dockerfile in the current directory to build a docker named with all the requirements, and name it flask_simple.After executing, you should see a stream of logs, showing the output from docker.
Later to execute, run
docker run -p 5000:5000 flask_simple
This command will expose port 5000 in docker to 5000 to our machine and run docker named flask_simple. Now open 0.0.0.0:5000 and you will see “Hello from docker!”.
That was easy! Now we have a simple reproducible and tiny container, which can be deployed anywhere. Docker is much more complicated and way more powerful. Keep learning and have fun!