preface

What is yAPI?

YApi is a locally deployable, front-end and QA-enabled, visual interface management platform called Yapi.ymfe.org

The article will intersperse some relevant knowledge points, which can save you time to climb out of the pit.

From the idea of custom construction, optimization, to achieve the posture of interested partners go down ~~~

  • yapi(Docker Hub): crper/yapi

  • Making address:yapi-docker

rendering

The login

Login successful

The project area

Yapi container

Bash has been built in as the default shell, and Vim has configured some common Settings

  • Vim’s default

Lead based

Knowledge reserves

Docker/Linux/Node foundation, such as Linux and Docker common commands,shell preparation and so on

Building the infrastructure

  • Docker version 18.03.1 - ce
    • Based on thealpine ,alpineIt’s a very lightweight oneLinux, the raw version only has 5M
  • Docker Compose(frompip3Default version installed)

Build goals: upgradeable, database independent, first build is to pull the latest version!!!!

Practical science

If Docker Hub is used for automatic construction, since it is built on a foreign server, there is no problem of slow construction.

The following is limited to your local build

It is well known that foreign resources are relatively slow, so we build the preference to choose the domestic supply

Docker China

Linux image source with the hkUST source

alpineList of warehouses, official and query-enabled

Sync Github’s code cloud every other day

Minimize configuration as much as possible, so don’t configure anything personalized, like OH my ZSH, Neovim

In this article, you can learn about simple docker deployment, basic Dockerfile writing, and how to publish your own custom containers

I provide yAPI image go automated build, so internal dependence or international source, not local packaging, there will be no slow

So if you want to pull, you just have to think about the docker pull source

Build yAPI normally

The one I chose here is based on Alpine, and there are many poses,

You can start from an empty container or someone else’s packed Node container

Mirror functions should be kept as simple as possible to facilitate choreography,

If a single image provides multiple services, it can be cumbersome to maintain.

There are many things to consider, especially when updating or suspending some services

Version 1: The rules

Dockfile

# Build based on alpine image
FROM alpine:3.8
# Mirror maintainer information
LABEL MAINTAINER = '[email protected](https://github.com/crper)'
# Build the base environment
# - Replace domestic source, speed bar
# - Update source
# - Install the base environment pack
# - Change the default shell for the user, since the container is only for yAPI, so there is no such thing as creating user groups and individual users, so there is only root
If the container contains multiple functions, it is better to use user groups (not recommended), and keep the container functionality as simple as possible
# - Finally, remove some caches
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
  && apk update \
  && apk add --no-cache shadow git nodejs nodejs-current-npm bash vim tar curl python python-dev py-pip gcc libcurl make\
  && usermod -s /bin/bash root \
  && rm -rf /var/cache/apk/*
Clone the project and initialize the project
# yapi official network deployment tutorial: https://yapi.ymfe.org/devops/index.html
# install-server initializes database index and administrator account. Administrator account name can be configured in config.json
RUN npm i -g node-gyp --registry https://registry.npm.taobao.org \
  && npm install -g yapi-cli --registry https://registry.npm.taobao.org \
  && mkdir /yapi && cd /yapi \
  && git clone https://github.com/YMFE/yapi.git vendors  \
  && cd vendors \
  && npm install --production --registry https://registry.npm.taobao.org
# Working directory
WORKDIR /yapi/vendors
Configure the yAPI configuration file
COPY config.json /yapi/
Copy the execution script to the container's execution directory
COPY entrypoint.sh /usr/local/bin/

# Exposed port
EXPOSE 3000

# Specify the configuration file
ENTRYPOINT ["entrypoint.sh"]



Copy the code

entrypoint.sh


#! /bin/sh
# yAPI is initialized with an init.lock file
lockPath="/yapi/init.lock"

If the initialization file exists, run it directly, initialize it otherwise
if[!-f "$lockPath" ]; then
  node /yapi/vendors/server/install.js
else
  node /yapi/vendors/server/app.js
fi

Copy the code

.dockerignore

This file is a nice thing, similar to.gitignore, which is designed to ignore submissions so we don’t have to load our image with unnecessary stuff

.git/
node_modules/
Copy the code

Local packaging image only after the discovery, although it can be used, but there are two problems exposed (volume, build speed)

So LET me see if I can optimize,

Version 2: Reduced mirror volume, reduced build time

Choose a good parent container, one is to reduce the number of build layers, and one is to reduce dependency packages

The first step can’t be changed, although node-Alpine is also available, so start with the last two

Dockfile


# Build based on alpine image
FROM alpine:3.8
# Mirror maintainer information
LABEL MAINTAINER = '[email protected](https://github.com/crper)'
# Build the base environment
# - Replace domestic source, speed bar
# - Update source
# - Install the base environment pack
# - Change the default shell for the user, since the container is only for yAPI, so there is no such thing as creating user groups and individual users, so there is only root
If the container contains multiple functions, it is better to use user groups (not recommended), and keep the container functionality as simple as possible
# - Finally, remove some caches
# - Clone project
#!!!!! Yapi official network deployment tutorial: https://yapi.ymfe.org/devops/index.html
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
  && apk update \
  && apk add --no-cache shadow git nodejs nodejs-current-npm bash vim tar curl python python-dev py-pip gcc libcurl make\
  && usermod -s /bin/bash root \
  && rm -rf /var/cache/apk/* \
  && mkdir /yapi && cd /yapi && git clone https://gitee.com/mirrors/YApi.git vendors
# Working directory
WORKDIR /yapi/vendors
Configure the yAPI configuration file
COPY config.json /yapi/
Copy the execution script to the container's execution directory
COPY entrypoint.sh /usr/local/bin/
Copy the vim configuration file
COPY .vimrc /root/
# Exposed port
EXPOSE 3000

# Specify the configuration file
ENTRYPOINT ["entrypoint.sh"]


# 'shadow' : 'alpine' does not integrate 'usermod' by default, so this extra package is needed because it is used to change the default 'shell'
# 'vim' : Edit magic
# 'tar' : Decompress
# 'make' : compile dependent
# 'GCC' : GNU compiler suite
# 'python' : 'python python-dev py-pip' these packages include the basic development environment
# 'curl' is a command line tool that tests links and downloads content
# 'git' : Needless to say
# `nodejs` : node
# 'nodejs-current-npm' : 'Alpine' Linux version relies on this version to be recognized by 'NPM'

Copy the code

entrypoint.sh

#! /bin/sh
# yAPI is initialized with an init.lock file
lockPath="/yapi/init.lock"

# Set source to Taobao source
npm config set registry http://registry.npm.taobao.org/;

# Enter the YAPI project
cd /yapi/vendors


If the initialization file exists, run it directly, initialize it otherwise
if[!-f "$lockPath" ]; then
  Install the cli to update yAPI
  npm i -g node-gyp yapi-cli;
  Install the initialized dependency module
  npm i --production;
  # start Yapi initialization
  node server/install.js
else
  node server/app.js
fi
Copy the code

From more than 500 M image reduced to more than 400, 20% is still considerable, can you optimize it again!!

Vim,tar,bash,shadow,py-pip are all removed, everything else is required by the build (yAPI initialization relies on Python, etc.)

emm….. By removing all of this, we can reduce the build time by more than 40 M

Version 3: Reduces the probability of initialization failure

With dockerHub’s automated build, NPM directly selects the official source at build time

Dockerfile

# Build based on alpine image
FROM alpine:latest
# Mirror maintainer information
LABEL MAINTAINER = '[email protected](https://github.com/crper)'
# Build the base environment
# - Update source
# - Install the base environment pack
# - No need to change the default shell, just specify the shell when entering the image
# - Finally, remove some caches
# - Clone project
# - Using an automated build without considering the domestic NPM source can reduce the probability of initialization failure
#!!!!! Yapi official network deployment tutorial: https://yapi.ymfe.org/devops/index.html
RUN apk update \
  && apk add --no-cache  git nodejs nodejs-current-npm bash vim  python python-dev gcc libcurl make\
  && rm -rf /var/cache/apk/* \
  && mkdir /yapi && cd /yapi && git clone https://github.com/YMFE/yapi.git vendors \
  && npm i -g node-gyp yapi-cli \
  && cd /yapi/vendors && npm i --production;
# Working directory
WORKDIR /yapi/vendors
Configure the yAPI configuration file
COPY config.json /yapi/
Copy the execution script to the container's execution directory
COPY entrypoint.sh /usr/local/bin/
Copy the vim configuration file
COPY .vimrc /root/
# Exposed port
EXPOSE 3000

# Specify the configuration file
ENTRYPOINT ["entrypoint.sh"]


# 'vim' : Edit magic
# 'tar' : Decompress
# 'make' : compile dependent
# 'GCC' : GNU compiler suite
# 'python' : 'python python-dev py-pip' these packages include the basic development environment
# 'curl' is a command line tool that tests links and downloads content
# 'git' : Needless to say
# `nodejs` : node
# 'nodejs-current-npm' : 'Alpine' Linux version relies on this version to be recognized by 'NPM'

Copy the code

entrypoint.sh

#! /bin/sh
# yAPI is initialized with an init.lock file
lockPath="/yapi/init.lock"



# Enter the YAPI project
cd /yapi/vendors


If the initialization file exists, run it directly, initialize it otherwise
if[!-f "$lockPath" ]; then
  # start Yapi initialization
  node server/install.js
else
  Run the yAPI management system
  node server/app.js
fi
Copy the code

Packaging image

Format: Docker build [option] tagName path

docker build -t yapi .;

The latest version number is not included by default

Build an image based on a Dockfile in the current directory

You can also build your own maintenance version numbers, for example

Docker build-t yAPI :0.0.1

To compress the image in gz format, enter –compress

Release the mirror

Normal terminal manual publishing

Login account

The account here is the official registered account of Docker, and the overall process is similar to Git

  • Open terminal ->docker login

  • commit: Submit your own written or customized image

Docker Commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] [flags]

  • push: Pushes an image to a remote locationdocker hubAh, wrong report?

We don’t have permission. Why is this problem?

The submission specification of Docker Hub requires us to start with our own user name, which can be changed

The mirror volume of version one

Version 2 mirror volume

The version of COMPRESS I submitted so you see in docker Hub is only this big

Once you’re done, your work is available on Dock Hub

You can now build directly from locally built images,

If you’re doing a secondary build based on your own package, you don’t need to commit, just push

Automated build release

Docker Hub provides automated builds, which are simply automated builds,

We can build with github repositories,bitbucket can build with github repositories, but I don’t use that;

Both from here and from user management, you need to bind Github in advance.

With authorization, you can read your warehouse list. Select a warehouse to build, the requirements of the warehouse, the basic directory is as follows

├─.dockerignore // Git file ├─ Dockerfile // Build Configuration File ├─ readme.md // Don't say too much ├─ Json // yapi config file ├ ─ entrypoint.sh // setup entry scriptCopy the code

Initializers can set which branches trigger builds, or trigger an endpoint to build, most foolishly checking on the push event to automatically build

If you want to upload the image to aliyun, DAO and so on in China,

Some require signing up for a developer account and submitting according to their documentation requirements

Mirror deployment

Now follow me to deploy your image and initialize it;

The deployment of yapi

The first initialization pulls the latest version by default, so you don’t need to specify the version,

If the yAPI code is not strict, even the new version initialization will report an error, there is no solution!

Create a volume

  • docker volume create yapi-mongo

Create a storage volume to store the mongodb data used by YAPI

Why do you want to separate, this is for the sake of future upgrade, database retention, as long as the startup associated with the line

Start the mongo

  • docker run -d --name yapi-mongo -v yapi-mongo:/data/db mongo

Why start mongodb first when yAPI relies on mongodb for initialization, such as creating user tables

What does this order mean?


-d--name: sets a name for the container for easy control, such as start and stop -v: specifies the associated volume. => Local volume: specifies the storage location in the container where mapping data is storedCopy the code

If you need to manage the database externally, it is best to expose the port as well, and the mongodb container also exposes port 27017 by default

  • docker run -d --name yapi-mongo -v yapi-mongo:/data/db -p 27017:27017 mongo

Initialize Yapi and start Yapi

  • Initialize yapi

docker run -d --name yapi -p 3000:3000 --link yapi-mongo crper/yapi

One more parameter is –link, an outdated command used to enable communication between containers, which is no longer recommended

  • Start the yapi

docker restart yapi

Procedures can use the Docker Logs Details container ID or name to see what is going on inside

This is the shell execution process, such as this project can see the initialization of the account password (success).

Whether it’s Mongo or Crper/YAPI, when you request a container that doesn’t exist,

The latest version of the image will be pulled from the dockhub by default

The following is the basic initialization information

Json => adminAccount Value of this field Password: ymfe.orgCopy the code

—– and the error that can occur is that NPM hangs ——

At initialization time, execute

Docker logs -- Details Container ID

Looking at the execution process of the internal terminal, some sources of NPM may not be reliable,

If the NPM installation error is reported, you need to go to another source

Start the Crper/yAPI image and follow the tutorial

// npm config set registry [url]
// npm ---- https://registry.npmjs.org/
// cnpm --- http://r.cnpmjs.org/
// taobao - http://registry.npm.taobao.org/
// eu ----- http://registry.npmjs.eu/
// au ----- http://registry.npmjs.org.au/
// sl ----- http://npm.strongloop.com/
// nj ----- https://registry.nodejitsu.com/


// Enter vendors directory
// If there is node_modules,
// We should all kill node_modules first
// It will be cleaner to reinstall dependencies

Vendors directory, such as setting back to the official sources
npm config set registry https://registry.npmjs.org/;

// Install the global upgrade tool and the dependency compiled NPM module
npm i -g node-gyp yapi-cli \
npm i --production;

// Initialize yAPI
node server/install.js

Copy the code

Once the dependency installation is complete, you can reinitialize it and restart the container

Container entry operation

  • docker psFrom this you can see a list of containers to run your image
  • Docker exec -it container ID bashThis sentence is non-invasively entered into the container and calledshellforbashtheexitWon’t kill the container

Docker attach command should be used with caution. The container will be stopped when the terminal exits. This command can be used depending on the situation!!!!

Upgrade yapi

Because there is no container handling involved.. It’s just a simple file replacement. The official solution is provided, and the CLI is integrated into the container by default

// https://yapi.ymfe.org/devops/index.htmlCD {project directory} yapi ls// Check the version number list
yapi update // Upgrade to the latest version
yapi update -v v11.. 0 // Upgrade to the specified version
Copy the code

After the upgrade, restart the Node program or restart the container.

Making address:yapi-docker

GUI management database

We exposed port 27017, so our host can use tools to link inside the database,

Radish green vegetables to each his love, renderings

The same goes for liking the command line

Error summary

Some errors occurred during the build yAPI process

  • /bin/sh: npm: not found, install at build timenodejs-current-npm
  • usermod not found: Install at build timeshadow
  • gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
    • This is initializationyapiIf you do, you need to complete itpythonThe basic environment, build time to join the relevant installation package
  • mongodbNo access is available when you configure file Settings127.0.0.1The time..
    • indockerBy default, the container name maps the access IP of the container, soconfig.jsonMust be specified as the container name of Mongo (this pit wasted the thief much time, foreign communities are searched over, basically all say what--networkThese)

There are also some mistakes I forgot to include in the screenshots

———— Tips ————

Why do you see dockerfile with a lot of \ to link commands,

That’s because RUN builds an image once and then passes it on to the following second choreography,

If a lot of RUN is used, you’ll have to build a lot of layers from start to finish (and get bigger)… The official recommendation is no more than 7 floors!!

Build layers can currently be no more than 127!

For –link containers, this is officially deprecated as an outdated feature. The new network mode is robust.

Bridge, host, and subnet modes are provided, but these do not apply to –link combinations

Docker-compose is composed for multiple container configurations, which are easy to configure and maintain, such as the latest version 3.6

Portal: docs.docker.com/v17.09/comp…

conclusion

It took quite a bit of time to write the various screenshots of this article, and to reproduce the process (modifying files, packaging, running, debugging).

Why is there this tutorial? I feel it can help a lot of people who want to try docker.

The so-called “micro-service” is realized based on Docker to keep the function of the container simple and convenient for maintenance and testing

I would have liked to have continued writing docker-compose based versions, which would have been too long… Write another one sometime

Docker-compose deployments are written elegantly, configuration is straightforward, and can do complex container choreography…

Someone will definitely mention Kubernetes, this product is very popular, interested can have a look, generally do large factory operation and maintenance will use this product

Please leave a message if there is any mistake or improvement plan, we will revise and improve it in time. Thanks for reading ~~~