Front-end Docker deployment combat
background
Recently, the head of the company’s architecture has been training K8s, and mentioned Dokcer in conversation with the leaders of the team members. Back when I got to know Docker in 17 years, I studied it and made a lot of notes. So far, I have been eating nothing in my notes for a long time. The server that oneself buys before lane again recently plans to operate.
preface
1 installation docker
Environment to prepare
1. Some basic server knowledge is required. Centos7.3. The xshell operation is usedCopy the code
Environment view
# system kernel [root@iZbp1a48kwmva61kyrz5caZ ~]# uname -r 3.10.0-957.21.3.el7.x86_64Copy the code
Viewing the System Environment
[root@iZbp1a48kwmva61kyrz5caZ ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID=" CentOS" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0; 31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"Copy the code
Install the docker
Help: docs.docker.com/
In the Window environment, you can see:
1. First of all, we can check whether docker has been installed in the system
whereis docker
Copy the code
Since it has been installed before, we will now reinstall it, first uninstalling docker and then installing the Docker process step by step
$sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ $sudo yum remove docker-client \ docker-client-latest \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine # 2. $sudo yum install -y yum-utils # 3 1. Set up the mirror warehouse $sudo yum - config - manager \ - add - repo \ https://download.docker.com/linux/centos/docker-ce.repo version is not recommended (abroad) 2. Domestic version (the authors use ali cloud) $sudo yum - config - manager - add - 'http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 4. Docker-ce docker-ee enterprise version $sudo yum install docker-ce docker-ce cli containerd. IO # 5 [root@iZbp1a48kwmva61kyrz5caZ ~]# systemctl start docker [root@iZbp1a48kwmva61kyrz5caZ ~]# docker version Client: Docker engine-community Version: 20.10.2 API Version: 1.41 Go Version: go1.13.15 Git commit: 2291f61 Built: Mon Dec 28 16:17:48 2020 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.2 API Version: 1.41 (minimum Version 1.12) Go Version: Go1.13.15 Git commit: 8891C58 Built: Mon Dec 28 16:16:13 2020 OS/Arch: Linux/AMd64 Experimental: false containerd Version: 1.4.3 GitCommit: 269548 fa27e0089a8b8278fc4fc781d7f65a939b runc: Version: 1.0.0 - rc92 GitCommit: Ff819c7e9184c13b7c2607fe6c30ae19403a7aff docker - init: Version: 0.19.0 GitCommit: de40ad0 see here basically docker installation is successful. $sudo yum remove docker-ce docker-ce cli containerd. IO 2. $sudo rm -rf /var/lib/docker $sudo rm -rf /var/lib/containerdCopy the code
Ali Cloud image acceleration
Aliyun address: cr.console.aliyun.com/cn-hangzhou…
Docker common commands
Docker’s main core modules
A docker Image is like a template container: Container Docker uses Container technology to independently run one or a group of applications. It is a simplified version of Linux. Start, stop, and delete repositories. (Volumn) Data volumes are designed to persist data, and their life cycle is independent of the container. Docker does not automatically delete data volumes after the container is deleted, and there is no garbage collection mechanism to deal with data volumes without any container reference. If you need to remove the data volume while deleting the container.Copy the code
Docker info docker info docker --helpCopy the code
Enter docker –help to view various docker-related commands
[root@iZbp1a48kwmva61kyrz5caZ ~]# docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: Docker App (Docker Inc., V0.9.1-beta3) Builds with BuildKit (Docker Inc., V0.9.1-beta3) V0.5.1 - Docker) config Manage Docker configs container Manage Containers Context Manage Contexts Image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. To get more help with docker, check out our guides at https://docs.docker.com/go/guides/Copy the code
Image command (Image)
Docker images #explain REPOSITORY TAG IMAGE ID CREATED SIZE REPOSITORY TAG ID CREATED SIZECopy the code
Docker Search searches for images
[root@iZbp1a48kwmva61kyrz5caZ /]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely 2, the open - source base... 1046 [OK] Mariadb Mariadb is a community-developed fork of MyS... 3866 [OK] mysql/mysql-server Optimized mysql Server Docker images.create... 762 [OK] Percona Server is a fork of the MySQL... 524 [OK] centos/mysql-57-centos7 mysql 5.7 SQL Database Server 87Copy the code
You can also search for images on DockerHub
Container command (Container)
[Optional] imageid # Parameter Description --name=" name "# imageid # -p host port: [root@iZbp1a48kwmva61kyrz5caZ /]# docker run -it centos /bin/bash [root@7a19fe65ed43 /]# ls bin etc lib lost+found MNT proc run SRV TMP var dev home lib64 media opt root sbin sys usrCopy the code
Lists the containers that are running
[root@iZbp1a48kwmva61kyrz5caZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZbp1a48kwmva61kyrz5caZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a19fe65ed43 centos "/bin/bash" 3 minutes ago Exited (130) 10 seconds ago priceless_napierCopy the code
Exit or delete commands
Exit containers exit Ctrl + P + Q not cease docker rm container id # to delete the specified container Can't delete the running docker rm -f $(docker ps - aq) # delete all containers docker ps - a - q | Xargs docker rm # delete all containersCopy the code
Start and stop containers
Docker start container ID Docker restart container ID Docker Stop container ID Docker kill container IDCopy the code
Other Common Commands
[root@iZbp1a48kwmva61kyrz5caZ /]# docker run -d centos 07422 da5b573599ecc27184295cf7b6cb3fb839668969216f1f6bfd1b8b52e50 docker ps # problems found centos docker common pit stop cargo container runs in the background There must be a foreground process Docker logs -f -t --tail num container id No logCopy the code
Docker runs the program
Create your first Hello-world project
[root@iZbp1a48kwmva61kyrz5caZ ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ we can see that he carried out a few steps 1. The hello world image is not found. 2. Pull the remote DockerHub to pull the image. If yes, pull it.Copy the code
Look at mirror
[root@iZbp1a48kwmva61kyrz5caZ ~]# Docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest Bf756fb1ae65 14 months ago 13.3kBCopy the code
Deployed nginx
Download docker pull nginx 3. Docker run -d --name nginx01 -p 4444:80 ngixn # [root@iZbp1a48kwmva61kyrz5caZ /]# curl locahost:4444 curl: (6) Could not resolve host: locahost; Unknown error [root@iZbp1a48kwmva61kyrz5caZ /]# curl localhost:4444 <! DOCTYPE html> <html> <head> <title>Welcome to nginx! </title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx! </h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@iZbp1a48kwmva61kyrz5caZ /]# docker exec -it nginx01 /bin/bash root@6a214c47903b:/# ll bash: ll command not found root@6a214c47903b:/# ls bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@6a214c47903b:/# where nginx bash: where: command not found root@6a214c47903b:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginxCopy the code
Container data volume usage
What is a container data volume
Is that each of our containers is a simplified version of the operating system,
Dockerfile
Create Dockefile
This section uses node installation as an example
1.mkdir nodejs 2.cd nodejs && npm init -y 3. The new app. Js DockerfileCopy the code
As shown in the figure:
Open the Dockerfile
FROM node:14.5.0-alpine3.12 RUN mkdir -p /home/service WORKDIR /home/service COPY. /home/Service RUN NPM install EXPOSE 3000 CMD ["npm","start"]Copy the code
Save. Perform the start build image in the current directory
docker build -t nodejs:latest .
Copy the code
When you execute Docker images after the build is complete, you can see the image being executed
Then perform
docker run -itd -p 4444:80 nodejs
Copy the code
Then type the address in your browser
docker-compose
Docker – cmpose is what
Docker-compose is something that makes Docker automated. Docker-compose allows you to automate all complex Docker operations in one command.
Why docker-compose, and what does it solve
In popular language, docker operation is still a series of primitive actions. The actions you use manually can be divided into several steps
Find a system image / 1 / docker search 2 installed vm or virtual box / / apt - get the install docker 3 installation image / / in the vm docker run 4 - d - it your image A little..Copy the code
There are two basic concepts for docker-compose:
1.service: an application container, docker container, such as mysql container, nginx container (project) : A complete business unit consisting of a set of associated application containers, such as the mysql, Web App, and Nginx containers mentioned above. Docker-compose is managed for projects.
Install nginx using Docker-compose
1. Install Docker-compose first
Domestic sudo curl -l https://download.fastgit.org/docker/compose/releases/download/1.27.4/docker-compose- ` ` uname - s - ` uname -m '> /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-composeCopy the code
2. Create a docker-comemess. yml file in the docker-comemess. yml file
Version: "3" volumes: latest ports: -2333:80 # mount - / root/nginx/HTML: / usr/share/nginx/HTML # mapping file - / root/nginx/nginx. Conf: / etc/nginx/nginx. Conf # mapping ring: True # Container nginx call permission issueCopy the code
Version: represents the version of our compose file. Currently, there are 1, 2, and 3 versions. The syntax of each version is different. Services: We are going to start defining services, one for each Docker container. Nginx: the name of the service image: the image of the service port: the empty port number of the service build: the building parameters of the container volume: /user/nginx/ HTML working_dir: specifies the container working directory environment: sets the environment variables. The laravel framework will not load the. Env configuration if the environment variable already has a value, DB_HOST and REDIS_HOST are the parameters in the. Env file. Database and Redis are the next service names to be defined.Copy the code
3. Compile the nginx.conf file
Map $sent_http_content_type $expires {default off; text/html epoch; } HTTP {include mime. Types; HTTP {include mime. default_type application/octet-stream; sendfile on; keepalive_timeout 65; #gzip on; server { listen 80; server_name xxxxxxxx; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html last; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}}Copy the code
4. Choreography docker – compose
CD go to the current directory of the file and run docker-compose up -dCopy the code
-d indicates that services are run in the background. After the execution is complete, services are choreographed and commands are executed one by one. If the docker-compose orchestration is successful, enter the address in the browser and execute successfully.
At the end
That’s pretty much the end of it. The follow-up will continue to update the modification, see this don’t forget to click a thumbs-up ~