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
- Download Docker Desktop for Windows: Go to the Docker Desktop download page. Download the installer that corresponds to your version of Windows
- Install Docker Desktop: Run the downloaded .exe file. Follow the installation instructions, accept the license agreement, and let the installer complete the setup
- 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
- Verify Installation: Open PowerShell or Command Prompt and type the following command:
You should see the Docker version information if the installation was successful.
Installing Docker on macOS
- Download Docker Desktop for macOS: Visit the Docker Desktop download page for macOS
- Install Docker Desktop: Open the .dmg file and drag Docker into your Applications folder
- Launch Docker: Open Docker from the Applications folder. Follow the prompts to complete the setup
- Verify Installation: Open the Terminal and run:
You should see the Docker version if everything is set up correctly.
Installing Docker on Linux
- Update Your Package Database:
- Install Required Packages:
- Add Docker’s Official GPG Key and Repository:
- Install Docker Engine:
- Start Docker and Enable It to Start at Boot:
- Verify Installation: Run
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
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"
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
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.
- 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: - Run a Container: Now, run a container using this image:
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
- Create a Project Directory:
- Initialize a Node.js Project:
- Add a Simple Server: Create a file called server.js with the following content:
mkdir my-node-app
cd my-node-app
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
- Create a Dockerfile: In the project directory, create a
Dockerfile
with the following contents: - Build the Docker Image: Run the following command to build the image:
- Run the Container: Start the container using:
# 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"]
docker build -t my-node-app .
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:
- 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
- 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:
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
- 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
- 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
- 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
- In the Project Directory, create a
docker-compose.yml
file: - Run Docker Compose, to start all services
- Stop Docker Compose, for you to stop all services, press
Ctrl + C
in the terminal, or run:
version: '3'
services:
app:
build: .
ports:
- "3000:3000"
db:
image: mongo
ports:
- "27017:27017"
Docker will build the app
service, pull the mongo
image for the db
service, and start both.
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.