How to Use Docker for Web Development (Beginner Tutorial)

Apr 14, 2026
Dailova Editorial
7 min read
How to Use Docker for Web Development (Beginner Tutorial)

Docker is a game-changer for web development, providing an easy way to containerize your applications and ensure consistency across different environments. Whether you are working on a small project or developing a complex application, Docker helps you avoid the "works on my machine" problem by creating a uniform environment for development, testing, and production. In this beginner tutorial, we’ll walk you through the basics of Docker, explain how to set it up for web development, and guide you through creating and managing containers. (docker.com)

If you're a web developer looking to streamline your workflow, Docker can make it easier to manage dependencies, isolate environments, and ensure that your app behaves the same way on every machine. From setting up your first container to building multi-container applications with Docker Compose, this guide will help you get started with Docker and use it to enhance your web development process.

What is Docker?

Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. These containers package an application and its dependencies, ensuring that it can run consistently across any environment. Docker containers are isolated from each other and the host system, which makes them a great tool for both development and production environments.

Why Docker for Web Development?

In web development, Docker is useful for the following reasons:

  1. Consistency: Ensure that your application works the same on every machine, from local development environments to production servers.
  2. Isolation: Isolate dependencies and services within containers, preventing conflicts between them.
  3. Portability: Docker containers can run on any system that supports Docker, allowing for easy sharing and collaboration between developers.
  4. Ease of Setup: Docker allows you to define your development environment in a simple file, making it easy to set up and configure your application across different environments.

Step 1: Install Docker

Before you can start using Docker, you need to install it on your system. Docker supports macOS, Windows, and Linux, and you can download the appropriate version for your operating system from the official Docker website.

  1. Go to the Docker Download page.
  2. Select your operating system and download the installer.
  3. Follow the installation instructions specific to your OS:
  4. Windows: Docker Desktop for Windows requires Windows 10 or later.
  5. macOS: Docker Desktop for Mac requires macOS Sierra 10.12 or later.
  6. Linux: Follow the instructions for your specific distribution (Ubuntu, CentOS, etc.).

Once installed, you can verify Docker’s installation by running the following command in your terminal or command prompt:


docker --version

This will display the installed Docker version, confirming that Docker is ready to use.

Step 2: Create Your First Docker Container

Now that Docker is installed, let’s create your first container. Containers are created from Docker images, which contain all the dependencies and settings needed to run an application. You can create a container from an official image, such as the nginx web server image.

  1. Open your terminal or command prompt.
  2. Pull the official nginx image from Docker Hub by running the following command:

docker pull nginx

This command downloads the nginx image, which is a lightweight web server commonly used in web development.

  1. Once the image is downloaded, you can run a container using the following command:

docker run -d -p 80:80 --name mynginx nginx

  1. The -d flag runs the container in detached mode (in the background).
  2. The -p 80:80 flag maps port 80 on the host machine to port 80 in the container, allowing you to access the container through your browser.
  3. The --name flag assigns a name to the container (in this case, “mynginx”).
  4. Now, open your browser and go to http://localhost. You should see the default nginx welcome page, indicating that your container is running successfully.

Step 3: Dockerizing a Simple Web Application

In web development, you’ll often need to containerize your web application along with its dependencies. Let’s walk through how to use Docker for a simple web application built with Node.js.

1. Create Your Node.js Application

Let’s start by creating a simple Node.js application.

  1. Create a new directory for your project and navigate into it:

mkdir myapp
cd myapp

  1. Initialize a new Node.js project:

npm init -y

  1. Create an index.js file with a simple HTTP server:

// index.js
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, '0.0.0.0', () => {
console.log('Server running at http://localhost:3000/');
});

2. Create a Dockerfile

A Dockerfile is a script that defines the environment for your application. It contains instructions on how to build a Docker image for your application.

  1. In the same directory as your index.js file, create a file named Dockerfile (no file extension).
  2. Add the following content to the Dockerfile:

# Use the official Node.js image
FROM node:14

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the app port
EXPOSE 3000

# Run the app
CMD ["node", "index.js"]

  1. The FROM node:14 line specifies the base image, which in this case is an official Node.js image.
  2. The WORKDIR line sets the working directory inside the container.
  3. The COPY lines copy the necessary files into the container.
  4. The CMD line tells Docker to run node index.js when the container starts.

3. Build the Docker Image

Now that you have your Dockerfile, it’s time to build the Docker image:


docker build -t mynodeapp .

This command tells Docker to build an image using the current directory (.) as the build context and tag the image as mynodeapp.

4. Run the Docker Container

Once the image is built, you can run your container:


docker run -d -p 3000:3000 --name mynodecontainer mynodeapp

This runs your Node.js app inside a container and maps port 3000 on the host to port 3000 in the container.

You can now visit http://localhost:3000 in your browser, and you should see the message “Hello, Docker!” displayed.

Step 4: Managing Docker Containers

As you start using Docker more frequently, you’ll need to learn how to manage your containers. Here are some useful commands for managing Docker containers:

  1. List running containers:

docker ps

  1. Stop a running container:

docker stop mynodecontainer

  1. Remove a container:

docker rm mynodecontainer

  1. View container logs:

docker logs mynodecontainer

Step 5: Using Docker Compose for Multi-Container Applications

Docker Compose allows you to define and manage multi-container Docker applications. For example, if you’re building a full-stack application with a backend and a database, you can define all the services in a single docker-compose.yml file.

  1. First, install Docker Compose by following the instructions in the official Docker docs. (docker.com)
  2. Create a docker-compose.yml file to define your services, for example:

version: '3'
services:
web:
build: .
ports:
- "5000:5000"
db:
image: postgres
environment:
POSTGRES_PASSWORD: example

  1. Run your application with Docker Compose:

docker-compose up

This command will start both the web server and the database, with all the necessary configurations defined in the docker-compose.yml file.

Conclusion

Docker is a powerful tool that can significantly improve your web development workflow by providing a consistent and isolated environment for your applications. In this tutorial, we walked you through the steps of setting up Docker for web development, including creating your first container, Dockerizing a simple Node.js app, and using Docker Compose for multi-container applications.

Whether you’re working on a single-page application or a complex web app, Docker can help you manage dependencies, simplify deployment, and ensure that your app behaves consistently across all environments. By using Docker for web development, you can streamline your development process and make it easier to manage and scale your applications.

Share This Article

Get Updates

Subscribe to get the latest articles delivered to your inbox.