Photo by Element5 Digital on Unsplash
Docker Tutorial Series: Part 3
In part 3 of this series for Docker, you will learn about the basics of Docker, including Dockerfiles, images, and the daemon process.
7 min read
Understanding Docker with an Analogy
Docker is a popular technology that allows developers to package their applications and dependencies into small, portable units called "containers." Here's an analogy to help explain what Docker is and how it works:
Imagine you're a chef who wants to cook a meal for your customers. You have all the necessary ingredients but need a kitchen to cook in. Instead of building a new kitchen from scratch, you decide to use a food truck as your kitchen. The food truck has everything you need to cook your meal, including an oven, stove, and refrigerator. In this analogy, Docker is like a food truck. The chef represents the developer with all the necessary code and dependencies to run their application. Instead of building a new environment from scratch, the developer uses Docker to package their application and dependencies into a container, which can be run on any machine with Docker installed. Just as the food truck can be driven to different locations to serve customers, Docker containers can be deployed to different environments (such as a development machine, staging server, or production server) to run the application. And just as the food truck can be easily replicated to serve more customers, Docker containers can be easily replicated to handle more traffic or to scale up the application.
We will use the same analogy to understand docker concepts further.
Docker concepts and terminologies
To ensure that you understand the upcoming blogs, you must first understand the terminologies related to Docker. We can break down the components and related terminologies at a high level into the following parts.
In the chef and food truck analogy, the Dockerfile would be like the chef's recipe to cook the meal. Just as a recipe lists all the ingredients and steps needed to make a dish, a Dockerfile lists all the instructions and dependencies required to build a Docker container.
For example, a Dockerfile might specify the base operating system, any necessary libraries or packages, and the application code that needs to be included in the container. When the Dockerfile is built, it generates a container image that can spin up new containers with the same configuration. Going back to our analogy, if the chef wants to cook a new dish, they might need to modify the recipe to include new ingredients or cooking steps. Similarly, if a developer wants to modify their application, they might need to modify the Dockerfile to include new dependencies or configuration settings.
A basic Java/Spring Boot application will have the following instructions defined in the Dockerfile.
FROM openjdk:17 COPY target/*.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
FROM the instruction indicates the base image for our application.
COPY instruction will copy the local .jar file built by the build tool, i.e., Maven/Ant/Gradle, into our container image.
ENTRYPOINT instruction acts as a default executable for our container when it starts. We want to run the jar file using the
java -jar command in this example.
Overall, the Dockerfile is a crucial part of the Docker workflow because it allows developers to define and automate the process of building a Docker container. Using a Dockerfile, developers can ensure that their containers are built consistently and reproducibly, making it easier to deploy and manage their applications.
In the chef and food truck analogy, the Docker image would be like the prepared meal that the chef has cooked and packaged for serving to customers. In the same way, a Docker image is a packaged and portable unit that contains the application code and all of its dependencies. When the chef prepares a meal, they use a combination of ingredients, cooking techniques, and presentation to create a delicious and visually appealing dish that can be easily served to customers. Similarly, a Docker image is created by combining the application code, system dependencies, and runtime environment in a way that can be easily replicated and run on any machine that has Docker installed. Once the Docker image is created, it can be shared and used to create new containers. Just as the chef can use the same recipe for cooking multiple servings of the same dish, developers can use the same Docker image to spin up multiple instances of the same application.
By using Docker images, developers can easily share and deploy their applications consistently and reliably, making managing and scaling their software easier.
In the chef and food truck analogy, the Docker daemon would be like the food truck driver who operates and manages the food truck. The Docker daemon is a background process that runs on a host machine and drives the creation, deployment, and execution of Docker containers. The Docker daemon is responsible for receiving commands from the Docker client and then interacting with the host machine's operating system to perform tasks like starting and stopping containers, networking between containers, and managing the storage of container images. Similarly, the food truck driver is responsible for driving the truck to different locations, setting up the kitchen equipment, and serving the food to customers. They ensure that the food truck is well-maintained and operates smoothly, just as the Docker daemon ensures that Docker containers run efficiently and reliably.
By providing a centralized management system, the Docker daemon makes it easier to run and scale complex applications across multiple machines and environments.
In the chef and food truck analogy, the Docker CLI (Command Line Interface) would be like the order-taking and communication system the chef and food truck driver uses to coordinate their actions. The Docker CLI is a command-line tool that allows developers to interact with the Docker daemon and perform tasks like building and running containers. Using the Docker CLI, a developer can issue commands to the Docker daemon to create and manage containers, images, networks, and volumes. For example, a developer can use the docker run command to start a new container based on a specific Docker image.
Similarly, the chef and food truck driver use a communication system to coordinate their actions and ensure they provide customers with the food they want. The chef might receive orders through a tablet or phone app, and the food truck driver might use a walkie-talkie or messaging app to communicate with the chef and receive new orders.
Using the Docker CLI, developers can easily manage their containers and images and quickly spin up new environments for testing and development.
In the chef and food truck analogy, Docker Hub would be like a food distribution network or marketplace where chefs and food trucks can share and sell their recipes and prepared meals. Docker Hub is a cloud-based registry service that allows developers to store, share, and distribute Docker images. Developers can use Docker Hub to publish their images and share them with other developers, teams, or the wider community. They can also use Docker Hub to search for existing images and incorporate them into their applications. Similarly, a food distribution network or marketplace allows chefs and food trucks to share their recipes and meals with a broader audience. They can sell their meals through online platforms, grocery stores, or other distribution channels, which helps to expand their reach and generate more revenue.
Using Docker Hub, developers can easily find and use existing images and contribute their images to the broader community. It makes it easier to build and deploy complex applications using Docker and helps to promote collaboration and knowledge sharing among developers.
Here's a Mermaid diagram that summarizes what we have learned so far about Docker using the chef and food truck analogy:
In this diagram:
The developer creates a Dockerfile that defines the application and its dependencies.
The Dockerfile builds an image containing the application code and its dependencies.
The Docker image creates a Docker container that runs the application.
The developer uses the Docker CLI to interact with the Docker daemon, which manages the creation, deployment, and execution of Docker containers.
The Docker image can be stored and shared through Docker Hub, a centralized registry service for Docker images.
With the help of this diagram, we can conclude the relationships between the different components of the Docker workflow and how they relate to the chef and food truck analogy. The chef represents the developer, the food truck represents the Docker container, and the food distribution network represents Docker Hub.
Did you find this article valuable?
Support Ashish Choudhary by becoming a sponsor. Any amount is appreciated!