Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
In this tutorial, you'll learn how to create a Docker container running Node.js, a popular JavaScript runtime built on Chrome's V8 JavaScript engine. We'll use the official Node.js image provided by the Docker community.
Prerequisites:
Pull the official Node.js image:
To pull the official Node.js image, run the following command:
docker pull node
This will download the latest version of the Node.js image to your system.
Create a sample Node.js application:
First, create a new directory for your Node.js application:
mkdir node-app cd node-app
Next, create a new file named app.js
in the node-app
directory with the following content:
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 World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
This is a simple Node.js web server that listens on port 3000 and responds with "Hello World" for every request.
Create a new container with Node.js:
To create a new Docker container running your Node.js application, use the docker run
command. We'll map the container's port 3000 (the port used by our Node.js application) to the host's port 3000, so you can access the application from your host machine. Additionally, we'll mount the local node-app
directory as a volume inside the container:
docker run --name node-container -p 3000:3000 -v $(pwd):/usr/src/app -w /usr/src/app -d node node app.js
This command creates a new container named node-container
, maps the container's port 3000 to the host's port 3000, mounts the local node-app
directory as a volume at /usr/src/app
inside the container, sets the working directory to /usr/src/app
, and runs the container in detached mode (-d
flag). The container will run the app.js
file using Node.js.
Verify the Node.js container is running:
To check if the Node.js container is running, use the docker ps
command:
docker ps
You should see your node-container
listed as running.
Access the Node.js application:
Open your browser and navigate to http://localhost:3000
. You should see the "Hello World" message from your Node.js application.
Update your Node.js application:
Since your node-app
directory is mounted as a volume inside the container, any changes you make to the app.js
file (or other files in the node-app
directory) will be reflected in the running container.
If you need to restart your Node.js application after making changes, use the docker restart
command:
docker restart node-container
In this tutorial, we covered how to create a Docker container running a simple Node.js application, access the application from your host machine, and update the application while it's running. You can now use this containerized Node.js environment for development, testing, or deployment purposes.
Running Node.js in Docker Container:
docker run -d -p 3000:3000 --name my-nodejs-container node:latest
How to Create a Docker Image with Node.js:
FROM node:latest # Add custom configurations or install dependencies
Docker-Compose Node.js Installation:
version: '3' services: nodejs: image: node:latest ports: - "3000:3000"
Node.js Dockerfile Example:
FROM node:latest WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . .
Setting Up Node.js in a Docker Environment:
version: '3' services: nodejs: image: node:latest ports: - "3000:3000" environment: NODE_ENV: production
Configuring Node.js in Docker-Compose:
version: '3' services: nodejs: image: node:latest ports: - "3000:3000" environment: NODE_ENV: production volumes: - ./app:/usr/src/app
Docker Node.js Image Customization:
FROM node:latest WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . .
Docker Node.js Volume Mounts:
version: '3' services: nodejs: image: node:latest ports: - "3000:3000" volumes: - ./app:/usr/src/app
Custom Node.js Image Creation in Docker:
FROM node:latest WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . .
Docker Swarm and Node.js Service:
version: '3' services: nodejs: image: node:latest deploy: replicas: 3 # Other Swarm deployment configurations
Troubleshooting Node.js in Docker:
docker logs <nodejs_container_id_or_name>