LoginSignup
0
0

Docker -- Containerize an application (Part 1)

Posted at

1, Containerize an application

First, let's create an application using Express.

terminal
    npm init -y
    npm i express
    touch server.js

image.png

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(`Example app listening on port ${port}`)
    })

Run the Express.

terminal
    node server.js

image.png

Second, let's create a Dockerfile and a .dockerignore.

terminal
    touch Dockerfile
    touch .dockerignore
Dockerfile
    FROM node:lts-alpine
    
    WORKDIR /app
    
    COPY . /app/
    
    RUN npm install
    
    CMD [ "node", "server.js"]
    
    EXPOSE 3000
.dockerignore
   node_modules 

Build the image using the following commands

terminal
    docker build -t node-image .

Created a new image

image.png

Start an app container

You can run the application in a container using the "docker run" command and specify the name of the image you just created.

terminal
    docker run -d -p 3000:3000 --name node-app node-image

The "-d" flag runs the container in the background.
The "-p" flag creates a port mapping between the host and the container.
The "--name" flag creates a container name.

After a few seconds, open your web browser to http://localhost:3000. You should see your app.

image.png

image.png

Run the following "docker ps" command in a terminal to list your containers.

terminal
    docker ps

image.png

2, Update the application

Stop the application

terminal
    docker stop node-app

Delete the node-app container

terminal
    docker rm node-app

Update the server.js

server.js
    const express = require("express");
    const app = express();
    const port = 3000;
    
    app.get("/", (req, res) => {
      res.send("This is Updated!");
    });
    
    app.listen(port, () => {
      console.log(`Example app listening on port ${port}`);
    });

Build the image using the following commands again.

terminal
    docker build -t node-image .

Start an app container again.

terminal
    docker run -d -p 3000:3000 --name node-app node-image

After a few seconds, open your web browser to http://localhost:3000. You should see your app.

image.png

3, Push the image to your Docker Hub.

First, you need to login your Docker Hub

terminal
    docker login

image.png

Use the "docker tag" command to give the "node-image" image a new name.

terminal
    docker tag node-image [YOUR-USER-NAME]/node-image

Run the "docker push" command that you see on Docker Hub.

terminal
    docker push [YOUR-USER-NAME]/node-app

Now that your image has been built and pushed into a registry, try running your app on a brand new instance that has never seen this container image.

image.png

Reference

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0