DEV Community

Cover image for Building and Pushing a Custom Docker Image to Docker Hub
Adekunle Osatuyi
Adekunle Osatuyi

Posted on

Building and Pushing a Custom Docker Image to Docker Hub

Tools: Docker Desktop, VScode

I am creating a simple Node.js web app and containerize it.

Step 1 – Create a Project Directory**
mkdir docker-lab
cd docker-lab

Step 2 – Create app.js**
nano app.js code:

const http = require('http');
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.end('Hello, Docker World. Welcome to Lubemart Docker-Space!\n');
});

server.listen(port, () => {
console.log(Server running at http://localhost:${port}/);
});

Save (Ctrl + S).

Step 3 – Create package.json**
nano package.json code:

{
"name": "docker-lab",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js"
}
}

Save (Ctrl + S).

Step 4 – Create the Dockerfile**
nano Dockerfile code:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY .
EXPOSE 3000
CMD ["npm", "start"]

Save (Ctrl + S).

Step 5 – Build the Image (Using Terminal Console)**
docker build -t docker-lab:2.0 .

This command does the following;
Reads the Dockerfile.
Downloads Node.js base image.
Installs dependencies.
Creates an image named docker-lab with version 2.0

Step 6 – Run the Container**
docker run -d -p 3000:3000 --name lubemart-app docker-lab:2.0

Step 7 - Result**
http://localhost:3000
Hello, Docker World. Welcome to Lubemart Docker-Space!

Output

Step 8 -Tagging Image and Pushing to Docker Hub**
docker tag docker-lab:2.0 shakol090/docker-lab:2.0
docker images

docker push shakol090/docker-lab:2.0

Top comments (0)