DEV Community

Cover image for Make Me A Container: The Docker Guide
lebePage
lebePage

Posted on

Make Me A Container: The Docker Guide

Absolutely dread setting up applications — the relentless pain of choosing libraries, tweaking environments, and all the headaches of making stuff work on your machine. But thank goodness for Docker, because those days are now just a crappy memory!

If you’ve been living under a rock, you might not have heard of Docker, but no worries — now that you’re here, I can fill you in on all the details.

So what is Docker?

Docker is an open-source platform that uses containerization technology to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Check the site: Docker website for more information.

Why Docker

There are plenty of reasons to get familiar with Docker, not least because it’s downright cool — and yes, it even has a charming whale as its mascot! But Enough jokes, let’s get technical.

Docker has revolutionized the way developers deploy and manage software applications. Here are several reasons why Docker has become an indispensable tool in modern development environments:

  1. Consistency Across Environments: Docker containers ensure that applications run the same way on any system. This eliminates the “it works on my machine” problem, where applications behave differently across environments due to varying configurations and dependencies.

  2. Rapid Deployment: Docker containers can be created and deployed in seconds, making the process of pushing changes or rolling out new features significantly faster compared to traditional methods of software deployment.

  3. Efficiency: Containers utilize system resources more efficiently than virtual machines, as they share the host system’s kernel and require less overhead. This makes Docker an economical choice for both development and production environments.

  4. Scalability and Modularity: Docker makes it easy to scale applications up or down with its orchestration capabilities. Containers can be replicated, managed, and monitored at scale, which is ideal for microservices architectures where components are loosely coupled and independently scalable.

Enough talk let’s get down to business the real reason you came here is to “LEARN DOCKER!”

Here’s a fascinating approach to understanding the power of Docker: We’re going to set up a full-stack Node.js application that handles basic CRUD operations, using MongoDB as the database. We’ll tackle this setup in two distinct ways — first, through a manual setup process without Docker, and then by utilizing Docker. This will illustrate the differences and advantages that Docker brings to the table.

Note: I had the pleasure of contributing to the Docker repository for this application. If you find the application helpful or impressive, please consider giving it a star⭐ on GitHub.

Setup without Docker:

Environment Preparation:

  • Install Node.js: Ensure that Node.js is installed on your system. You can download it from nodejs.org.
  • Install MongoDB: Follow the installation guide on the official MongoDB documentation.

Application Setup:

  • Initialize the repository: npm init -y
  • Navigate into the directory: cd your-repository-name
  • Install dependencies: npm install

Database Configuration:

Ensure MongoDB is running on your system.
Configure your MongoDB URI in your application to connect to your local database instance. check MongoDB website for more information

Run the Application:

Start the server: npm start
Your application should now be running on localhost:3000.

By following these steps, you can manually configure and run your Node.js application linked to a MongoDB database. However, consider the scenario where you need to replicate this setup across different machines and operating systems. It’s a daunting task, isn’t it? Now imagine scaling this process to 50 machines. The complexity and potential for errors multiply, making such a setup exceedingly challenging and stressful. But don’t worry docker is here for the rescue.

Let’s try replicating this step again but with Docker.

Setup with Docker:

Using Docker simplifies and streamlines the setup process, ensuring consistency regardless of the local development environment. Below, we’ll explore how to set up the same full-stack Node.js application using Docker, which includes the database and server environment.

  1. Prerequisites:
    Install Docker: Ensure Docker is installed on your system. You can download it from the official Docker website.

  2. Docker Configuration:
    Dockerfile: Create a Dockerfile in your project directory. This file will define the environment for your Node.js application. Here’s a basic example of what it might look like:


FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Enter fullscreen mode Exit fullscreen mode
  1. Docker Compose: Optionally, you can use Docker Compose to manage both your application and MongoDB services together. Create a docker-compose.yml file:
version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    environment:
      - MONGO_URI=mongodb://mongo:27017/mydatabase
  mongo:
    image: mongo
    ports:
      - "27017:27017"
Enter fullscreen mode Exit fullscreen mode
  1. Build and Run the Docker Container:
  • From the directory containing your Dockerfile and docker-compose.yml, run:
docker-compose up --build
Enter fullscreen mode Exit fullscreen mode
  • This command builds the Docker images for your application and MongoDB, and starts the containers as defined in your Docker compose file.
  1. Verify the Setup:
  • After the containers are up and running, you can access your application by visiting http://localhost:3000 in your web browser.

  • You should see your application running, connected to MongoDB running in another container.

By using Docker, you eliminate the need for manual environment setup and ensure that your application runs in an isolated and consistent environment. This method greatly reduces setup time and increases the reliability of your application across different machines. Docker encapsulates the application and its dependencies, making the deployment and scaling process straightforward and less prone to errors.

You can also eliminate this process by cloning the GitRepo on your machine and making your edits. Also, check out Awesome-Compose for more template.

Conclusion

In conclusion, the contrasting experiences of setting up and managing applications manually versus using Docker highlight the immense value Docker brings to development workflows. With its containerization technology, Docker simplifies the deployment process, enhances consistency across various environments, and significantly reduces the potential for errors associated with manual setups. Whether you’re developing locally or scaling across multiple platforms, Docker provides a reliable and efficient solution, making it an essential tool for developers aiming to streamline their development processes and focus more on innovation than infrastructure.

Thank you for taking the time to read this article! I hope you found it informative and useful.

👍 If you appreciated this post, please give it a like!

🔄 Feel free to share this with anyone who might benefit from it.

💬 I’d love to hear your thoughts and any tips you might have — drop a comment below!

👤 Follow me for more insightful blogs in the future!

Support my work with a coffee ☕ — thank you for your generosity!

Buy me coffee

Top comments (0)