[Docker] Dockerfile and .dockerignore


Dockerfile

In the following example, we create a web application ,use Dockerfile to create a image, and run the application in a container.
  1. In a new folder, create a node.js express server in index.js

     const express = require('express');
     const app = express();
     const port = 3000
    
     app.get('/', (req, res) => res.json([{
         'name': 'Knut',
         'email' : 'knut@gmail.com'
     },
     {
         'name': 'Alex',
         'email': 'alex@gmail.com'
     },
     {
         'name': 'Green',
         'email': 'green@gmail.com'
     },
     {
         'name': 'Maria',
         'email': 'maria@gmail.com'
     }]));
    
     app.listen(port, () => console.log(`Example app listening on port ${port}!`))
    
  2. Set up node using npm
    1. npm init
    2. npm install --save express
    3. run the server by npm index.js
    4. you can see the website port at localhost:3000
  1. Create Dockerfile

     FROM node:latest    # getting the source images
     WORKDIR /app        # set the working directory
     ADD . .             # add all files into the working path
     RUN npm install     # run the npm install
     CMD node index.js   # when start the container, this command will be executed
    
  2. Using cache

    • Because we don't change the package*.json files every time,
      if we add them before ADD . . , we will be using cache, which will save us a lot of time.
    • So we add a line to ADD all package*.json files into the pwd directory, and run the npm install command, if there's no any changes, we would be using cache, preventing from the unnecessary process.
      FROM node:latest # getting the source images
      WORKDIR /app # set the working directory
      ADD package*.json ./ # add all package*.json files to the pwd directory
      RUN npm install # run the npm install
      ADD . . # add all files into the working path
      CMD node index.js # when start the container, this command will be executed
      
  3. Build image by Dockerfile

     docker build -t user-service-api:latest .
    
    1. --tag could be shortened as -t, user-service-api is name of the image you want to build, and :latest after : is the tag.
    2. . means the location of Dockerfile.
  4. Run and start container

     docker run --name user-api -d -p 3000:3000 user-service-api:latest
    
    1. run run a command in a new container.
    2. --name user-api set the name of the container
    3. -d detached mode
    4. -p because we know the node.js express server which we create is listening on port 3000, so we map our 3000(former one, the port of the host) to 3000(latter one, the port of the container we run)
    5. user-service-api:latest the name of the image we want to use.

dockerignore

  1. In the pwd directory, we create a file named .dockerignore, which lists files or directories we want to ignore when using the Dockerfile.
     node_modules
     Dockerfile
     .git
     .gitignore
    
#docker






你可能感興趣的文章

Stapler Walkthrough (1)

Stapler Walkthrough (1)

從 Hoisting 理解底層運作機制

從 Hoisting 理解底層運作機制

在 AWS 上使用 Docker 安裝 Jitsi

在 AWS 上使用 Docker 安裝 Jitsi






留言討論