Introduction to Docker
Docker is a platform designed to make it easier to create, deploy, and run applications by using containers. Containers allow developers to package an application with all its dependencies into a standardized unit for software development. They ensure that the application will run on any other Docker container, regardless of any customized settings that the machine might have that could differ from those used for writing and testing the code.
Key Benefits of Docker
- Consistency: Containers ensure that the application runs the same in any environment.
- Isolation: Each container runs in isolation, ensuring the environment is clean and dependencies do not conflict.
- Portability: Containers can run on any system that supports Docker.
- Scalability: Containers can be easily scaled up or down, making them ideal for microservices architectures.
Docker Installation Guide
Install Docker on Ubuntu
- Update your existing list of packages:
sudo apt update
- Install a few prerequisite packages that let apt use packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- Add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Add the Docker repository to APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Update your package database with the Docker packages from the newly added repo:
sudo apt update
- Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ce
- Install Docker:
sudo apt install docker-ce
- Check if Docker is running:
sudo systemctl status docker
Install Docker on Windows
- Download Docker Desktop for Windows from the Docker Hub.
- Run the Docker Desktop Installer.exe to start the installation process.
- Follow the installation wizard to accept the license, authorize the installer, and proceed with the installation.
- Once the installation is complete, launch Docker Desktop from the Start menu.
- Verify the installation by opening a command prompt and running:
docker --version
Install Docker on macOS
- Download Docker Desktop for Mac from the Docker Hub.
- Open the .dmg file and drag the Docker icon to the Applications folder.
- Open Docker from the Applications folder.
- Verify the installation by opening a terminal and running:
docker --version
Important Docker Commands
Basic Commands
- Check the Docker version:
docker --version
- List all Docker images:
docker images
- List all running containers:
docker ps
- List all containers (including stopped ones):
docker ps -a
- Build an image from a Dockerfile:
docker build -t <image_name> .
- Run a container from an image:
docker run -p <host_port>:<container_port> <image_name>
- Stop a running container:
docker stop <container_id>
- Remove a stopped container:
docker rm <container_id>
- Remove an image:
docker rmi <image_id>
Advanced Commands
- View logs of a container:
docker logs <container_id>
- Enter a running container:
docker exec -it <container_id> /bin/bash
- Copy files from container to host:
docker cp <container_id>:<container_path> <host_path>
- Prune unused Docker objects (containers, images, volumes, and networks):
docker system prune
Dockerizing a Node.js Application
Prerequisites
- Docker installed on your machine
- Basic knowledge of Node.js and Docker
Step-by-Step Guide
- Create a Node.js Application
- Initialize a new Node.js project:
mkdir node-app && cd node-app && npm init -y
- Install Express (for example):
npm install express
- Create a simple server in
index.js
:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
- Create a Dockerfile
- In the root of your project, create a file named
Dockerfile
:
# Use the official Node.js image from the Docker Hub
FROM node:14
# Create and change to the app directory
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy application code
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Start the app
CMD ["node", "index.js"]
- Create a .dockerignore file
- In the root of your project, create a file named
.dockerignore
to exclude unnecessary files from the Docker build context:node_modules npm-debug.log
- Build the Docker Image
- Run the following command in your project directory:
docker build -t node-app .
- Run the Docker Container
- Run the container:
docker run -d -p 3000:3000 node-app
- Your Node.js application should now be accessible at
http://localhost:3000
.
Dockerizing a React.js Application
Prerequisites
- Docker installed on your machine
- Basic knowledge of React.js and Docker
Step-by-Step Guide
- Create a React Application
- Use Create React App to bootstrap a new React project:
npx create-react-app react-app && cd react-app
- Build the React Application
- Run the build command to create a production build:
npm run build
- Create a Dockerfile
- In the root of your project, create a file named
Dockerfile
:
# Use an official Node.js runtime as a parent image
FROM node:14 AS build
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Build the app
RUN npm run build
# Use an Nginx image to serve the app
FROM nginx:alpine
# Copy the build output to Nginx's web root
COPY --from=build /app/build /usr/share/nginx/html
# Expose the port Nginx runs on
EXPOSE 80
# Start Nginx
CMD ["nginx", "-g", "daemon off;"]
- Create a .dockerignore File
- In the root of your project, create a file named
.dockerignore
to exclude unnecessary files from the Docker build context:node_modules build npm-debug.log
- Build the Docker Image
- Run the following command in your project directory:
docker build -t react-app .
- Run the Docker Container
- Run the container:
docker run -d -p 80:80 react-app
- Your React application should now be accessible at
http://localhost
.
These guides provide a fundamental approach to Dockerizing Node.js and React.js applications.