Docker is easily one of the most popular and sought after technologies to learn these days. Docker has only been growing in popularity since the initial release in 2013. Many companies and developers have adopted Docker as their defacto tool to deploy and test development and production level applications. In this concise, quick start guide, we’ll get hands-on with Docker to help you get started quickly to immediately use it for your projects.
In part one, we go over the essential Docker introduction and run our first “hello Docker” container. In Part two, we create a more real-world example by building a simple HTML web application and deploying a container with Nginx to Digital Ocean and AWS.
Aside from being comfortable using the terminal, you will need the following software installed on your system:
So What is Docker?
Docker is an OS-level virtualization technology that can help you build and run containers. It makes it easy to package your code into containers with all the necessary components to run anywhere with Docker Engine installed. Unlike virtual machines, containers do not require you to install Guest-OS. Containers run on a single operating system using much fewer resources than virtual machines, making containers a much lighter weight solution.
Here’s an illustration of a side by side comparison of how containers run on a system vs. virtual machines.
Why is Docker Useful?
As mentioned previously, Docker allows you to package your application with all the dependencies into a container. Docker also keeps track of each container image you build and versions them for you, much like version control systems such as Git. And since every container is a standardized portable file, you can be sure that your application will run 100% the same on any machine.
With Docker, the headache of managing your application in different environments, such as development and production, is significantly reduced. Because a docker image contains the complete runtime environment and dependencies for your application to run, it solves the dreaded developer problem of “it works on my machine.”
Alright, Let’s get some definitions and terminology out of the way!
Docker Container Image
A Docker Container Image is a portable, executable package containing your application code, runtime, libraries, and everything required for your application to run smoothly and securely. Many people use the words image and container interchangeably; however, they are not the same thing. You can think of an image as a blueprint for architectural design or a meal recipe.
A Docker Container or simply Container is the result or finished product of your blueprint or recipe. In technical terms, It’s the name given to a running instance of your docker container image. You can launch multiple container instances from one container image, much like how you can create several meals using the same recipe.
Suppose a container is a result or final product of a recipe or blueprint. In that case, Dockerfile is the instructions on how to put that final product or recipe together. Dockerfile contains the complete instructions on how Docker should build your container image, which resources and assets to include, the ports that the container exposes, etc. It’s a step by step definition file on how to construct a container image.
When you build your container image, you can store it inside a Container Registry such as Docker Hub, AWS ECR, and Google Container Registry. Once you push the container image into a container registry, you can later pull from the registry to launch multiple container instances.
Let’s run our first hello-world example. First, make sure Docker is running on your machine. Open your terminal and run the following command:
docker run hello-world
The output in your terminal should look something like this:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly.
Congrats! You’ve just run your first docker container. Although it’s not a very sophisticated application, it’s a testament to how easy it is to run an application inside a docker container.
Docker container from scratch
Create a folder on your desktop or any directory. I’ll call mine hello-docker.
mkdir hello-docker && cd hello-docker
# for windows
md hello-docker && cd hello-docker
Create a file in that directory and name it Dockerfile
# for windows type nul > Dockerfile
Next, open the file with your favorite text editor. I’ll use vscode for this example and add the following to the Dockerfile.
FROM alpine:3.7 ENV HELLO="hello Docker" ENTRYPOINT echo $HELLO
FROM – pulls the alpine Linux image and adds it to this container as a base
ENV – sets an environment variable inside the container with the value of “hello Docker”
ENTRYPOINT – Is called as soon as the container starts, and it runs the echo command.
Alright, Let’s build and run the container. Open up your terminal and if you’re not already there, navigate to the project directory hello-docker. Once there, run the following command:
docker build -t hello-docker .
docker run hello-docker
Thanks for reading!
That brings us to the end of part one.
Hang in there because, In part two, we create a more real-world example by building a simple HTML web application and deploying a container with Nginx to Digital Ocean.