In this tutorial, we are going to be taking a look at what Docker is, why it’s needed and how you can get up and running using Docker in your day-to-day development.
What Is Docker?
Docker is a containerization technology that allows you to package up your application into something that will run everywhere that Docker can run.
You typically define absolutely everything you need for your application within your docker file and no more. If your application is a Java application, then you would typically use a Java-based Docker image that would have the exact version of the JVM that you need.
By specifying only what your application needs, you end up with lightweight containers that can be scheduled and ran side-by-side.
Why Should You Use Docker?
Imagine you were working in a development team that spanned across the world. In this team you were working on a number of different microservices that all pieced together to form one larger system.
If all of these microservices were written in different languages and required various environment variables to be set or exact versions of programming languages such as Python, Go, Node to be used. Setting up your machine to support the development of these various microservices could be difficult and you might find yourself spending hours on getting your configuration just right.
By using Docker, we can remove this issue almost entirely. Each microservice would come with its own
Dockerfile within the root of the repository. This
Dockerfile would contain everything each of your microservices would need in order to run.
In order to run and develop one of these microservices, you would simply have to pull the project down and run the appropriate
docker run command with appropriate flags and your microservice would be started on your local machine. You would no longer have to spend hours trying to get everything just right and you could dive right into the codebase and start making changes.
Docker can be installed by navigating to this page: Docker Download and clicking the download button.
Once you have Docker installed on your machine, you should be able to run
docker commands from the terminal. Running just
docker by itself should return a list of everything you can do with the
A Simple Python Docker Application
Let’s have a shot at writing our own Docker based application that uses Python and serves a very simple REST API over port 3000.
Now, let’s have a look at how we would go about implementing our
app.py python application:
Now, in order to run this, we would first have to build our image using the
docker build command passing in the name of the image we wish to build using the
-t flag and specifying where our
Dockerfile exists with the
. at the end of the command like so:
We then want to run our newly created
py36-account-service image by utilizing the
docker run command specifying the name of our container as
py36-account-service, that we wish to run this in a detached mode with
-d and the port we wish to expose with
-p. We want to access this application through port
9000 locally but within the container our application will be running on port
80, so we specify
9000:80 to map these two ports together and forward any traffic hitting
http://localhost:9000 into our container and subsequently to our application.
Finally we specify the image we wish to build our container from by appending
py36-account-service to the end of our command:
The main benefits of Docker-izing the above Python program would be that if it is reliant on the particular version of Python we specified, another developer wouldn’t have to worry about getting this installed on their machine. They would simply have to have
docker installed on their machine and then they would run that above
docker run command and their service would start up and be exposed on port 9000.
In this tutorial, we looked at what Docker is, and how you can use it to improve the way that you develop software. We created a really simple NodeJS project that used Docker and we launched this Docker application using the
docker run command.