
August 20, 2025
How to Use Docker for Local Development Environments
How to Use Docker for Local Development Environments
The classic "it works on my machine" problem appears often to developers. Local workplaces can be very different. Behaviour is often not uniform when using different operating systems, software versions, or setups. In this case, Docker comes in handy.
Docker is a tool for containerisation that lets you execute applications in separate environments known as containers. These containers make sure that your app works the same on your system, on your coworker's system, or on a live server.
Set up a Node.js working environment with Docker in this article. It includes code, setup, and tips for a smooth process.
What is Docker and Why Use It?
With Docker, you can deploy your app and all of its resources into a standard block called a container. These containers can run the same way in different environments, which fixes a lot of problems with connectivity.
Benefits of Docker:
- Consistency: Always run the same code everywhere.
- Easy Onboarding: Developers may create a dev environment with a single command.
- Isolation: It is important to keep your applications and tools away from your host machine.
Installing Docker
Get Docker from the official site and install it before we start:
https://docs.docker.com/get-docker/
After installation, verify it by running:
docker --version
Setting Up a Node.js Docker Development Environment
Create a basic Node.js app that runs inside Docker. When you go to this app in the browser, it will say "Hello from Docker!"
Project Structure:
my-app/
âââ Dockerfile
âââ docker-compose.yml
âââ package.json
âââ index.js
1. Node.js App (index.js)
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => res.send('Hello from Docker!'));
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
2. Package.json
{
"name": "docker-node-app",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.18.2"
}
}
3. Dockerfile
# Use Node.js base image
FROM node:18
# Set working directory
WORKDIR /usr/src/app
# Copy package files and install dependencies
COPY package*.json ./
RUN npm install
# Copy app files
COPY . .
# Expose port
EXPOSE 3000
# Start the app
CMD ["npm", "start"]
Using the standard Node.js image, this Dockerfile makes a container image for your application. It sets up requirements, moves files, and tells the app how to start up.
4. Docker Compose for Simplicity
Use Docker Compose to run your service instead of long Docker scripts.
docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
- build: .: This command uses the Dockerfile to build.
- ports: connects port 3000 on your computer to port 3000 in the container.
- volumes: It is possible to change local code in real time by mounting volumes inside the container.
5. Run Your App
Open a terminal in the my-app folder and run:
docker-compose up --build
Visit http://localhost:3000 in your browser. You should see:
| Hello from Docker!
Enabling Live Reloading with Nodemon
When you make changes, you do not want to restart your container. You can live reload by adding Nodemon.
Install nodemon:
npm install --save-dev nodemon
Update your package.json:
"scripts": {
"start": "nodemon index.js"
}
Docker will immediately show any changes to files since we mounted the project folder as a drive.
Best Practices:
- Use .dockerignore to stop extra files from being added to the picture.
- Keep the Dev and Prod images separate. For testing and production, use different Dockerfiles or build steps.
- Keep containers light; only install what you need.
Common Pitfalls and Fixes
- Port Conflicts: Make sure that no other service is using the port (3000, for example).
- Permissions Issues: If you need to change who file ownership on Linux, you can use chown to do it.
- Nodemon Is Not Reloading: Make sure volumes are mounted properly and that node_modules is not allowed to replace volumes.
Conclusion
Docker makes it easy to set up and use regular local working setups. With just a few steps, you made a Node.js app that runs in a container and runs with a single command.
When you feel ready, try making your setup bigger:
- Put in a MongoDB container
- Make sure the frontend and backend containers are different.
- To keep your database alive, use Docker boxes.
Have fun writing code in containers!
104 views