Docker profile

  • docs.docker.com/
docker run --name nginx-test -p 8080:80 -d nginx
Copy the code
docker run --rm -d -p 40001:3000 jayfong/yapi:play
The default administrator account is [email protected] and the password is adm1n
Copy the code

What is a Docker

  • Docker is an open platform that integrates packaging, running, testing and publishing
  • We can isolate the infrastructure from the development process and deploy it to Docker
    • DevOps: Development, build, automated deployment, testing, documentation
    • GitLib, Jenkins
    • Nginx, Apache
    • MySQL, directing
    • Document management tool
  • With Docker, you can avoid complex application environment configuration and start in seconds
  • Supported on most platforms, the performance overhead of the container is extremely low

Docker application scenarios

  • Automated packaging and distribution of Web applications
  • Automated testing and continuous integration, release
  • Deploy and adjust databases or other backend applications in a service environment

Docker core concepts

  • Docker Daemon Daemon
    • Docker Daemon is a Docker Daemon
    • The Docker Client communicates with the Docker Daemon through the command line to complete docker-related operations
  • Docker Client Is a Client
    • Interact with the user through the terminal
    • Terminal input instructions, Docker client to pass instructions to the Docker Daemon
  • Docker Image mirror
    • Think of it as an image of a minimal Linux system, containing the required file system and some configured applications
    • Images need to be loaded through containers
    • It is static and can be compared to object-oriented classes
  • Docker Container vessel
    • Create a container from the image
    • You can create multiple containers, each of which starts a process, isolated from each other
    • It is dynamic and can be compared to an object-oriented instance

Docker architecture

  • Docker uses a client-server (C/S) architecture pattern that uses remote apis to manage and create Docker containers

The difference between Docker and virtual machine

  • VMS are hardware-level virtualization. Each VM needs to divide system resources and create virtual hardware
  • Docker is system-level virtualization. Containers share system resources and do not virtualize hardware

  • Notes on the official website

    • www.docker.com/resources/w…
  • Container technology and virtual machine difference

Install the Docker

Reference documentation

  • docs.docker.com/install/

  • www.runoob.com/docker/cent…

  • Start and stop Docker

systemctl start docker
systemctl stop docker
# Boot
systemctl enable docker
Run immediately and start up
systemctl enable --now docker
Copy the code
  • Run a Hello World
docker run hello-world

# 1. Docker Client connects to Docker Daemon
Docker daemon pulls an image of 'Hello World' from Docker Hub
# 3. The Docker Daemon creates a container based on the pulled image
# 4. The Docker daemon sends the result of application execution in the container to the Docker client and prints it to the terminal
Copy the code

Mirror to accelerate

When Docker gets the image, it will get it from Docker Hub by default. The server is in a foreign country, so the download will be slow in China. We can set it to a domestic image address, similar to NPM

  • Reference documentation
    • www.runoob.com/docker/dock…
  • Common domestic mirror address
    • docker.mirrors.ustc.edu.cn
    • almtd3fa.mirror.aliyuncs.com
    • registry.docker-cn.com
  • When obtaining the image, specify the address
docker run hello-world --registry-mirror=https://docker.mirrors.ustc.edu.cn

docker run hello-world --registry-mirror=https://almtd3fa.mirror.aliyuncs.com
Copy the code
  • Set the mirror address during the configuration
    • Set the following in the /etc/docker-daemon. json file
{"registry-mirrors":["https://almtd3fa.mirror.aliyuncs.com", "https://registry.docker-cn.com"]}
Copy the code
  • Restart the service
systemctl daemon-reload
systemctl restart docker
Copy the code

Docker basic use

Docker image is used

  • www.runoob.com/docker/dock…
  • Find the image through the Docker Hub website
    • hub.docker.com/
  • Common commands
# View the local mirror
docker images

Start, stop, and restart the specified containerDocker start/stop/restart Container ID# View the container being executed
docker ps

# View all containers
docker ps -a

Get a mirror image
Centos :latest, the default is the latest version
docker pull centos

# delete mirror
docker rmi hello-world

# delete containerDocker Rm container nameSet a new tag for the image
# image id -> 860c279d2fec
# mirror user name -> LFZ
-> centos
# tag -> dev
docker tag 860c279d2fec lfz/centos:dev
Copy the code

containers

  • Create a container using the image
Start a container with a centos image
-i Interactive operation, -t terminal, centos image name, /bin/bash Command executed after the image runs open terminal
docker run -it centos /bin/bash

Different mirror users are different, and the parameters used to start the mirror are also different
--name Specifies the name of the nginx-server container
-p mapping container port, host port: container port, nginx image name
docker run -d --name nginx-server -p 8080:80 nginx
Copy the code
  • Common commands
# View all containers
# view all running containers without a parameter
docker ps -a

Check the status of the running container
docker stats 

Start the container with the container id or container name
docker start nginx-server

Stop, restart, and delete containers
docker stop nginx-serve
docker restart nginx-serve
docker rm -f nginx-serve

Clean up all terminated containers
docker container prune

# Enter container
docker exec -it nginx-server /bin/bash

View the logs inside the container
docker logs -f nginx-server
Copy the code
  • Import and export containers
Export the snapshot file of the container
docker export nginx-server > nginx-server.tar

Mynginx image name, v1 image tag
cat docker/nginx-server.tar | docker import - mynginx:v1
Copy the code

Install yapi

Hub.docker.com/r/jayfong/y…

docker run --rm -d -p 40001:3000 jayfong/yapi:play
The default administrator account is [email protected] and the password is adm1n.
Copy the code

Install the nginx image

www.runoob.com/docker/dock…

Docker command is commonly used

  • mirror
# Open a container with an image and enter the container - IT interactive terminal operation
docker run -it nginx /bin/bash 
Start a container with an image, run in the background
--name nginx-test container name -p port mapping -d Nginx background image name
docker run --name nginx-test -p 8080:80 -d nginx
# mirror delete
docker rmi nginx:test
Copy the code
  • Container operation
Execute command in container
docker exec -it nginx-test /bin/bash 
# start stop
docker start bb0d52be2f81
docker stop bb0d52be2f81
# remove
docker rm bb0d52be2f81

Clean up all terminated containers
docker container prune
Copy the code

The container deploys the vue.js project

  • Cn.vuejs.org/v2/cookbook…

Dockerfile

  • Docs.docker.com/engine/refe…
  • Create a Docker image
  • Docker image configuration file

  • Dockerfile
# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install --registry=https://registry.npm.taobao.org 
COPY.
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
  • Using Dockerfile
Docker build --rm -t deploy-web:v1.0.If you cannot resolve the address of NPM install during compilation, you can use the host networkDocker network ls docker build --network host --rm -t deploy-web:v1.0.# open containerDocker run-itd --name web -p 88:80 deploy-web:v1.0Copy the code
  • Test in a browser

Docker Hub

  • Private warehouse Harbor
  • Public warehousehub.docker.com/
    • First log in to Docker Hub
docker login
Create a new image from the containerDocker commit a6a685eb4ba1 goddlts/web: v1.1Upload image to hub
docker push goddlts/web
Copy the code

Map the configuration directory to the site root directory

  • Map the configuration directory to the site root directory
# copy the nginx configuration file from the container to the current directory
docker cp web:/etc/nginx .
Copy the code
docker run -itd \ --name web-web \ --volume /home/mypro:/usr/share/nginx/html \ --volume /home/nginx:/etc/nginx \ -p 89:80 \ deploy - web: v1.0Copy the code
# change permission
chmod -R 777 .
Copy the code

Gitlab

The installation

  • about.gitlab.com/install/
  • Docs.gitlab.com/omnibus/doc…
  • [hub.docker.com/r/gitlab/gi…
Docker run --detach \ --hostname 192.168.137.129 \ --publish 13880:80 --publish 13822:22 \ --name gitlab \ --restart always \ -v /home/gitlab/config:/etc/gitlab -v /home/gitlab/logs:/var/log/gitlab -v /home/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest

docker run --detach \
  --hostname lfz.com \
  --publish 13880:80 --publish 13822:22 \
  --name gitlab \
  --restart always \
  -v /backup/gitlab/config:/etc/gitlab -v /backup/gitlab/logs:/var/log/gitlab -v /backup/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest

Firewall allows ports
firewall-cmd --add-port=13880/tcp --permanent
firewall-cmd --reload

docker logs -f gitlab
Copy the code
  • Use a browser to modify the Chinese interface

    • http://192.168.137.129:13880/
    • Click profile picture – settings-preferences – Localization – Language – Save Changes – to refresh the browser
  • Modify the port displayed by Gitlab

vi /home/gitlab/config/gitlab.rb
Copy the code
external_url 'http://192.168.137.129:13880'
nginx['listen_port'] = 80
gitlab_rails['gitlab_shell_ssh_port'] = 13822
Copy the code
docker restart gitlab
Copy the code

Disable firewall (resolve DNS resolution failure in container)

  • Disabling the Container Firewall
  • Restart the docker
# disable firewall
systemctl stop firewalld
# Boot disable
systemctl disable firewalld
# Boot enabled
systemctl enable firewalld
# restart docker
systemctl restart docker
Copy the code

Jenkins

  • www.jenkins.io/zh/doc/
# Download image
docker pull jenkins/jenkins
Create container 1
docker run -itd --name jenkins --net host --restart always -p 8080:8080 -p 50000:50000 jenkins/jenkins


docker run -itd --name jenkins --restart always -p 8080:8080 -p 50000:50000 jenkins/jenkins

Copy the code
Jenkins default administrator password
docker logs -f jenkins

# 36f11bfeab664931b463a2047fb3e395

Enable port 8080
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
# disable firewall
systemctl stop firewalld
Copy the code
  • Recreate Jenkins
# Copy Jenkins' data to host
docker cp jenkins:/var/jenkins_home /home/

Create container 2docker run -itd \ --name jenkins \ --user jenkins \ --net host \ --restart always \ -p 8080:8080 -p 50000:50000 \ -v /home/jenkins_home:/var/jenkins_home \ -v /usr/bin/docker:/usr/bin/docker \ -v /var/run/docker.sock:/var/run/docker.sock  \ jenkins/jenkins# the host machine
cd /home/jenkins_home
chmod -R 777 .
chmod -R 777 /usr/bin/docker
chmod -R 777 /var/run/docker.sock
Copy the code

Jenkins plug-in

  • plugins.jenkins.io/
  • Jenkins plug-in acceleration
    • mirrors.tuna.tsinghua.edu.cn/
  • Jenkins configures the acceleration address
    • Manage Jenkins –> Manage Plugins –> Advanced –> Update Site
    • Mirrors.tuna.tsinghua.edu.cn/jenkins/upd…
  • Installing common plug-ins
    • Localization: Chinese (Simplified)
    • Git client
    • GitLab
    • Gitlab Authentication
    • Publish Over SSH
    • GitHub
    • Build Timeout
  • Permission management plug-in
    • PAM Authentication
    • Role-based Authorization Strategy
    • LDAP

Continuous integration configuration

Jenkins

  • Configure Gitlab permissions in Jenkins
    • System Management -> Global Security Configuration

  • Fill in after creating the application in Gitlab

  • After setting up, apply and save !!!!!! first The lessons of the blood

Gitlab

  • Configure the Jenkins callback address
    • To view the callback address how to write: plugins. Jenkins. IO/gitlab – oaut…
    • Admin Panel -> Applications -> New Application
    • http://192.168.137.128:8080/securityRealm/finishLogin 

  • Allow outgoing requests

    • Admin Panel – Settings – Network – Outbound Requests –
      • Allow requests to the local network from web hooks and services
  • Gitlab builds new warehouses and pushes local warehouses over

  • New task in Jenkins

    • Configure git addresses and credentials
    • Build triggers when push when GitLab executes scripts
      • GitLab webhook URL: http://192.168.137.128:8080/project/mypro
      • Generate Secret Token in advanced
        • 8ecbbcb6dad150467cc48906940052f4
      • Set the script to execute in the build
Check whether the myprocon container exists
docker ps | grep myprocon &> /dev/null
if [ $? -eq 0 ]
then
	docker rm -f myprocon
fiDocker run-itd --name myprocon -p 89:80 mypro:v1.0 docker run -t mypro:v1.0Copy the code
  • The warehouse at Gitlab that wants to apply this task
    • Repository Settings -> Webhooks
      • Configure the Webhook URL in Jenkins
      • Configure Jenkins tokens
      • Removing SSL Authentication
      • Click Add Webhook
      • Click Test on the bottom webhook to trigger push

Docker compose

  • Official: docs.gitlab.com/omnibus/doc…
web:
  image: 'gitlab/gitlab-ce:latest'
  restart: always
  hostname: 'gitlab.example.com'
  environment:
    GITLAB_OMNIBUS_CONFIG: |
      external_url 'https://gitlab.example.com'
      # Add any other gitlab.rb configuration here, each on its own line
  ports:
    - '80:80'
    - '443:443'
    - 'and'
  volumes:
    - '/srv/gitlab/config:/etc/gitlab'
    - '/srv/gitlab/logs:/var/log/gitlab'
    - '/srv/gitlab/data:/var/opt/gitlab'
Copy the code
  • Github:github.com/sameersbn/d…

  • Docker-compose makes it easy to run multiple containers quickly

  • Create and start all containers from the YML configuration file using a single command

The installation

  • Get the latest version of Docker Compose. Installation instructions are provided in the documentation
    • Github.com/docker/comp…
Download the latest stable versionThe curl -l https://github.com/docker/compose/releases/download/1.25.4/docker-compose- ` ` uname - s - ` uname -m ` -o/usr /local/bin/docker-compose

Grant docker-compose executable
chmod +x /usr/local/bin/docker-compose

Test whether the installation is successful
docker-compose --version
Copy the code
# open container
docker-compose up -d
Copy the code