Here's a beginner-friendly Docker Tutorial to get you started. Docker is an essential tool for creating, deploying, and running applications in isolated containers. It simplifies the development and deployment process by allowing you to package your application and its dependencies into a container that can run consistently across different environments.
What is Docker?Docker is an open-source platform that automates the deployment of applications inside lightweight containers.
Containers are isolated environments that can run an application and all its dependencies.
Docker helps developers to build, ship, and run applications consistently across various environments.
In this tutorial, we will:
- Install Docker on your local machine.
- Create a Dockerfile for a Node.js application.
- Build a Docker image for the application.
- Run a container from the Docker image.
- Push the Docker image to Docker Hub (optional).
- Use Docker Compose for multi-container applications (optional).
Step 1: Install Docker
First, you need to install Docker on your machine. Follow the instructions for your operating system:
- Windows: Docker Desktop for Windows
- macOS: Docker Desktop for macOS
- Linux: Install Docker Engine on Linux
After installation, verify that Docker is working by running the following command in your terminal:
docker --version
You should see the Docker version if everything is installed correctly.
Step 2: Create a Simple Node.js Application
Let's create a simple Node.js application that we can run inside a Docker container.
1 Create a new directory for the project:
mkdir node-docker-example
cd node-docker-example
2 Initialize a new Node.js project:
npm init -y
3 Create an index.js file with a basic Node.js server:
// index.js
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Docker World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
4 Create a package.json file with the following content (ensure start script is defined):
{
"name": "node-docker-example",
"version": "1.0.0",
"description": "Node.js app to run inside Docker",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {}
}
Step 3: Create a Dockerfile
Now, let's create a Dockerfile, which contains instructions for building a Docker image for our Node.js app.
1 Create a new file named Dockerfile (no extension):
# Use the official Node.js image from the Docker Hub
FROM node:16
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to install dependencies
COPY package*.json ./
# Install the dependencies
RUN npm install
# Copy the rest of the application files
COPY . .
# Expose the application port (3000)
EXPOSE 3000
# Define the command to run your app
CMD ["npm", "start"]
Explanation:
- FROM node:16: This pulls the official Node.js image version 16 from Docker Hub.
- WORKDIR /usr/src/app: This sets the working directory inside the container where the app will be stored.
- COPY package*.json ./: This copies package.json and package-lock.json into the container to install the dependencies.
- RUN npm install: Installs the app's dependencies.
- COPY . .: Copies the rest of the application files into the container.
- EXPOSE 3000: Exposes port 3000 to access the app from outside the container.
- CMD ["npm", "start"]: Runs the application using the npm start command.
Step 4: Build the Docker Image
Now that we have our Dockerfile, let's build the Docker image.
1 In the project directory, run the following command:
docker build -t node-docker-example .
This command will:
Build a Docker image using the current directory (.).
Tag the image as node-docker-example.
2 Once the build process is complete, you should see an image listed when you run:
docker images
The output will list all available Docker images, including the node-docker-example image.
Step 5: Run the Docker Container
To run the Docker container from the image we just created, use the following command:
docker run -p 3000:3000 node-docker-example
Explanation:
- -p 3000:3000: This maps port 3000 of your local machine to port 3000 inside the container (the port the Node.js app is listening on).
- node-docker-example: This is the name of the image we built.
Once the container is running, open your browser and go to http://localhost:3000. You should see the message "Hello, Docker World!"
Step 6: Push the Docker Image to Docker Hub (Optional)
If you want to share your Docker image, you can push it to Docker Hub.
1 First, log in to Docker Hub:
docker login
2 Tag your image with your Docker Hub username:
docker tag node-docker-example <your-username>/node-docker-example
3 Push the image to Docker Hub:
docker push <your-username>/node-docker-example
Replace <your-username> with your Docker Hub username.
Step 7: Using Docker Compose for Multi-Container Applications (Optional)
For more complex applications, you might need to run multiple containers (e.g., a Node.js app and a database). Docker Compose simplifies this process.
1 Create a docker-compose.yml file:
version: '3'
services:
app:
build: .
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
environment:
NODE_ENV: development
2 Run the application with Docker Compose:
docker-compose up
This command will:
- Build the Docker image based on the Dockerfile.
- Start the container and expose the app on port 3000.
To stop the application, use:
docker-compose down
Step 8: Clean Up
After you're done with the tutorial, don't forget to clean up your containers and images.
1 To remove all stopped containers:
docker container prune
2 To remove all unused images:
docker image prune -a
Conclusion
In this tutorial, you have learned how to:
- Set up a Node.js application.
- Create a Dockerfile to containerize the app.
- Build and run Docker containers.
- Optionally push Docker images to Docker Hub.
- Optionally use Docker Compose for managing multi-container applications.
Docker is a powerful tool for creating consistent environments for your applications, and mastering Docker is essential for modern software development.
Happy coding and enjoy Dockerizing your applications! 🚀