Docker -- Containerize an application (Part 1)

# 1, Containerize an application

### First, let's create an application using Express.

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/876fe7dc-9c3c-8991-0ec4-51b871315a4d.png)

```javascript: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:terminal
    node server.js
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/90ca1955-72ac-806f-acff-7ec2b720fad5.png)

### Second, let's create a Dockerfile and a .dockerignore.
```terminal:terminal
    touch Dockerfile
    touch .dockerignore
```

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

```.dockerignore:.dockerignore
   node_modules 
```

### Build the image using the following commands
```terminal:terminal
    docker build -t node-image .
```

### Created a new image

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/f76ad21b-33a7-207f-99c4-53191b8b8397.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: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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/58d48659-66c1-b754-6e91-e193adf76858.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/0127b1bf-07f9-da47-445d-981e3e81ca9c.png)

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

```terminal:terminal
    docker ps
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/ade7a486-2645-0129-9318-3f59c594b3c4.png)

# 2, Update the application

### Stop the application
```terminal:terminal
    docker stop node-app
```

### Delete the node-app container
```terminal:terminal
    docker rm node-app
```

### Update the server.js
```javascript: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:terminal
    docker build -t node-image .
```

### Start an app container again.

```terminal: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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/76ab18e3-39ea-51cc-9fd4-c85aeeb72761.png)

# 3, Push the image to your Docker Hub.

### First, you need to login your Docker Hub

```terminal:terminal
    docker login
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/2fb26a9d-a545-81da-c02a-6142280db1d1.png)

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

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

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

```terminal: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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654025/dc7b3ac3-fb21-def2-2b26-a924447e6978.png)

# Reference

https://docs.docker.com/get-started/