DEV Community

Cover image for It's Working on My Machine; "Deploy Your Machine Then"
JohnKagunda
JohnKagunda

Posted on

It's Working on My Machine; "Deploy Your Machine Then"

We’ve all heard the joke: “It’s working on my machine; deploy your machine then.” This joke highlights a common problem in software development: software that runs perfectly on a developer’s local machine might not work the same way when deployed to a different environment.

To tackle this issue, developers needed a way to ensure that software runs consistently across different machines. That’s where Docker containers come in.

What Are Docker Containers?

Docker containers are a technology that allows you to package your application and all its dependencies into a single, portable unit. Think of a Docker container as a lightweight, self-contained box that holds everything your application needs to run.

Why Use Docker Containers?

  1. Consistency: Since the container includes the application and all its dependencies, you can be sure that it will work the same way on any machine. Whether it’s your local development machine or a production server, the container will behave the same.

  2. Isolation: Containers run in isolation from each other and from the host system. This means that one container’s processes won’t interfere with another’s, and your application won’t affect the underlying system.

  3. Portability: Containers can be moved easily from one environment to another. You can build a container on your local machine and deploy it to a cloud server without worrying about compatibility issues.

Example: Running a Simple Web App in Docker

Let’s go through a basic example of using Docker containers. Suppose you have a simple web application that requires Python and some libraries to run.

  1. Create a Dockerfile: This file describes how to build your Docker image. It includes instructions for setting up the environment and installing dependencies.

    # Use the official Python image from Docker Hub
    FROM python:3.8
    
    # Set the working directory in the container
    WORKDIR /app
    
    # Copy the application code into the container
    COPY . /app
    
    # Install the dependencies
    RUN pip install -r requirements.txt
    
    # Define the command to run the application
    CMD ["python", "app.py"]
    
  2. Build the Docker Image: Run the following command in your terminal to create an image from the Dockerfile.

    docker build -t my-web-app .
    

    This command builds an image named my-web-app using the current directory (denoted by .) as the build context.

  3. Run the Docker Container: Start a container from the image you just built.

    docker run -p 5000:5000 my-web-app
    

    This command runs the my-web-app container and maps port 5000 of the container to port 5000 on your local machine.

  4. Access the Application: Open your web browser and go to http://localhost:5000. You should see your web application running.

By using Docker containers, you’ve packaged your application in a way that makes it easy to deploy and run consistently, no matter where you are. This eliminates the “it works on my machine” problem and simplifies the deployment process.

Conclusion

Docker containers are a powerful tool for ensuring your applications run smoothly across different environments. They provide consistency, isolation, and portability, making them a popular choice for modern software development. By using containers, you can avoid common deployment issues and focus on building great software.


Check out this video about Docker in 100 seconds

Top comments (1)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Cool post! Great quick introduction!

For anyone who wants to learn more, you can check out this free ebook here:

GitHub logo bobbyiliev / introduction-to-docker-ebook

Free Introduction to Docker eBook

💡 Introduction to Docker

This is an open-source introduction to Docker guide that will help you learn the basics of Docker and how to start using containers for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you will most likely have to use Docker at some point in your career.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Docker.

🚀 Download

To download a copy of the ebook use one of the following links:

📘 Chapters