Docker Fundamentals: Introduction to an Increasingly Essential Developer Tool
Docker is growing quickly in popularity in development teams across industries and use cases. It has come to be an essential technology for many developers. This is especially true for software that gets deployed online or across various host environments.
In this Docker fundamentals guide, we’ll walk through the basics of Docker. This is by no means a comprehensive post. However, hopefully, by the end you’ll have an understanding of why Docker is useful and whether it’s worth learning for your role as a developer.
What Is Docker?
Docker is a piece of software for performing virtualization.
Essentially, Docker mimics the environment that an operating system provides. However, it uses containers to make sure these resources stand alone and are walled off from the settings, dependencies, and environment of the host machine.
For instance, a Docker container might virtualize a Linux installation. You could run that container on a Mac, PC, or Linux machine and the container itself would remain identical.
Why Do We Need Docker?
Talk to any software developer who has been around for a while, and you’ll be sure to hear stories of having to support the same software across multiple environments. Indeed, cross-platform support has been a major challenge for developers ever since the early days of
A common trope heard in software development teams used to be, “Well, it works on my machine! I don’t know what’s wrong with yours!”
The lack of consistency across environments made software support a nightmare. While smart developers created dependency management systems and virtual environments, it was still difficult to guarantee that everyone had a consistent operating system and environment
Docker solves that problem with virtualization. Now, instead of running the application locally, you run the container locally and the container runs the application. Docker takes care of all the work involved in creating a consistent virtualized, containerized environment, no matter what operating system your computer uses.
Is It a Virtual Machine?
One misconception is that Docker is basically a virtual machine. It’s not. Docker is different.
Virtual machines are an older concept that has been around for a while. In effect, running a virtual machine involves installing an entire operating system that runs dual-booted, in parallel, via a hypervisor, or instead of your native operating system.
In contrast, Docker containers are not full installations of operating systems. They just require access to your native OS’s kernel, memory, and other functionality. The Docker container engine works in tandem with your native OS without needing a full install of the container’s OS.
How Do I Use Docker?
You can get started by following the download instructions for your operating system on the Docker website.
Docker is an open source technology, so anybody can use it and play around with it at no cost.
Once you’ve installed Docker, you can check your installation by running $ docker run hello-world.
Docker will first look to see if you have a container image (the blueprint for how to spin up a Docker container) called “hello-world” locally. If it doesn’t find it on your local machine (and it shouldn’t because you just freshly installed Docker), then it will go to Docker Hub online to pull a container image from the web.
Docker Hub has all kinds of useful pre-designed container images for projects. They’re worth checking out if you’re new.
But for now, let’s see what goes inside a container image.
What Do I Do to Make Docker Work?
First, you’ll need an application to run. Most commonly, Docker is used with web applications that will run on a server or via a cloud provider like AWS, GCP, or Azure.
I’ll assume you have a web application already created and that it lives in a file called App.py, App.js,
App.java, or something like that.
There are three key steps to running your application inside a Docker container
1. Create a Dockerfile
Now, we need to tell Docker about that application and the dependencies it requires. So, we create a Dockerfile in the same directory as our application.
The Dockerfile is the blueprint for how Docker should construct the container and run your application.
If we were running a Python application, the file would look something like this:
# Inherit from the Python Docker image
# Install the web framework “Flask” via the package manager, pip
RUN pip install flask==1.0.2
# Copy the source code to app folder
COPY ./app.py /app/
# Change the working directory
# Set "python" as the entry point
# Set the command as the script name
Docker will build the application using these instructions. In other languages, the process is similar. Inherit from the language’s existing Docker image hosted on Docker Hub, install any dependencies, copy the source code into the Docker app, then set an entry point and a command to start the application.
2. Build the Container
Now that we have our Docker file ready, we need to give Docker the time to pull together all the resources and dependencies we specified.
This is the part where Docker creates the containerized environment to run our application. Since it involves downloading and setting up dependencies, it can sometimes take a while depending on the complexity of your application.
To build a new container, type: docker build -t my_app:0.1
Docker will look for a Docker file in the current directory, then it will start to build the container based on those instructions. The -t flag tells Docker what we want to name the new container with the syntax <name>:<tag> – the tag is usually a version number.
3. Run the Container
Now that Docker has the environment and dependencies ready, we can run our application inside the new container: docker run -d my_app:0.1
Docker will try to start up your application using the command you specified in the Dockerfile.
The -d flag tells Docker to run the application in “detached” mode, so that it doesn’t take over your terminal session.
You can always see what applications Docker is running by typing: docker ps hat
What Did That Do?
Well, it didn’t change your application in any way. Docker doesn’t place limits on the types of applications you can build. If you can build it on your local machine, you can build it as a Docker container.
What changed is the environment in which your application is running. If you get your application working correctly on a Docker container locally, you can be sure it will work on any computer in the world that has Docker installed.
That peace of mind is priceless when you consider the variety of operating systems and deployment options there are for your application.
Can I Make It More Complex?
It’s nice that Docker can reliably run a single file, App.py or App.js or whatever.
But can it reliably run a complex web application that has an app, database, cache, worker tasks, and more?
The answer is yes! Docker includes a set of tools called docker-compose that allow you to work with multiple containers at once, running different services alongside one another and allowing them to talk.
Implementing docker-compose is beyond the scope of this article, but it’s an important next step in learning to use Docker effectively.
This post has only scratched the surface of what Docker is and how it can help development teams. It’s becoming a valuable tool for ensuring that environments are consistent across developer teams, and dev, staging, and production branches of your application. Using containers is quickly becoming standard practice in the industry for developing and deploying web applications, so now is a great time to dive in and learn more about Docker.
Founded in 1991, Intertech delivers software development consulting and IT training to Fortune 500, Government and Leading Technology institutions. Learn more about us. Whether you are a developer interested in working for a company that invests in its employees or a company looking to partner with a team of technology leaders who provide solutions, mentor staff and add true business value, we’d like to meet you.