Alibaba cloud Linux server deployment docker project complete tutorial (springboot+mysql)

Posted by Eskimo on Sat, 02 Oct 2021 03:54:05 +0200

Install Docker
Install Docker Compose (this is required for the operation of a complete project)

This document mainly records the process of learning to deploy Docker. The article is followed by the deployment of open source projects based on Docker and Docker Compose. It's never too old to learn, keep an empty cup mentality, learn knowledge and skills carefully. I am a front-end programmer, like tossing new things, new things, interested can pay attention to my personal website( Ape vision).

Later in the article, I will teach you how to deploy SpringBoot+Vue+MySQL+Nginx open source project with docker. The main purpose of using docker to deploy the project is to save our limited time. Source code of open source project: https://github.com/cloudfavorites/favorites-web After we have installed the docker environment, we will take you to deploy this open source project.

prerequisite

Installation command

It is recommended to install Docker in linux environment. The window environment is complex and error prone. It is convenient to install Docker environment using Centos7+yum.

Docker package has been included in the default CentOS extras software source. Therefore, to install docker, just run the following yum command:

yum install docker

Start Docker service

After installation, use the following command to start the docker service and set it to startup:

service docker start
chkconfig docker on

Test Docker installation succeeded

test

docker version

Enter the above command to return the version information of docker, which proves that docker is installed successfully.

Set domestic image

Modify docker to domestic image address through vim: https://registry.docker-cn.com

Because the official warehouse in China is slow to connect to Docker, we will use Docker China accelerator in daily use. Through the accelerated Docker official mirror, users in China can quickly access the most popular Docker images. The mirror is hosted in Chinese mainland, and local users will enjoy faster download speed and stronger stability, so that they can be more agile. Send and deliver docker based applications.

Docker China's official image acceleration can be accessed through registry.docker-cn.com. This image library only contains popular public images, and private images still need to be pulled from the American image library.

Modify the configuration file corresponding to docker in the system, as follows:

vi  /etc/docker/daemon.json

#After adding
{
    "registry-mirrors": ["https://registry.docker-cn.com"],
    "live-restore": true
}

Demonstrate the Hello World program

//Run the following command to grab the image file from the warehouse to the local.
docker pull library/hello-world

//In the above code, docker image pull is the command to grab image files.
//Library / Hello world is the location of the image file in the warehouse,
//Where library is the group where the image file is located, and hello world is the name of the image file.
docker images

//Now, run the image file.
docker run hello-world

After outputting this prompt, hello world will stop running and the container will terminate automatically. Some containers will not terminate automatically because they provide services, such as Mysql image.

Install Docker Compose

Through docker Compose, users can easily define a multi container application with a configuration file, and then install all the dependencies of the application with one instruction to complete the construction. Docker Compose solves the problem of how to manage orchestration between containers. Dockerfile allows users to manage a separate application container, while Compose allows users to manage a single application container A set of associated application containers (called a project, i.e. project) are defined in the template (YAML format), such as a Web service container and a back-end database service container.

Docker Compose is an independent product of docker, so you need to install Docker Compose separately after installing docker

#download

sudo curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

#install

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

#View version

docker-compose version

Common commands

In addition to the Docker commands we used above, Docker also has some other commonly used commands

Pull docker image

docker pull image_name

View the image on the host. The Docker image is saved in the / var/lib/docker Directory:

docker images

delete mirror

docker rmi  docker.io/tomcat:7.0.77-jre7   perhaps  docker rmi b39c68b7af30

See which containers are currently running

docker ps

View all containers

docker ps -a

Start, stop and restart container commands:

docker start container_name/container_id
docker stop container_name/container_id
docker restart container_name/container_id

After starting a container in the background, if you want to enter the container, you can use the attach command:

docker attach container_name/container_id

Command to delete a container:

docker rm container_name/container_id

Delete all stopped containers:

docker rm $(docker ps -a -q)

View Docker information of current system

docker info

Download an image from Docker hub:

docker pull centos:latest
docker pull centos:latest

Find nginx image on Docker Hub

docker search nginx

Executing docker pull centos will download all the images under the Centos repository to the local repository.

Deployment project demo (Cloud Collection)

Deploy SpringBoot project - Cloud Collection

Address: https://github.com/cloudfavorites/favorites-web

-bash: unzip: command not found
Note: it has not been installed. Just execute the following commands. It is also applicable to Unix kernel system

yum install -y unzip zip

Drop down items:

wget https://github.com/cloudfavorites/favorites-web/archive/favorites-1.3.0.zip

decompression

unzip favorites-1.3.0.zip

Enter directory

cd favorites-web-favorites-1.3.0/
Modify file application-docker.properties
vi app/src/main/resources/application-docker.properties

The amendments are as follows:

favorites.base.path=http://xx.xxx.xx.xx/ 
forgotpassword.url=http://xx.xxx.xx.xx/newPassword

The address is the address of the deployment server

Deployment command:

docker-compose up --build
 perhaps
docker-compose up -d
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'forgotpassword.url' in value "${forgotpassword.url}"
app_1    |      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:379) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
app_1    |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
app_1    |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
app_1    |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
app_1    |      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
app_1    |      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
app_1    |      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
app_1    |      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.

To modify a project profile:

favorites.base.path=http://Public ip/
forgotpassword.url=http://Public ip/newPassword 

Project started successfully:

Access ip: http://47.100.188.105/ View project home page

Data backup and recovery

mysql data backup (shell script backs up mysql data regularly)

Standard format:

docker exec [mysql-container-name] /usr/bin/mysqldump -u [user] --password=[password] [database-name] > [backup-file-name]
docker exec CONTAINER /usr/bin/mysqldump -u username --password=xxx DATABASE > backup.sql

Case:

docker exec c7f1b671c57f /usr/bin/mysqldump -u ***** --password=*****  favorites | gzip > /root/data_backup/database_name_2021_05_08.sql.gz

The above backup command will prompt a warning: mysqldump: [warning] using a password on the command line interface can be secure

The following backup commands do not display warnings.

docker exec -it c7f1b671c57f mysqldump -uroot -p***** favorites | gzip > /root/data_backup/database_name_2021_05_07.sql.gz  [Put shell The script will fail to execute, shell [command line execution valid]

Script scheduled backup:

#!/bin/bash
echo '##########################################'
echo '###### The database is automatically backed up at 01:00 am every day ######'
echo '##########################################'
# Setting environment variables
DATE=$(date +%Y_%m_%d_%H_%M_%S)
# $(date +%Y%m%d)
# Database user name
USERNAME=root
# password
PASSWORD=****
# Database to back up
DB=favorites

# Docker container ID
DOCKERID=c7f1b671c57f

#DB1=xxxx
# The ubuntu system is not a root user. You should put the files generated by backup in the directory where xxx user is located. Otherwise, you must use sudo to enter the administrator password to execute this script.
DIR=/root/data_backup
echo 'Get system date: ' $DATE
 
if [ ! -d "$DIR" ]; then
mkdir $DIR
fi
cd $DIR
 
echo 'backup started...' $(date "+%Y-%m-%d %H:%M:%S")
	
# Back up the database in the docker container
# docker exec -it container name mysqldump -u user name - p password database name to be backed up | gzip > file name to package
	
# docker exec -it $DOCKERID mysqldump -u$USERNAME -p$PASSWORD $DB > /root/data_backup/database_name_$DATE.sql
	
# The above backup command is invalid. Please change to the following backup database command.
	
	
docker exec $DOCKERID /usr/bin/mysqldump -u $USERNAME --password=$PASSWORD  $DB | gzip > /root/data_backup/database_name_$DATE.sql.gz

# Back up the database installed directly in the linux system
# /usr/bin/mysqldump -u$USERNAME -p$PASSWORD $DB2 | gzip > xxx_$DATE.sql.gz
# just backup the latest 7 days

# find ${DIR} -name "database_name_*.sql.gz" -type f -mtime +7 -exec rm {} \; > /dev/null 2>&1
	
# find ${DIR} -name "database_name_*.sql.gz" -type f -mtime +7 -exec rm {} \; > /dev/null 2>&1


#Keep the latest documents
ReservedNum=5
date=$(date "+%Y%m%d-%H%M%S")
 
FileNum=$(ls -l $DIR|grep ^- |wc -l)
 
while(( $FileNum > $ReservedNum))
do
    OldFile=$(ls -rt $DIR| head -1)
    echo  $date "Delete File:"$OldFile
    rm -rf $DIR/$OldFile
    let "FileNum--"
done 

# mysql data backup is over!
echo 'backup completed!' $(date "+%Y-%m-%d %H:%M:%S")

Here I use the scheduled task of the pagoda panel to execute shell script tasks regularly:

To view the final SHELL file backup directory:

mysql data recovery:

cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u username --password=xxx DATABASE


cat database_name_2021_05_08_18_30_01.sql | docker exec -i 6e64fa337312 /usr/bin/mysql -u user name --password=User password database name

If the content of mysql my.cnf configuration file is modified, Docker needs to be rebuilt: Docker compose up -- build, otherwise the configuration file cannot take effect.

#Enter a container
docker exec -it c7f1b671c57f /bin/bash

# Enter the mysql command line

mysql -uroot -p

# View max_allowed_packet.  If this parameter is set too small, the backup sql recovery will fail. The default is 4MB and the setting is 200MB

show variables  like '%max_allowed_packet%';

# Find mysql configuration file location

mysql --help | grep my.cnf

/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

# View docker mysql file configuration

cat /etc/my.cnf

After backing up the database, I reported an error when restoring the backed up sql data to the Docker mysql container:
ERROR 1153 (08S01) at line 50: Got a packet bigger than 'max_allowed_packet'

In the meantime, I tried many methods and failed to change them. After repeated attempts, it was finally changed successfully. The docker project needs to be rebuilt before the mysql configuration file modification takes effect. Instead of simply restarting the docker container.

Configuration modification succeeded.

Then the mysql backup data is restored to normal. Finally solved the little problem.

Note: of which

CONTAINER: CONTAINER alias or CONTAINER id: c7f1b671c57f
DATABASE: name of the DATABASE to be operated: favorites
username: user name of database login:*****
xxx: password for database login:******

Risks will be prompted after implementation;
After successful import and export, dos will enter the normal input state

Docker installation phpmyadmin

This is mainly installed to manage the mysql database online

Steps:

# Download the latest version of phpmyadmin
docker pull phpmyadmin/phpmyadmin
# Start container
 docker run -d --name myadmin -e PMA_HOST=Public network ip -e PMA_PORT=3306 -p 888:80 phpmyadmin/phpmyadmin

Command description:

-e PMA_HOST=192.168.206.132 IP address of database
-e PMA_PORT=3307 port of the database
-p 8283:80 map container port 80 to host port 8283
– name myadmin name the container myadmin

Browser access: http: / / public ip:888 / online access phpmyadmin

Error reporting problem record

–initialize specified but the data directory has files in it. Aborting.

After restarting docker for a certain period of time, the mysql container of the project cannot be started. The mysql container has been restarted. The mysql container cannot be started. Of course, the project cannot be accessed normally. Continue to solve it immediately. Check the log command and find the above error.

    volumes:
      - ./mysql_data:/var/lib/mysql

Including: MySQL_ The data directory is the linux hard disk mount directory. Mapped to docker container directory / var/lib/mysql
Mysql_ The files in the data directory can be deleted. If the data is important, please backup it first and then delete it.

Solution: find the specific location of the docker mysql datadir mounting directory, back up the files in this directory, then delete them, re package and start the project with the docker compose up - D build command, and then start the mysql container correctly. The key reason is that there are files in the directory attached to the datadir hard disk and they are damaged, so the docker mysql container cannot be restarted.
reference material: Use Docker to quickly build Mysql and separate data volumes

docker logs container id
View container startup and operation logs

#Get the list of running docker containers and find the specific container id
docker ps  
docker logs container id

Start docker compose and stop docker compose

# Docker compose is an instruction that refers to multiple containers of a choreographed project
# Stop project docker
docker-compose stop
# Recompile the code and start the project docker container
docker-compse up -d build  

Focus on the original more exciting https://www.yundashi168.com/244.html

Topics: Linux MySQL Docker