preface

The previous article explained how to make a mirror (juejin.cn/post/684490…) , let’s take a koA-based photo uploading service as an example.

Prepare the image upload code

const Koa = require('koa')
const fs = require('fs')
const app = new Koa()
const koaBody = require('koa-body') // Plugins that parse uploaded files
// These two variables are read from environment variables
const { MAX_SIZE, FILE_TYPE } = require('./config')

const Path = require('path')
const serve = require('koa-static')
app.use(serve(Path.join(__dirname, '/pic/')))

app.use(koaBody({
  multipart: true.formidable: {
    maxFileSize: MAX_SIZE * 1024 * 1024    // Set the maximum size of a file to be uploaded. The default size is 2 MB}}))// Response processing
app.use(async ctx => {
  let { files, method, path } = ctx.request; // Get the uploaded file
  if(method ! = ='POST'|| path ! = ='/upload') return ctx.throw(404.'Not found')
  if(! files)return ctx.throw(400.'Please send pictures')
  // Verify the image type
  if(! FILE_TYPE.split(', ').includes(files['image'].type)) return ctx.throw(400.'Picture type error')
  // Create a readable stream
  const reader = fs.createReadStream(files['image'] ['path'])
  let filePath = Path.join(__dirname, `./pic/${files['image'] ['name']}`)
  let remotefilePath = ` /${files['image'] ['name']}`
  // Create a writable stream
  const upStream = fs.createWriteStream(filePath);
  // The readable stream writes to the writable stream through the pipe
  reader.pipe(upStream);
  return ctx.body = {
    url: remotefilePath,
    message: "File uploaded successfully".cc: 0
  }
})
app.listen(3000)

Copy the code

Configuration file (config.js):

// Both parameters are read from environment variables
// Image size limit
const MAX_SIZE = process.env.MAX_SIZE || 2
// Image type restriction
const FILE_TYPE = process.env.FILE_TYPE || 'image/jpeg,image/png'

module.exports = {
  MAX_SIZE,
  FILE_TYPE
}
Copy the code

Project address: github.com/Vongolatt/i… .

Dockfile

# KOA photo upload
# Version 1.0

# Base image
FROM node:current-alpine

# Maintainer information
LABEL name="vongola" 

#ENV Sets the environment variable
ENV MAX_SIZE=4 FILE_TYPE='image/jpeg,image/png'

#WORKDIR is equivalent to CD. When specified, the following run command will be executed in the current file path
WORKDIR /home

#RUN RUN the following command
RUN wget https://github.com/Vongolatt/image-upload/archive/master.zip
RUN unzip master.zip \
&& cd image-upload-master/ \
&& mkdir pic \
&& npm install --production
ENTRYPOINT [ "node"."/home/image-upload-master/app.js" ]
#EXPOSE port
EXPOSE 3000
Copy the code

Here we are based on the Alpine version of Node, which is much smaller than the other versions, but has limited functionality and may be buggy, so be careful when using it. Then set the default values for the image size and image type, pull the project (using the wget command, you don’t need to install Git, it’s a trick), and finally launch the command at the entrance and expose port 3000.

Build Dockerfile

Execute the following command in the directory where the Dockerfile resides (note the last dot) :

docker build –no-cache -t image_upload:v1 .





Start the container

docker run -itd -p 3000:3000 –rm –name upload -v /Users/vongola/Documents/Image/:/home/image-upload-master/pic  image_upload:v1

Here the container starts and maps the image save path, please replace the red part with your path, and no environment variables are passed, so the default is the environment variable when building the image.

Test container





Passing environment variables

Earlier we started the container without passing environment variables. Now we test the passing of environment variables. Close the container first (since the –rm argument was used to start the container, it will be deleted directly after closing the container) :

docker stop upload

Passing environment variables

docker run -itd -p 3000:3000 –rm –name upload -e FILE_TYPE=image/jpeg -v /Users/vongola/Documents/Image/:/home/image-upload-master/pic  image_upload:v1

FILE_TYPE=image/jpeg

conclusion

This completes a very simple application Docker wrapper, but there are a lot of issues that haven’t been considered. Such as:

  1. At the code level, the problem of uploading the same name is not considered.
  2. The git command is also not used when pulling git projects, which is sufficient for normal projects, but for private projects, git configuration needs to be considered.