Docker

The installation

The local installation

  • Docs.docker.com/docker-for-…
  • Docker version Indicates the version

Test machine installation

  • curl -fsSL get.docker.com -o get-docker.sh

  • sh get-docker.sh

The mirror command

  • docker images
  • # docker pull nginx
  • Docker RMI image ID
  • Docker build name. # Based on Dockerfile
  • Docker image prune # clear image Id

Container order

  • docker ps -a
  • Docker start Container ID
  • Docker stop Container ID
  • Docker RM Container ID
  • Docker logs container ID
  • Docker inspect Container ID
  • Docker exec -it container ID # enter the console

Container operation

D * docker run - p - 81-80 - v/Users/wangJK/Desktop/blog/code/HTML: / usr/share/nginx/HTML - name nginx - test nginx * - p port mapping * -d background run * -v file mapping: Nginx is a container that has a piece of index.html pointing to port 80, host port 81 maps to port 80, and files map to index.html somewhere new, so that the container itself changes from a container to a service * --name container nameCopy the code

Dockerfile

  • A simple configuration file that describes how to build a new image
  • The file name must be Dockerfile and must be in the root directory of the project

The node server example

  FROM node:14
  LABEL [email protected]
  WORKDIR /app  Create a working directory
  COPY.
  # Set time zone
  RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
  # installation
  RUN npm set registry https://registry.npm.taobao.org
  RUN npm i
  Start blocking console, otherwise the docker will not continue
  CMD echo $SERVER_NAME && echo $AUTHOR_NAME && npm run pro-dev && npx pm2 log 

  # Environment variables
  ENV SERVER_NAME="biz-editor-server"
  ENV AUTHOR_NAME="shuangyue"
Copy the code

Web sample

  # build stage
  FROM node:12-alpine as build-stage
  LABEL [email protected]
  Create a working directory
  WORKDIR /app
  COPY.
  RUN npm install cnpm -g --no-progress --registry=https://registry.npm.taobao.org
  RUN cnpm install --no-progress
  RUN npm run build
  # production stage
  FROM nginx:stable-alpine as production-stage
  COPY --from=build-stage /app/dist /usr/share/nginx/html
  EXPOSE 80
  CMD ["nginx"."-g"."daemon off;"]
Copy the code

Build the mirror

  • docker build server-admin .
  • docker build web-web .

Create a container based on the image you built and run it

  • docker run -p 8081:3000 –name server-test server-admin
  • docker run -p 8082:80 –name web-test web-web

docker-compose

The installation

The local installation

  • Docker can be used directly after local installation
  • Docker-compose version # view docker-compose version

Test machine installation

  • curl -L Get. Daocloud. IO/docker/comp… -s-uname -m` > /usr/local/bin/docker-compose
  • sudo chmod +x /usr/local/bin/docker-compose

background

  • Docker is just a container docker-compose can be understood as a composite container
  • For example, we run the above server-admin container, it will rely on other services such as Mongo, mysql, Redis, etc., if it is using docker, it does not need to run one by one, then can there be a solution, put them together into a package

Container order

  • docker-compose ps
  • docker-compose up -d
  • docker-compose down

Build the mirror

  • Docker-compose build name # build

docker-compose.yml

* Configuration fileCopy the code

The sample

version: '3' services: editor-server: # service name build: context: . dockerfile: Dockerfile image: editor-server container_name: editor-server ports: - 8081:3000 editor-redis: image: redis container_name: Editor-redis ports: -6378:6379 environment: -tz =Asia/Shanghai # Editor-mongo: image: mongo container_name: Editor - mongo restart: always volumes: - 'the docker - volumes/mongo/data: / data/db' # data persistence environment: -mongo_initdb_database =myblob # database -tz =Asia/Shanghai ports: - '27016:27017' Editor -mysql: image: MONGO_INITDB_DATABASE=myblob # database -tz =Asia/Shanghai ports: - '27016:27017' Editor -mysql: image: Mysql container_name: editor-mysql restart: always # privileged: restart privileged: true --default-authentication-plugin=mysql_native_password You can use 127.0.0.1:3305 to connect to your database. Volumes: - the docker - volumes/mysql/log: / var/log/mysql # log - docker - volumes/mysql/data: / var/lib/mysql data persistence - # /mysql/init:/docker-entrypoint-initdb.d/ # -mysql_database =koa2_weibo_db # Create database when initialize container -mysql_root_password = WJK ******* -tz =Asia/Shanghai # Set time zoneCopy the code
Docker-compose build editor-serverCopy the code

Release test machine

Create a work account

Add work permission

  • whereis sudoers
  • chmod u+w /etc/sudoers
  • vim /etc/sudooers
    • Find root ALL=(ALL) ALL
    • Add work ALL=(ALL) ALL
  • chmod u-w /etc/sudoers
  • exit
  • Log in to the test machine using work, drive into su, and enter the password of root account to have [super permission].

Login to trust

*** touch ~/. SSH /authorized_key # Create file * chmod 700 ~/. SSH # modify folder permissions * chmod 600 ~/.ssh/authorizes_key # Paste the contents of the local id_rsa.pub into * exit * Now you can use secret free loginCopy the code

Log in to the test machine using Work to install Git

  • yum install git
  • git –version

The test machine was deployed with GitHub ActiOSN

  • .github/workflows/deploy-dev.yml
name: test

on:
    push:
        branches:
            - master
        paths:
            - '.github/workflows/**'
            - '__test__/**'
            - 'src/**'
            - 'Dockerfile'
            - 'docker-compose.yml'
            - 'bin/*'


jobs:
    deploy-test:
        runs-on: ubuntu-latest In what operating environment is # used

        steps:
            - uses: actions/checkout@v2 # Third-party Actios pulling code down is equivalent to Git pull
            - name: set ssh key # temporarily set SSH key to login trust
              run: | mkdir -p ~ /. SSH / # to the private key to the run environment ubuntu - latest echo "${{secrets. WFP_ID_RSA}}" > ~ /. SSH/id_rsa # secret configuration here https://github.com/imooc-lego/biz-editor-server/settings/secrets chmod 600 ~/.ssh/id_rsa # add a permission ssh-keyscan "47.94. *. * * * *" > > ~ /. SSH/known_hosts            - name: deploy # deployment
              run: | SSH [email protected]. * *. * * "# limit from this three-step test machine own work now, this is equivalent to test machine with our warehouse source code # 【 note 】 in the work to login, Manually create/home/work/WJK - lego master catalog # then git clone https://username:[email protected]/wjk-lego/biz-editor-server.git - b Remember to delete Origin, otherwise it will expose the Github password
                    The above three steps are the foundation, after which we will deploy to the test machine every time we submit code to master
                    cd /home/work/wjk-lego/github-action-test;
                    git remote add origin https://${{secrets.WFP_USERNAME}}:${{ secrets.WWFP.PASSWORD}}@github.com/${{secrets.WFP_USERNAME}}/github-action-test.git;
                    # git fetch;
                    git checkout master; If it is not the branch you want, open comments to switch branches
                    git pull origin master; # Re-download the latest code s
                    git remote remove origin; # Delete Origin, otherwise it will expose the Github password
                    # start docker
                    docker-compose build editor-server; Docker-comemage. yml service
                    docker-compose up -d;
                  "Name: delete SSH key # Delete SSH key run: rm -rf ~/. SSH /id_rsaCopy the code

Welcome to pay attention to the public number, learn together

The front-end growth

Refer to the link

  • Docs.github.com/cn/actions/…
  • Docs.docker.com/engine/inst…
  • Docs.docker.com/compose/ins…