Explaining Docker with Pizza!

Framework/Tools

Imagine a food cart that stands out from the crowd by offering a unique and optimized approach to pizza preparation and delivery. Docker is like a digital version of this food cart, but instead of pizzas, it optimizes the process of building, shipping, and running software applications. Docker allows developers to package their applications, along with all dependencies, into these portable and consistent "containers," resembling a pizza that is ready to be served in a box.

Let’s explore all the related terms in Docker using this food cart analogy.

Dockerfile(Recipe Book)

Dockerfile is like a recipe book. It contains all the step-by-step instructions and ingredients needed to prepare the pizza. In software world, it’s a text file with set of instructions on how to build the "image".

Here is a sample Dockerfile to dockerize a React application-

# Use an official Node.js runtime as the base image
FROM node:14-alpine

# Set the working directory within the container
WORKDIR /app

# Copy the package.json and package-lock.json (or yarn.lock) files to the working directory
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Build the React app
RUN npm run build

# Specify the command to run when the container starts
CMD ["npm", "start"]

Docker Image (Prepared Ingredients)

Imagine we've prepared the ingredients for the signature pizza dish, based on the recipe book (Dockerfile).We have all the dough, sauce, cheese, and toppings neatly organized and ready to go. Once the ingredients are prepared, we put them in a sealed box to keep them fresh and uncontaminated until we are ready to use them.

The Docker Image is like that sealed box of prepared pizza ingredients. It's a self-contained package that contains all the necessary code, libraries, configurations, and files required to run the software application. It cannot be modified and remains unchanged during runtime.

To create a docker image from Dockerfile, use the following command-

docker build -f Dockerfile -t react-app .
  • f : Path to the docker file
  • t : Name and tag for the image
  • . : The . command indicates that the build context is the current directory, which should contain your Dockerfile and application files.

After the build is complete, you can use the docker images command to see your newly created image in the list of available Docker images:

docker images

Just like the raw ingredient can’t be eaten yet, Docker Image is not directly executable or runnable on its own.It serves as the starting point or the blueprint to create Docker Containers.

Docker Container (Cooked Dish)

When we’re ready to serve your pizza, we take the prepared ingredients (Docker Image) and use them to cook the pizza. We follow the instructions and combine the dough, sauce, cheese, and toppings to create the final, delicious pizza.

Similarly, when we run a Docker Image, it becomes a Docker Container. The Docker Container is the actual, runnable instance of the software application, where the code, configurations, and libraries from the Docker Image come to life. It's like the cooked pizza, ready to be served and interacted with.

For example, if you want to run a container from an image named "react-app" and your React app is listening on port 3000 inside the container, and you want to expose it on port 8080 on your host, you would run:

docker run -p 8080:3000 react-app

You can use other Docker commands like docker ps to see the running containers and manage them.

Docker Compose (Combo Meal)

Imagine we've decided to offer combo meals to customers, where they can get a pizza, a side dish, and a drink all together. Each item in the combo meal is like a separate container that contains a different component of the meal:

The Pizza Container contains the pizza, including the dough, sauce, cheese, and toppings, just like we discussed earlier.

  • The Side Dish Container contains another tasty dish, like garlic bread or salad.

  • The Drink Container contains the refreshing beverage to complement the meal.

With Docker Compose, we can define the specifications for the combo meal. We create a YAML file, similar to creating a combo meal on our menu. In this YAML file, you specify which containers (pizza, side dish, drink) to include in the combo meal and how they should interact with each other. In software world in our yaml file we define any required configurations, network connections, and volumes for the application to run. Here is a sample of a Docker Compose file docker-compose.yml for a basic React app with a Nginx server-

version: '3.7'
services:
  react-app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - .:/app
    working_dir: /app
    command: npm start

  nginx:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./build:/usr/share/nginx/html

To start the application run the command-
docker-compose up

To stop the application run the command-
docker-compose down

Docker Volume (Storage Space)

Docker Volume is like a storage space in our cart. It's a place where we store additional ingredients, utensils, or supplies that are not immediately needed for cooking. Docker Volumes allow data to persist between container runs, just as the storage space in our cart allows you to keep things for future use.

Docker Registry

Docker Registry is like a central warehouse for our food cart business, where we store all the secret pizza recipes and pizza ingredients. It acts as a repository, similar to how Docker Registry stores Docker Images – the pre-prepared application packages. Docker Registry enables easy sharing and distribution of Docker Images, ensuring consistent software deployment across different environments.

The benefits of using Docker are truly appetizing:

  1. Consistency - Docker ensures applications run consistently across different environments. This consistency eliminates issues caused by variations in software setups, making deployment more reliable.

  2. Portability - Similar to how a food cart can move to different places, Docker's containers are portable, enabling easy deployment and scaling across various platforms.

  3. Isolated and Secure Environment: Like in the food cart we can have separate stations for preparing each kind of meal, Docker's containers provide isolation, ensuring one application's changes don't affect others, ensuring security and stability.