Welcome to the Node.js and Docker Quick Start Guide! This guide will walk you through the process of setting up a basic Node.js application using Docker on an Ubuntu system. If you're new to Docker or want a quick refresher, follow the steps below to install Docker and create a Dockerized Node.js app.
sudo apt update
sudo apt 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
Adding Docker's GPG key ensures the authenticity and integrity of Docker packages.
echo "deb [signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
Check the status of your Docker service using
sudo systemctl status docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
Log out and log back in or run
newgrp docker
for the changes to take effect.
mkdir node-docker
cd node-docker
touch Dockerfile
Edit the Dockerfile with the following content:
# Dockerfile
# Use the official Ubuntu base image
FROM ubuntu:latest
# Update package lists and install Node.js
RUN apt-get update && apt-get install -y nodejs npm
# Create and set the working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package.json .
RUN npm install
# Copy the application files
COPY . .
# Expose a port
EXPOSE 3000
# Command to run the application
CMD ["npm", "start"]
docker build -t express-docker-image .
docker run -p 3000:3000 --name express-docker-container express-docker-image
docker ps
docker stop express-docker-container # or CONTAINER_ID
docker rm express-docker-container # or CONTAINER_ID
Feel free to explore and modify this setup based on your project requirements. Happy coding!