How to Use Docker for Local Development Environments


How to Use Docker for Local Development Environments
Image by Author

 

Docker has become essential for developers seeking consistent, efficient local development environments. By using containers, Docker encapsulates applications and their dependencies, creating isolated environments that run consistently across various setups. With Docker, developers no longer worry about “it works on my machine” issues, as Docker ensures the environment is the same wherever it’s deployed. This guide will cover how to set up Docker for local development, so you can experience its power firsthand.

 

Prerequisites

 
Before we start, here are some essentials that will help you follow along smoothly:

  • Familiarity with the Command Line (CLI): Docker commands are primarily run from the command line, so some basic knowledge will be helpful
  • Basic Docker Concepts: A general understanding of terms like containers and images will make this easier to follow, though we’ll explain the key points as we go along

We’ll cover everything from installation to running your first container, so you’ll have a solid foundation by the end.

 

Step 1: Installing Docker on Your System

 
The first step in using Docker for local development is to install it on your machine. Here’s how to do it for Windows, macOS, and Linux.

 

Installing Docker on Windows

  1. Download Docker Desktop for Windows: Go to the Docker Desktop download page. Download the installer that corresponds to your version of Windows
  2.  
    Download Docker Desktop for WindowsDownload Docker Desktop for Windows
     

  3. Install Docker Desktop: Run the downloaded .exe file. Follow the installation instructions, accept the license agreement, and let the installer complete the setup
  4. Start Docker Desktop: Once installed, launch Docker Desktop from the Start menu. Docker will start and run in the background, making Docker commands available on your command line
  5. Verify Installation: Open PowerShell or Command Prompt and type the following command:
  6.  
    You should see the Docker version information if the installation was successful.

 

Installing Docker on macOS

  1. Download Docker Desktop for macOS: Visit the Docker Desktop download page for macOS
  2. Install Docker Desktop: Open the .dmg file and drag Docker into your Applications folder
  3. Launch Docker: Open Docker from the Applications folder. Follow the prompts to complete the setup
  4. Verify Installation: Open the Terminal and run:
  5.  
    You should see the Docker version if everything is set up correctly.

 

Installing Docker on Linux

  1. Update Your Package Database:
  2.  

  3. Install Required Packages:
  4. sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common

     

  5. Add Docker’s Official GPG Key and Repository:
  6. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    sudo add-apt-repository "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

     

  7. Install Docker Engine:
  8. sudo apt-get update
    sudo apt-get install -y docker-ce docker-ce-cli containerd.io

     

  9. Start Docker and Enable It to Start at Boot:
  10. sudo systemctl start docker
    sudo systemctl enable docker

     

  11. Verify Installation: Run
  12.  
    You should see the Docker version information if the setup was successful

 

Step 2: Running Your First Docker Container

 
With Docker installed, let’s run a simple container to make sure everything works as expected.

  1. Pull an Image: In Docker, images are the building blocks for containers. Let’s start with the hello-world image, which is commonly used for testing:
  2.  

  3. Run a Container: Now, run a container using this image:
  4.  
    This command downloads the hello-world image if it’s not already on your system and then runs it. You should see a welcome message confirming that Docker is working.

 

Step 3: Setting Up a Local Development Environment

 
Now that Docker is running, let’s set up a basic development environment using Docker. We’ll create a simple Node.js application and run it in a container.
 

Create a Basic Node.js Application

  1. Create a Project Directory:
  2. mkdir my-node-app
    cd my-node-app

     

  3. Initialize a Node.js Project:
  4.  

  5. Add a Simple Server: Create a file called server.js with the following content:
  6. const http = require('http');
    const server = http.createServer((req, res) => 
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello, Docker!\n');
    );
    
    server.listen(3000, () => 
      console.log('Server running at http://localhost:3000/');
    );

     

 

Dockerizing the Application

  1. Create a Dockerfile: In the project directory, create a Dockerfile with the following contents:
  2. # Use an official Node.js runtime as a base image
    FROM node:14
    
    # Create and set the working directory
    WORKDIR /app
    
    # Copy the package files and install dependencies
    COPY package*.json ./
    RUN npm install
    
    # Copy the rest of the application code
    COPY . .
    
    # Expose the application port
    EXPOSE 3000
    
    # Define the command to run the app
    CMD ["node", "server.js"]

     

  3. Build the Docker Image: Run the following command to build the image:
  4. docker build -t my-node-app .

     

  5. Run the Container: Start the container using:
  6. docker run -p 3000:3000 my-node-app

     

    Open a browser and go to http://localhost:3000 to see the message Hello, Docker!

 

Step 4: Viewing and Managing Containers with Docker Desktop

 
Docker Desktop offers a graphical interface to help you manage your Docker environment, providing an intuitive way to view running containers, images, networks, and volumes. If you’re new to Docker or prefer a visual tool, Docker Desktop can greatly enhance your workflow. Here’s a step-by-step guide on how to use Docker Desktop to manage your containers:

  1. Open Docker Desktop: First, launch Docker Desktop by selecting it from your applications or the Start menu (on Windows). Ensure Docker Desktop is running, enabling you to manage all Docker-related tasks through the graphical dashboard
  2. Access the Docker Dashboard: Upon opening Docker Desktop, you’ll see the Dashboard screen, which provides an overview of your Docker setup. Key sections include:
  • Viewing and Managing Running Containers: When you start a container (e.g., with docker run or docker-compose up), it will appear in the Containers/Apps tab. Here’s what you can do in this view:
    • Start/Stop/Restart Containers:Use the icons next to each container to control it directly from the dashboard
    • View Logs: Click on a container name to see real-time logs, which helps debug or monitor application activity
    • Inspect Container Details: You can review settings like environment variables, port configurations, and networking details within each container’s details
  • Managing Docker Images: In the Images section, Docker Desktop displays all images stored locally on your machine. This tab allows you to:
    • Pull New Images:Easily pull images from Docker Hub directly through the search feature
    • Delete Unused Images: Free up space by removing unused images with the trash icon
    • Build Images from Dockerfiles: If Dockerfiles are set up, you can use docker build from the command line or create images within Docker Desktop
  • Using Docker Desktop for Docker Compose Applications: Docker Desktop also makes it easier to manage multi-container applications defined with Docker Compose:
    • Starting and Stopping Services:Each service from your docker-compose.yml file appears under Containers/Apps, allowing you to start or stop services individually.
    • Viewing Network Information: Docker Compose creates a network for communication services, which you can manage from the Networks tab
  • Cleaning Up Unused Resources: Docker Desktop includes a built-in cleanup feature, which helps maintain an organized environment:
    • Resource Cleanup:From the Docker Desktop settings, you can clear out unused containers, images, networks, and volumes to free up disk space

     

    Step 5: Using Docker Compose for Multi-Container Applications

     
    For more complex setups, you may need multiple services running together (like a database with an application server). Docker Compose simplifies this by allowing you to define and run multi-container applications with a single configuration file.

     

    Create a docker-compose.yml File

    1. In the Project Directory, create a docker-compose.yml file:
    2. version: '3'
      services:
        app:
          build: .
          ports:
            - "3000:3000"
        db:
          image: mongo
          ports:
            - "27017:27017"

       

    3. Run Docker Compose, to start all services
    4.  

      Docker will build the app service, pull the mongo image for the db service, and start both.

    5. Stop Docker Compose, for you to stop all services, press Ctrl + C in the terminal, or run:
    6.  

     

    Conclusion

     
    Using Docker for local development brings stability, flexibility, and ease of management of the environment. No matter what operating system you’re using, in this guide, you’ll learn how to install, build, and run containers on Windows, Linux, and macOS and orchestrate multiple container applications with Docker Compose. Docker Desktop is also a visual tool. Powerful for monitoring and managing your containers and images, it streamlines the entire development process.

    Using Docker in your workflow doesn’t just eliminate the problem. Not only is it “this works on my machine,” but it also makes it easier to collaborate and adapt to production. As you continue to work with Docker, you’ll discover new ways to do it. To optimize and customize your settings This creates a truly efficient development environment. Whether you’re building a simple application or a multi-service solution, Docker helps you develop confidently. Knowing that your environment remains consistent and scalable from local setup to production.
     
     

    Shittu Olumide is a software engineer and technical writer passionate about leveraging cutting-edge technologies to craft compelling narratives, with a keen eye for detail and a knack for simplifying complex concepts. You can also find Shittu on Twitter.



    Recent Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here