Today, I explored an amazing tool that makes the software development and deployment process much easier—Docker. Here is link to my GitHub repo with Docker file : https://github.com/Pradnyas23/docker-forked-repo
In this blog, I'll cover:
✅ What Docker is and why it's useful
✅ Basic Docker commands with explanations
✅ How I used GitHub Codespaces as a virtual environment instead of my local computer
✅ Containerizing node.js app and pushing it to public repository.
What is Docker? 🐳
Docker is an open-source containerization platform that allows developers to package applications and their dependencies into lightweight containers. These containers can be easily shared, deployed, and run on any system, whether it's a developer's laptop, a testing server, or a production environment.
🔹 Why use Docker?
Eliminates environment compatibility issues
Ensures faster deployments
Works consistently across all platforms
Reduces resource overhead compared to traditional virtual machines
Basic Docker Commands 📝
Here are some fundamental Docker commands that helped me get started:
Command | Explanation |
docker --version | Check if Docker is installed and see the version |
docker pull <image> | Download an image from Docker Hub |
docker build -t my-app . | Build a Docker image from a Dockerfile |
docker images | List all available images on the system |
docker run -p 3000:3000 my-app | Run a container from the image and expose port 3000 |
docker ps | List all running containers |
docker stop <container_id> | Stop a running container |
docker rm <container_id> | Remove a stopped container |
docker tag my-app my-dockerhub/my-app:v1 | Tag an image before pushing to Docker Hub |
docker push my-dockerhub/my-app:v1 | Push the image to Docker Hub |
Using GitHub Codespaces Instead of a Local Computer 🌐
Rather than setting up everything on my local machine, I used GitHub Codespaces, a cloud-based development environment, to:
✅ Avoid installation issues
✅ Run Docker commands in a virtual machine
✅ Clone repositories and work on them in an isolated environment
This made the entire process faster and more efficient without overloading my local system.
Cloning a Node.js App from GitHub Repository
To practice Docker, I cloned a simple todo node.js application from Cloud Champ's GitHub repository using:
git clone https://github.com/N4si/learn_docker.git
cd learn_docker
Here is the link to a simple todo node.js application that you can clone to practice and master docker:
https://github.com/N4si/learn_docker.git
Containerizing a Node.js App with Docker and Pushing to Docker Hub 🚀
Prerequisites
Before we start, ensure you have:
✅ A Node.js application ready to containerize
✅ A Docker Hub account
Step 1: Create a Dockerfile
📝
A Dockerfile
tells Docker how to build your container. Below is a sample Dockerfile
for a Node.js application:
#use an official node.js runtime as the base image
FROM node:14-alpine
#set the working directory in the container
WORKDIR /usr/src/app
#copy package .json and package-lock.json to the working directory
COPY package*.json ./
#Install dependencies
RUN npm install
#copy the rest of the application code to working directory
COPY . .
#expose the port on which your node.js app runs
EXPOSE 3000
#Install dependencies
CMD ["npm","start"]
Step 2: Build the Docker Image 🏗️
Navigate to your project directory in the terminal and run:
docker build -t your-dockerhub-username/node-app .
This will create a Docker image named node-app
.
Step 3: Run the Container 🐳
Now, let’s start a container from the image:
docker run -p 3000:3000 your-dockerhub-username/node-app
Your Node.js app should now be running inside a Docker container on port 3000. 🎉
Step 4: Log in to Docker Hub 🔑
Before pushing the image, log in to your Docker Hub account:
docker login
Enter your credentials when prompted.
Step 5: Tag the Image 🏷️
Before pushing, tag the image with your Docker Hub username:
docker tag node-app your-dockerhub-username/node-app:v1
Step 6: Push the Image to Docker Hub 🚀
Now, push the image to your Docker Hub repository:
docker push your-dockerhub-username/node-app:v1
Your image is now available in Docker Hub and can be pulled from anywhere! 🎯
Here is the docker image that I pushed to DockerHub : https://hub.docker.com/repository/docker/pradnya23/mytodoapp/general
Credits 🎥
This blog is inspired by the Cloudchamp’s YouTube video on Docker. A huge thanks for the detailed tutorial.
Here are some useful links to learn Docker:
Cloud Champ’s Youtube Channel: https://youtu.be/q5S14cfOWfE?si=Ee8fTOY196lBsE92
Abhishek Veeramalla’s Youtube channel : https://www.youtube.com/playlist? list=PLdpzxOOAlwvLNOxX0RfndiYSt1Le9azze
TechWorld with Nana’s Youtube Channel : https://www.youtube.com/watch?v=pg19Z8LL06w