This web application is supposed to display information such as: IP address of the server on which the application is running,
Server name (hostname) and Application version (in any scheme) – the application version is to be specified in the docker –build-arg by giving value
the VERSION variable defined by the ARG statements. I using simple index.js. The result of the application is that the js code itself appears on the website.
I used commands like:
docker build --build-arg VERSION=2 -f Dockerfile1 -t local/base:v1 .
docker run -d -p 8080:80 --name web1 local/base:v1
curl http://localhost:8080
and it shows that container is healthy
FROM node AS build1
# Set the working directory in the image
WORKDIR /usr/app
# Copy application files
COPY . .
# Install npm dependencies
RUN npm install
# Set the environment variable for application version
ARG VERSION
ENV APP_VER=production.${VERSION:-v1.0}
# Stage 2: Utilize the Nginx base image
FROM nginx:latest
# Copy the Nginx configuration file handling JavaScript files
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy the built application from the first stage to the Nginx image
COPY --from=build1 /usr/app /usr/share/nginx/html
# Set the working directory
WORKDIR /usr/share/nginx/html
# Set the port on which the HTTP server will run
EXPOSE 80
# Set the default page
CMD ["nginx", "-g", "daemon off;"]
# Health check: check if the server is available at localhost:80
HEALTHCHECK --interval=30s --timeout=5s
CMD curl --silent --fail localhost:80 || exit 1
const os = require('os');
const app = express();
const port = 8080;
// Define the main path ("/")
app.get('/', (req, res) => {
// Construct the response with server IP address, hostname, and application version
let response = `Server IP Address: ${getIPAddress()}n`;
response += `Server Name (hostname): ${os.hostname()}n`;
response += `Application Version: ${process.env.VERSION}n`;
// Send the response as HTML
res.send(response);
});
// Serve HTML file
app.use(express.static('public'));
// Listen on port and log a message upon server start
app.listen(port, () => {
console.log(`The application is available on port ${port}`);
});
// Function to get the server IP address
function getIPAddress() {
// Get network interfaces
const interfaces = os.networkInterfaces();
for (const dev in interfaces) {
// Filter IPv4 addresses, excluding internal addresses
const iface = interfaces[dev].filter(details => details.family === 'IPv4' && !details.internal);
// Return the first found address
if (iface.length > 0) return iface[0].address;
}
// Return default value
return '0.0.0.0';
}
# Listen on this port as default
listen 80 default_server;
# Listen for ipv6
listen [::]:80 default_server;
# Serve static files
root /usr/share/nginx/html;
index index.js;
location / {
# If request does not match any file
try_files $uri $uri/ =404;
}
}