Containerize your application with Docker
- One of the biggest trends in software development right now is the use of Docker and containerization. Since companies like Spotify, Uber, Yelp, and Lyft are using Docker, we have to assume they are on to something. So let’s take a look at why Docker is so hot right now and dig a little deeper into the core technology that makes Docker so popular - containers. In order to understand what containers are, it is important to understand what problem they solve in software development.
WHAT ARE CONTAINERS AND DOCKER?
- A key piece of developing software is the ability for engineers to be working on the same application on different machines. Those machines may be running on any number of combinations of operating systems (MacOS, Windows, Ubuntu), different versions of operating systems, packages and libraries, etc. This vast array of possible machine environments can cause the same application code to function differently across individual developer’s machines. One developer may have a working application on their machine, but when another developer tries to run the exact same code, the application does not work.
- So how can developers work on the same project without running into these issues? The original solution was to use virtual machines. A virtual machine (VM) is essentially an application that runs as a separate operating system on a host machine. This allows a developer using Mac and another developer using Windows to set up a VM that runs a Linux operating system on their respective computers to work on the same application.
- The issue with VMs is that they are resource intensive since a VM partitions a set of resources such as RAM and SSD from the host machine. VMs come with an entire operating system and a lot of other features that may not be necessary to run an application. Using a VM for the sole purpose of working on an application is the equivalent of hammering a nail with a sledgehammer.
- Then along came the container! A container is simply a unit of software that packages all the code of an application and the dependencies needed in order for it to run consistently across multiple computing environments. Since containers only need to run the packaged application, they become more lightweight and portable than VMs because they do not require an entire operating system or the same amount of resources as a VM.
- So what does Docker have to do with with containers? Docker is a platform that allows developers to create, deploy, and run their applications using containers. The Docker Engine was open sourced in 2013 and has remained a staple in the software industry ever since. Docker also provides a repository service, Docker Hub, which can be thought of as the GitHub for Docker images.
Benefits of using Docker
- Using containers with Docker allows developers to work in a consistent environment from development to production. This increases overall productivity as development teams can spend more time working on an application rather than figuring out why a feature within an application only works on one machine. Containers also promote the use of microservices by having each service run in its own container. Microservices allow large monolithic applications to be split into small services that each handle a single task independently.
- Another important benefit for many businesses is that containers allow companies to save money on resources. Instead of using Virtual Machines to run multiple servers, companies can containerize their application and easily ship their container to be run on another machine. Not only will they save money by using less resources to run the same application, but they will also save time since VMs usually have a slow boot up time.
Getting started with Docker
- To get started using Docker, follow the guide to download and install Docker for either Mac or Windows. Links to get started with Docker are below:
- To ensure Docker has been installed correctly on your machine simply run the following command in the terminal.
docker --version
- You should see the version of Docker you have installed on your machine.
Containerize a Simple Application in 3 Easy Steps
1. Create a Dockerfile
- Before we can containerize our application we first need to create an image using a Dockerfile. An image is like a snapshot of our application that contains all the files and dependencies that are needed to start running the application. The Dockerfile is where we write out the blueprint on how to build the image for our application. An example of a Dockerfile for building an image of a simple Node.js app is shown below.
FROM node:latest
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 3000
ENTRYPOINT ["node","server.js"]
2. Build the image
- Once we have created our Dockerfile, we use the build command to build out the image. There are some optional flags that can be used such as the -t flag which enables you to add a tag to the build. The -f flag can be used to select a certain Dockerfile for the build. If your application requires multiple containers, you will need a Dockerfile for each container you build. There could be a Dockerfile-database, Dockerfile-dependencies, etc. Without the -f flag, the default file used for the build will simply be Dockerfile. The command below will build an image using the Dockerfile in the current directory.
docker build .
3. Create the container from the image
- Now that our image is created, we can use that image to run a container using the docker run command. There are also some important flags to take note of when using the run command such as -d and -p. With -d you can utilize detached mode which will run the container in the background while giving you access to your terminal. Without using detached mode, you will have to keep your terminal open to keep your container running and you will need a new tab if you need to enter more commands in the terminal. The -p flag allows you to map a port on the host machine to an internal port on the container. This will allow you to see the application that is running on the container through a port that you specify on the host machine. The command below will run the container from the image created earlier in detached mode while mapping our machine’s port 3000 to the container’s port 3000.
docker run -d -p 3000:3000 <image_id/image_name>
Important Docker commands
- We can use this image to create our container or we can push the image to Dockerhub
docker push <image_name>
- We can also pull images down from Dockerhub
docker pull <image_name>
- To show all running containers we can use
docker ps
- And to stop a running a container we can use the stop command
docker stop <container_name>
Conclusion
- The incredible benefits of using containers have made Docker ubiquitous in software development. Not only are containers much more cost and resource effective compared to VMS, they also provide uniformity across production and development environments.
Tags:
Technical Blogs