If you want to pull a Node image from Docker Hub and run Node.js code directly in the container without creating an application on your local machine first, you can follow these steps:
Step 1: Pull the Node Image
docker pull node
Step 2: Run the Node.js Container
Next, run the Docker container in interactive mode with a volume mounted. This will allow you to create and run your Node.js code directly in the container:
docker run -it –name my-node-app -v ${PWD}:/usr/src/app -w /usr/src/app node bash
(if error use below)
docker run -it –name my-node-app -v $PWD -w /usr/src/app node bash
docker run -it –name my-node-app -v C:\Users\Admin\Desktop\mlc\Linux\dest-loc:/usr/src/app -w /usr/src/app -p 3000:3000 node bash (Windows)
Use absolute paths to avoid issues with shell interpretation.
Using ${PWD} within a shell:
docker run -it –name my-node-app -v ${PWD}:/usr/src/app -w /usr/src/app -p 3000:3000 node bash
Using the absolute path directly:
Replace /path/to/your/project with your actual absolute path:
docker run -it –name my-node-app -v /path/to/your/project:/usr/src/app -w /usr/src/app -p 3000:3000 node bash
If your current working directory is /home/user/my-node-app, the command would look like this:
docker run -it –name my-node-app -v /home/user/my-node-app:/usr/src/app -w /usr/src/app -p 3000:3000 node bash
-v $(pwd):/usr/src/app: Mounts the current directory (or any specific directory) to /usr/src/app inside the container.
-w /usr/src/app: Sets the working directory inside the container to /usr/src/app.
node: Specifies the Node.js image to use.
bash: Opens a Bash shell in the container.
(or) running container without volume
docker run -it –name my-node-app node bash
Docker doesn’t support adding volumes to an already running container.
To mount the current directory to an already running container, you typically need to stop the container, remove it, and then re-run it with the appropriate -v option.
Stop the Container: docker stop my-node-app
Remove the Container: docker rm my-node-app
Run the Container with Volume Mount:
docker run -it –name my-node-app -v ${PWD}:/usr/src/app -w /usr/src/app node bash (if error use below)
docker run -it –name my-node-app -v $PWD -w /usr/src/app node bash
(or)
docker run -it –name my-node-app -v C:\Users\Admin\Desktop\mlc\Linux\dest-loc:/usr/src/app -w /usr/src/app -p 3000:3000 node bash (Windows)
docker cp c:/users/admin/desktop/mlc/linux/dest-loc/ docker:/usr/src/app
Verify the Mount
After running the container with the mounted volume, you can verify that the mount is working as expected:
docker exec -it my-node-app bash
Inside the container, check the contents of the /usr/src/app directory to ensure it reflects the
contents of your current directory on the host:
ls /usr/src/app
Step 3: Create a Node.js Application in the Container
Now, you are inside the container. You can create a simple Node.js application directly here.
Create a file called app.js:
echo ‘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}/`);
});’ > app.js
Create a package.json file:
echo ‘{
“name”: “docker-node-app”,
“version”: “1.0.0”,
“description”: “”,
“main”: “app.js”,
“scripts”: {
“start”: “node app.js”
},
“dependencies”: {}
}’ > package.json
Step 4: Install Dependencies
npm install
Step 5: Run the Node.js Application
npm start
Access the Application
Your Node.js application is now running inside the container. You can access it at http://localhost:3000 from your host machine.
2. Creating a Dockerfile for Nodejs
Running Node.js code in a Docker container using a Node image involves creating a Dockerfile, building the Docker image, and running a container from that image.
Step 1: Create a Node.js Application
a. “app.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 World\n’);
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
b. “package.json”
{
“name”: “docker-node-app”,
“version”: “1.0.0”,
“description”: “”,
“main”: “app.js”,
“scripts”: {
“start”: “node app.js”
},
“dependencies”: {}
}
c.”Dockerfile”
Create a file named Dockerfile in the root directory of your Node.js application
# 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 package.json and package-lock.json (if available)
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port that the app runs on
EXPOSE 3000
# Command to run the application
CMD [“npm”, “start”]
Step 3: Build the Docker Image
docker build -t docker-node-app .
This command builds the Docker image and tags it as docker-node-app.
Step 4: Run the Docker Container
Once the image is built, you can run it as a container:
docker run -p 3000:3000 docker-node-app
If we want to run in detach mode :
docker run -d -p 3000:3000 –name docker-node-app docker-node-app
5. Access the Running Container
docker exec -it my-node-app bash
1.Ensure the Container Is Running: Use docker ps to list running containers.
2.Access the Container: Use docker exec -it docker-node-app bash to open a Bash shell inside the container.
Step 5: Verify the Application
http://localhost:3000
Mount a Path from Windows Host to Container
To mount a path from a Windows host machine to a Docker container, you need to use the -v or –mount option with the docker run command.
1. Using -v Option
The -v option allows you to mount a directory from the host to the container.
docker run -it –name my-container -v C:\path\to\your\directory:/path/in/container image_name
docker run -it –name my-node-app -v C:\Users\YourUsername\my-node-app:/usr/src/app -w /usr/src/app -p 3000:3000 node bash
2. Using –mount Option
The –mount option provides more flexibility and is the recommended approach for newer Docker versions.
docker run -it –name my-container –mount type=bind,source=C:\path\to\your\directory,target=/path/in/container image_name