This quick tutorial shows you how to run a Node.js Express Server on Podman.
Step 1: Create Node.js Express Server
Let’s create a simple Node.js Express Server to demonstrate the usage of each command. Skip this step if you already have a Node.js application for this exercise.
$ mkdir my-server && cd my-server/
$ npm init
...
...
$ npm install express
$ vi server.js
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`My server listening at http://localhost:${port}`))
$ vi package.json
{
"name": "my-server",
"version": "1.0.0",
"description": "Node.js Express Server",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Step 2: Build Podman image
Create a Dockerfile.
$ vi Dockerfile
FROM node:10-alpine
RUN mkdir -p /src/app
WORKDIR /src/app
COPY package.json /src/app/package.json
RUN npm install
COPY . /src/app
EXPOSE 3000
CMD [ "npm", "start" ]
Based on your Dockerfile, start building the Podman image.
$ podman build -t app-image .
...
...
$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/node 10-alpine a07f309c9753 2 weeks ago 88.2MB
docker.io/library/app-image latest dd076d7c059b 13 seconds ago 92.3MB
Step 3: Run Podman container
Run the application with below command (-d
: run container in detached mode,
--name
: specify a name for container, -p
: specify exposed port mapping).
$ podman run -d --name app-container -p 3000:3000 app-image
e987635ba2a40cfc1054269c9e23be2591084b2f6831b88b15e21a821e3c40d0
$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e987635ba2a4 localhost/app-image:latest npm start 13 seconds ago Up 13 seconds ago 0.0.0.0:3000->3000/tcp app-container
$ curl localhost:3000
Hello World!
Leave a comment