Building Images

Build your own image

Let's write a simple Node.js app, add a index.js:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, world!\n');
});

server.listen(3000, '0.0.0.0', () => {
  console.log('Server started');
});

A Dockerfile is a text file describes how an image is created. Following Dockerfile creates a Node.js container, copies your project files, installs dependencies, and starts your application on port 3000.

Add a Dockerfile:

FROM node:22

WORKDIR /usr/src/app

COPY index.js ./

EXPOSE 3000

CMD ["node", "index.js"]

Build image:

docker build -t my-app .

Run container from the image

docker run -p 3000:3000 -d --name app my-app

Verify it works:

curl localhost:3000

Remove the container:

docker rm -f app