DEV Community

Cover image for Docker: A Simple Introduction with Legos
Angelo Costa
Angelo Costa

Posted on • Edited on

Docker: A Simple Introduction with Legos

Need help to understand how to start with docker? And has difficulty to understand all the terms and steps for building images or runing application? In bellow there is a little explanation using LEGOS!!!!

Docker: A Simple Introduction with Legos

What is Docker?

Imagine you are building a model with Legos. Each Lego piece represents an important part of your model. Docker is like a box of Legos that organizes all these pieces so you can build your models (applications) quickly and easily.

Why Use Docker?

1. Consistency

With Docker, it's like always having the same Lego pieces no matter where you are. Whether you're in your room, at a friend's house, or at a park, your Lego pieces will always be the same, ensuring your model works the same way everywhere.

2. Portability

Docker containers are like Lego boxes you can take anywhere. This means you can build and run your applications on any computer or server without worrying if the pieces will fit.

3. Resource Efficiency

Instead of having several large and heavy boxes (virtual machines), Docker allows you to use small and lightweight boxes (containers), which take up less space and are easier to carry.

4. Ease of Development

Docker makes building your models easy. You can organize your Lego pieces quickly and start building without wasting time looking for the right piece.

Docker in shape of pieces of lego on top of notebook

Why Do We Need to Build an Image?

Building an image is like assembling your Lego model so it's ready to use. Before you can start playing with it or showing it to your friends, you need to make sure all the pieces are in the right place and the model is complete. Building a Docker image ensures that your application is fully prepared to run.

How to Get Started with Docker

1. Installing Docker

First, you need to install Docker. You can find installation instructions here.

2. Creating Your First Container

Let's create a Lego box (container) for a simple Node.js application.

  1. Create a file called Dockerfile:

    # Use the Node.js image
    FROM node:14
    
    # Create a working directory
    WORKDIR /app
    
    # Copy the package.json file and install dependencies
    COPY package.json .
    RUN npm install
    
    # Copy the application code
    COPY . .
    
    # Expose the application port
    EXPOSE 3000
    
    # Command to run the application
    CMD ["node", "index.js"]
    
  2. Build the Docker image:
    Open the terminal and run:

    docker build -t my-node-app .
    

Here, Docker is assembling the Lego model. It pulls the Node.js base image, creates the working directory, installs dependencies, and copies the application code. At the end, you have a Docker image called my-node-app.

  1. Run the container:

    docker run -p 3000:3000 my-node-app
    

This is like taking the ready Lego model out of the box and starting to play with it. Docker creates a container from the my-node-app image and runs the application.

Stop everything!!!!!

This is a lot of information right? But why we use the step above???

Lego person thinking

I will explain little bit

Why Do We Need to Build an Image?

Building an image is like assembling your Lego model so it's ready to use. Before you can start playing with it or showing it to your friends, you need to make sure all the pieces are in the right place and the model is complete. Building a Docker image ensures that your application is fully prepared to run.

Understanding the Build Process
  1. Organize the Pieces:
    When you create a Dockerfile, you are telling Docker which pieces (dependencies, libraries, code) you need to assemble your model (application).

  2. Assemble the Model:
    By running the docker build command, Docker reads the Dockerfile and follows the instructions to assemble the model. This includes pulling the base image (like grabbing a base Lego set), adding your dependencies (like getting specific Lego pieces), and copying your application code (like putting the pieces in the right place).

  3. Create the Box:
    The result of the build process is a Docker image, which is like a box of assembled Lego ready to use. This image contains everything your application needs to run.

  4. Use the Ready Model:
    When you run a container from this image, it’s like taking the ready Lego model out of the box and starting to play with it. Everything is already set up and in place, so you can start using your application immediately.

Lego person with pieces lego behind

Using Docker Compose

Docker Compose is like a set of instructions for building a large model with several Lego boxes (containers). Let's create an example with a web application and a database.

  1. Create a file called docker-compose.yml:

    version: '3.9'
    services:
      web:
        image: my-node-app
        ports:
          - "3000:3000"
      db:
        image: postgres:13
        environment:
          POSTGRES_USER: user
          POSTGRES_PASSWORD: password
          POSTGRES_DB: mydb
    
  2. Start the services:
    Run in the terminal:

    docker-compose up
    

Lego girl reading instruction to create

Conclusion

Docker is like an amazing Lego box that makes it easy to build, transport, and run your applications. It ensures that your pieces (application components) always fit and work anywhere. Building a Docker image is essential because it prepares everything your application needs to run smoothly. Try Docker and see how it can transform the way you develop your applications.

Lego person saying good bye

All images was generated using AI

Top comments (5)

Collapse
 
mauroimamura profile image
Mauro Imamura

nice explanation, thanks for sharing 👏

Collapse
 
shahbazaldiablo profile image
Shahbaz Ahmad Siddiqui

Thanks Angelo Costa for this introduction to Docker...!!!
The AI images are great as well...

Collapse
 
ozama profile image
The gray gandalf

Thank you for sharing and it was fun to read

Collapse
 
bentesolution profile image
Irpan Abdul Rahman

awesome, enjoy when reading and understanding docker. thank you for the sharing

Collapse
 
rafaeljohn9 profile image
JohnKagunda

the lego analogy is amazing, thank you for sharing

Some comments may only be visible to logged-in visitors. Sign in to view all comments.