Dockerize a nodejs App
creating a Docker container for a Node.js application:
bash
Copy code
mkdir?my-node-app?cd?my-node-app
bash
Copy code
npm init -y npm install express
javascript
Copy code
const?express =?require('express');?const?app =?express(); app.get('/',?(req, res) =>?{ res.send('Hello, World!'); }); app.listen(3000,?() =>?{console.log('Server listening on port 3000'); });
bash
Copy code
node app.js
领英推荐
bash
Copy code
# Use an official Node.js runtime as a parent image?FROM node:14-alpine# Set the working directory to /app?WORKDIR /app?# Copy the current directory contents into the container at /app?COPY . /app?# Install any necessary dependencies?RUN npm install?# Make port 3000 available to the world outside this container?EXPOSE 3000?# Define the command to run your app when the container starts?CMD [?"node",?"app.js"?]
bash
Copy code
docker build -t my-node-app .
bash
Copy code
docker run -p 3000:3000 my-node-app
Congratulations! You've just created a Docker container for a Node.js application.