Docker quick start



Docker's idea comes from the container. What problem does the container solve? In a large ship, the goods can be placed neatly. And all kinds of goods are standardized by containers, and containers will not affect each other. Then I don't need a ship for fruit and a ship for chemicals. As long as the goods are well sealed in the container, I can take them all away in a big ship.

docker is a similar concept. Cloud computing is popular now. Cloud computing is like a large cargo ship. docker is a container.

1. Different applications may have different application environments. For example, the website developed by. net and the website developed by php depend on different software. If the software they depend on is installed on a server, it will take a long time to debug, which is very troublesome and will cause some conflicts. For example, IIS and Apache access ports conflict. At this time, you have to isolate the websites developed by. net and php. Generally speaking, we can create different virtual machines on the server and place different applications on different virtual machines, but the cost of virtual machines is relatively high. docker can realize the function of isolating the application environment of virtual machines, and the overhead is smaller than that of virtual machines, which means saving money.

2. When you develop software, you use Ubuntu, but the O & M management is centos. When O & M transfers your software from the development environment to the production environment, it will encounter some problems of converting Ubuntu to centos. For example, there is a special version of database, which is only supported by Ubuntu and not supported by centos. In the process of transfer, O & M must find ways to solve such problems. At this time, if there is a docker, you can directly package and transfer the development environment to the O & M, and the O & M can directly deploy the docker you gave him. And the deployment speed is fast.

3. In terms of server load, if you open a separate virtual machine, the virtual opportunity will occupy free memory, which will be used when docker is deployed.

In short, docker is the principle of container.

Docker is a Open Source The application container engine allows developers to package their applications and dependency packages into a portable image, and then publish them to any popular application Linux or Windows On the machine, it can also be realized Virtualization . The container is fully used sandbox Mechanism, there will be no interface between them.

1. Why does docker appear?

	A product goes from development to launch, from operating system, to operating environment, and then to application configuration. As development+We need to care about many things about the cooperation between operation and maintenance, which is also a problem that many Internet companies have to face. In particular, after the iteration of each version, the compatibility of different versions of the environment is a test for the operation and maintenance personnel.

	Docker The reason why it develops so rapidly is that it gives a standard solution to the above problems.

	The environment configuration is so troublesome that it takes time and effort to change a machine. Many people think, can we fundamentally solve the problem, and the software can be installed with the environment? That is to say as like as two peas, the original environment is copied exactly. Developer utilization Docker It can eliminate the problem of "normal operation on my machine" during cooperative coding.

	Docker All environments and configurations that can run an application can be packaged together to form an image. Pass this image through docker Copy to another machine, and this machine will have the environment and configuration on the original machine. Then the application will also run on the current machine.  

2.Docker concept

	Docker Is based on Go Language implementation of cloud open source project.

	Docker The main objectives of the project are:"Build,Ship and Run Any App,Anywhere",That is, through the management of the life cycle of application components, such as encapsulation, distribution, deployment and operation, users can App(Can be a Web Application or database application, etc.) and its running environment can be "encapsulated at one time and run everywhere".  

	Linux The emergence of container technology solves such a problem, and Docker It developed on its basis. Run app on Docker Above the container, and Docker Containers are consistent on any operating system, which enables cross platform and cross server. You only need to configure the environment once and switch to another machine to deploy it with one click. The operation is greatly simplified.

2.1 what does docker do:

  • Faster delivery and deployment
  • Faster upgrade and capacity expansion
  • Simpler system maintenance

2.2 describe docker in one sentence:

A software container that solves the problems of running environment and configuration, facilitates continuous integration and contributes to the overall release of container virtualization technology.
	an open platform to build, ship, and run any app, anywhere
	An open platform that can build, publish and run any application anywhere

2.3 comparison of docker container virtualization with traditional virtual machines

Traditional virtual machineDocker container
Disk occupancyA few GB to dozens of GB or soTens of MB to hundreds of MB or so
CPU memory usageThe virtual operating system takes up a lot of CPU and memoryDocker engine occupies very low
Starting speed(from Startup to running the project) a few minutes(from opening the container to running the project) a few seconds
Installation managementSpecial operation and maintenance technology is requiredConvenient installation and management
Application deploymentEvery deployment takes time and effortEasy and simple from the second deployment
CouplingMultiple application services are installed together, which is easy to affect each otherEach application service has a container to achieve isolation
System dependencynothingRequires the same or similar kernel. Currently, Linux is recommended

3.Docker three elements

  • Image: a packaged application and its dependent environment configuration on a machine. Images can create containers, which is equivalent to templates. One image can create multiple containers

  • Container: Docker uses the container to run one or a group of applications independently. A container is a running instance created by a mirror.

  • Warehouse: a warehouse is a place where images are stored centrally. Similar to Maven's central warehouse or github. DockerHub

    The relationship between image and container is similar to the relationship between class and object in object-oriented programming


4. Installation of docker

Docker installation in centOS must be above version 6.5. Here is a demo of centOS 7.2

1. If docker has been installed before, uninstall it first. Execute the following command

$ yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
**be careful:\There is no special meaning here. It means line feed. In order to let us see what has been unloaded here

2. Install the package and some storage drivers required by docker

$ yum install -y yum-utils \
  device-mapper-persistent-data \

3. Use the following command to set up a stable repository

$ yum-config-manager \
    --add-repo \

4. Install the latest version of docker Community Edition (Docker CE)

$ yum install -y docker-ce docker-ce-cli

5. Start docker

$ systemctl start docker
$ systemctl enable docker #Startup self startup

6. Verify whether Docker CE is installed correctly by running the Hello world image.

$ docker run hello-world

7. Configure the image accelerator

It is sometimes difficult to pull images from DockerHub in China. At this time, you can configure the image accelerator. Docker officials and many domestic cloud service providers provide domestic accelerator services, such as:

  • China image library officially provided by Docker:
  • Qiniu cloud accelerator:

After configuring an accelerator address, if it is found that the image cannot be pulled, please switch to another accelerator address. All major domestic cloud service providers provide Docker image acceleration services. It is recommended to select the corresponding image acceleration services according to the cloud platform running Docker.

Attachment: configuring alicloud image (acceleration) creation configuration

in the light of Docker Client version greater than 1.10.0 User
 You can modify daemon configuration file/etc/docker/daemon.json To use the accelerator
 Add the following configuration file
  "registry-mirrors": [""]
$ systemctl daemon-reload  #Load profile from New
$ systemctl restart docker #Restart docker

5. Operation process of docker run

6. Common commands of docker

6.1 auxiliary commands

$ docker version	#Displays Docker version information.
$ docker info		#Displays Docker system information, including the number of images and containers.
$ docker --help  	#Help command

6.2 mirror command

1. View image information

$ docker images	[options]	#List all local mirrors
	-a			#List all mirrors (including the intermediate image layer)
    -q			#Only the image id is displayed
    --digests	#Display mirror summary information
    --no-trunc	#Do not truncate the output (truncate excessively long columns by default)

2. Search for images

$ docker search [options] Image name		#Go to the dockerhub to query the current image
	-s Specified value		#Lists the mirrors with at least the specified number of collections
    --no-trunc	  #Do not truncate the output (truncate excessively long columns by default)

Parameter Description:

NAME: the NAME of the mirror warehouse source


OFFICIAL: is it officially released by docker

stars: similar to the star in Github, it means like and like.

AUTOMATED: built automatically.

3. Download Image

$ docker pull Image name[:TAG|@DIGEST]	
$ docker pull Image name:edition tomcat 7.0.62 8.5.50 

4. Delete image

$ docker rmi Image name:edition	  #No version specified. Delete the latest version
	-f		#Force delete 

6.3 container command

1. Run the container

$ docker run [OPTIONS] Image name [cmd]			   #Image name creates a new and starts the container
	-i							#Run the container in interactive mode, usually with - t
	-t							#Reassign a pseudo terminal to the container, usually with - i
	--name alias				   #Specify a name for the container
	-d							#Start the daemon container (start the container in the background) and return the container ID
	-p Mapping port number: original port number		 #Specify the port number to start, and specify the port mapping
	-P							#Host automatically assigns port number and random port mapping
	--rm                        #Automatic removal when container stops                                  
Example: $ docker run -it --name myTomcat -p 8888:8080 tomcat
    $ docker run -d --name myTomcat -P tomcat

be careful:

#If the following error is reported during startup:
docker: Error response from daemon: driver failed programming external connectivity on endpoint myTomcaaa (1148dccd5673fab421495087d352adc5428ab6ab7cf9f3fd708b662a25d92641):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 32807 -j DNAT --to-destination ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).
#The user-defined chain docker defined when the docker service is started is cleared for some reason
#Restart docker service and regenerate user-defined chain docker

#Restart the docker service before starting the container
systemctl restart docker
docker start foo

2. View running containers

$ docker ps		 #Lists all running containers
	-a			#Displays all containers, including those that are not running.
	-l			#Displays the most recently created container
	-n numerical value	   #Displays the last n containers created
	-q			#In silent mode, only the container number is displayed
	--no-trunc	 #Do not truncate the output (truncate excessively long columns by default)

Output details:



COMMAND: the COMMAND that runs when the container is started.

CREATED: the creation time of the container.

STATUS: container STATUS.

There are 7 states:

  • created
  • restarting
  • running
  • removing (migrating)
  • paused
  • exited
  • dead

PORTS: the port information of the container and the connection type used (tcp\udp).

NAMES: automatically assigned container name

3. Exit the container

$ exit		 #Container stop exit
$ Ctrl+p+q	 #The container does not stop exiting 

4. Enter the container

$ docker attach Container name/container id

5. Delete container

$ docker rm  Container name/container id		  #Delete container
$ docker rm -f 	Container name/container id	  #Delete a running container
$ docker rm -f $(docker ps -aq)	   #Delete all containers

6. Restart the container

$ docker start Container name/container id  	    #Open container
$ docker restart Container name/container id  	#Restart container

7. Stop the running container

$ docker stop Container name/container id 	   	#Normal stop of vessel operation
$ docker kill Container name/container id     	#Stop the container immediately

8. View container log

$ docker logs [OPTIONS] Container name/container id	  		
	-t			 #Join time
	-f			 #Follow the latest log print
	--tail number	#Show the last number

9. View the processes in the container

$ docker top Container name/container id   		

10. Check the internal details of the container

$ docker inspect Container name/container id    		

11. Enter the container

$ docker exec [options] Container name/container id In container command   		
	-i		#Run the container in interactive mode, usually with - t
	-t		#Assign a pseudo terminal

eg: docker exec -it centoss ls 

docker exec -it mytomcat /bin/bash

12. Copy

$ docker cp Container name/container id:Resource path in container host directory path  		#Copy the resources in the container to the host

[root@localhost~]docker cp centoss:/aaa.txt /root/

#Files can be shared between the host and the container
$ docker cp Host directory path container name/container id:Resource path in container

eg:docker cp /root/bbb.txt centoss:/

Host computer:Docker Where is the host installed  centos
 container:stay Docker A container that is started according to the image   centos container

13. Package and mirror a copy of the container

$ docker commit -a="author" -m="Description information" container ID Target image name:TAG

Example: docker commit -a="nan" -m="witout docs" b35d35f72b8d nan/mytomcat:1.2
	1.from dockerHub Download tomcat Mirror to local and run successfully
	2.Delete the image of the container generated in the previous step doc catalogue
	3.Not currently available doc Catalog tomcat As template commit Generate a new image
	4.Start a new image and compare it with the original image

7. Image principle of docker

7.1 what is mirroring?

	Image is a lightweight and executable independent software package, which is used to package the software running environment and software developed based on the running environment. It contains all the contents required to run a software, including code, libraries required for runtime, environment variables and configuration files.

7.2 why is a tomcat image so large?

Mirror image is a thousand layer cake.

7.3 union fs (Union file system):

	Union File system is a layered, lightweight and high-performance file system. It supports the superposition of file system modifications from one submission to another. At the same time, different directories can be mounted under the same virtual file system. Union The file system is Docker The basic image of the image. The image can be inherited through layering. Based on the basic image (there is no parent image), various specific application images can be made.
	characteristic:Multiple file systems are loaded at the same time, but from the outside, only one file system can be seen. Joint loading will overlay all layers of file systems, so that the final file system will contain all underlying files and directories 

7.4 Docker image loading principle:

	docker The image of is actually composed of layer by layer file systems.
	bootfs(boot file system)Mainly contains bootloader and kernel,bootloader Mainly boot loading kernel,Linux Will load at startup bootfs File system docker The bottom layer of the image is bootfs. This layer and Linux/Unix The system is the same, including boot Loader( bootloader)And kernel( kernel). When boot After loading, the whole kernel is in memory. At this time, the right to use the memory has been granted by bootfs To the kernel, which will be uninstalled bootfs. 
	rootfs(root file system),stay bootfs Above, it contains typical linux In the system/dev,/proc,/bin,/etc And other standards. rootfs Various operating system distributions, such as Ubuntu/CentOS wait.
	We usually install it into the virtual machine centos There are 1 to several GB,Why? docker It's only 200 here MB?For a streamlined OS,rootfs It can be very small. It only needs to include the most basic commands, tools, and program libraries, because the bottom layer is used directly Host of Kernal,You only need to provide rootfs That's it. It can be seen that different linux Distribution, their bootfs Is consistent, rootfs There will be a difference. Therefore, different distributions can be shared bootfs. 
	linux :centos6 centos7  macos  

7.5 why does the docker image adopt this hierarchical structure?

	One of the biggest benefits is resource sharing. For example, there are multiple images from the same base If the image is built from, the host only needs to save one copy on disk base Mirror. At the same time, only one copy needs to be loaded in memory base Image, you can serve all containers. And every layer of the image can be shared.
	characteristic: Docker Mirrors are read-only. When the container starts, a new writable layer is loaded on top of the image. This layer is usually called the container layer, and below the container layer is called the mirror layer.

8.Docker container data volume

8.1 what is a data volume

	In short, it is to do data persistence. Similar to tape, removable hard disk or U Plate. Or something like that Redis Medium rdb and aof File. It is mainly used for container data persistence and data sharing between containers. A volume is a directory or file that exists in one or more containers and is controlled by docker Mounted to a container, but not a federated file system, so it can bypass Union File System Provides features for persistent storage or sharing of data. The volume is designed for data persistence, which is completely independent of the life cycle of the container docker The data volumes on which the container is mounted are not deleted when it is deleted.

8.2 characteristics of data volume

1,Data volumes can share or reuse data between containers.
2,Changes in the volume can take effect directly.
3,Changes in the data volume are not included in the mirror's updates.
4,The life cycle of a data volume continues until no container uses it.
5,Data volumes can also complete host to container or container to host data sharing.

8.3 adding data volumes

There are two ways to add data volumes to a container. The first direct command is added. The second is to use DockerFile add to.

8.3.1 command addition

1. Add

Command: docker run -it -v /Path to the host:/The path image name within the container

example: docker run -it -v /hostDataValueme:/containerDataValueme centos

2. Check whether the data volume is hung successfully

Run the docker inspect container id command to check whether there are the following contents in the json string. If so, it proves that the volume is mounted successfully.

"Mounts": [
                "Type": "bind",
                "Source": "/hostDataValueme",
                "Destination": "/containerDataValueme",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"

You can then check whether the container and the host can share resources. Or whether the data in the host's volume can be read and loaded after the container is closed and then opened.

Set the data volume in the container to be read-only.

Command: docker run -it -v /Host path:/In container path:ro Image name
 example: docker run -it -v /hostDataValueme:/containerDataValueme:ro centos

Check configuration file

"Mounts": [
                "Type": "bind",
                "Source": "/hostDataValueme",
                "Destination": "/containerDataValueme",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"

8.3.2 adding dockerfile

What is Dockerfile

Simply put, it is a description file of an image.

1. Create a new file (dockerfile) in a directory and add the following script to the file

For example, create a mydocker folder in the / directory, and create a dockerfile file in this folder

# volume test
FROM centos
VOLUME ["/containerDataValueme1","/containerDataValueme2"]
CMD echo "finished,-------success!"
CMD /bin/bash

Note: you can use the volume directive in the dockerfile to add one or more data volumes to the mirror.

2. Production image after build (build image with prepared file file)

$ docker build -f mydocker/dockerfile -t zcn/centos .
	-f	#Specify the dockerfile file path 
	-t	#Specify the destination mirror name
	.	#In the current folder

After the image is created, run a container instance with the current image. At this time, we will find that there are already data volumes written in the container, but the data volumes in the container should be shared with the host. So where is the host's data volume reflected? Don't worry, although we can't specify the host volume with dockerfile. However, docker will provide us with the default host data volume.

Run docker inspect to check and find:

"Mounts": [
             "Type": "volume",
             "Name": "459e7a4be53a96eee859f11d10bc0b26a6a91bbd6754ecb8e355e9fe4a31e0b9",
             "Source": "/var/lib/docker/volumes/459e7a4be53a96eee859f11d10bc0b26a6a91bbd6754ecb8e355e9fe4a31e0b9/_data",
             "Destination": "/containerDataValueme2",
             "Driver": "local",
             "Mode": "",
             "RW": true,
             "Propagation": ""
             "Type": "volume",
             "Name": "4bf41829e4afaebbb40cf3d0d4725343980afffa04f09a30680fa957d80b6af4",
             "Source": "/var/lib/docker/volumes/4bf41829e4afaebbb40cf3d0d4725343980afffa04f09a30680fa957d80b6af4/_data",
             "Destination": "/containerDataValueme1",
             "Driver": "local",
             "Mode": "",
             "RW": true,
             "Propagation": ""

Benefits of using Dockerfile to build images

For the consideration of portability and sharing, use-v This way can not be directly in Dockerfile Implemented in.
Because the host directory depends on a specific host, it is not guaranteed that such a specific directory exists on all host computers.

9.DockerFile parsing

9.1. What is dockerfile

Dockerfile Is used to build docker The mirrored build file is a script composed of a series of commands and parameters.

Build steps write Dockerfile file
2.Docker build  create mirror
3.Docker run    Run container

Dockerfile content Basics

1.Each reserved word instruction must be capitalized and followed by at least one parameter.
2.The instructions are executed from top to bottom.
3.#Indicates a comment.
4.Each instruction creates a new mirror layer and commits the mirror.

Reserved word instruction in Dockerfile

Reserved wordeffect
FROMWhich image is the current image based on
MAINTAINERName and email address of the image maintainer
RUNInstructions that need to be run when the container is built
EXPOSEThe exposed port number of the current container
WORKDIRSpecify the working directory in which the terminal logs in by default after creating the container, which is a foothold
ENVUsed to set environment variables during image building
ADDCopy the files in the host directory into the image, and the ADD command will automatically process the URL and decompress the tar package
COPYSimilar to ADD, copy files and directories to the image
Copy the file / directory from the < original path > in the build context directory to the < target path > location in the image of the new layer
VOLUMEContainer data volume for data storage and persistence
CMDSpecify a command to run when the container starts
There can be multiple CMD instructions in Dockerfile, but only the last one takes effect. CMD will be replaced by the parameters after docker run
ENTRYPOINTSpecify a command to run when the container starts
The purpose of ENTRYPOINT, like CMD, is to start the program and its parameters in the specified container
ONBUILDWhen building an inherited Dockerfile, run the command. After the parent image is inherited, the onbuild of the parent image is triggered

9.2 analysis of dockerfile construction process

1.Docker Run a container from the underlying mirror
2.Execute a command and modify the container
3.Execution similar docker commit Commit a new mirror layer
4.docker Then run a new container based on the newly committed image
5.implement dockerfile The next instruction in until all instructions are executed

9.3 summary

1.From the perspective of application software, Dockerfile,docker Mirror docker Containers represent three different phases of software
	Dockerfile It is the raw material of software
	docker Mirroring is a software deliverable
	docker The container can be considered as the running state of the software
2.Dockerfile Development oriented, docker Image becomes the delivery standard, docker Container involves deployment and operation and maintenance, which are indispensable and work together docker The cornerstone of the system.

1.Dockerfile,Need to define a Dockerfile,Dockerfile Defines everything the process needs. Dockerfile The contents involved include executing code or files, environment variables, dependent packages, runtime environment, dynamic link library, operating system distribution, process services and kernel processes (when an application needs to deal with system services and kernel processes, what design should be considered namespace And so on
2.docker Mirror, in use Dockerfile After defining a file, docker build Will produce a docker Mirror, when running docker When you mirror, you really start providing services
3.docker Container, which provides services directly

10.Dockerfile case

Base scratch: 99% of the images in the Docker Hub are built by the software required for installation and configuration in the base image.

10.1 custom image myCentos

1. First learn about Centos on Docker Hub.

DockerHub Upper centos The default foothold of mirroring is/,Not supported by default vim. So now we want to customize one centos,Change its default foothold to support vim. 

2. Write Dockerfile

FROM centos
RUN yum -y install vim
CMD echo "build-------success"
CMD /bin/bash

3. Build image

$ docker build -f /myDocker/dockerfile -t zcn/mycentos .

4. Operating the container

$ docker run -it -P zcn/mycentos

10.2 custom image myTomcat

1. First create the directory / mydocekrfile/tomcat

take tomcat and jdk8 of tar Put the package in this directory. Then create a dockerfile File.

2. Write Dockerfile

FROM centos
#Copy the c.txt of the current context of the host into the / usr/local / path of the container
COPY ./c.txt /usr/local/cincontainer.txt
#Copy the tar package of tomcat and jdk to the container
ADD ./apache-tomcat-9.0.22.tar.gz /usr/local/
ADD ./jdk-8u171-linux-x64.tar.gz /usr/local/
#Install vim editor
Run yum -y install vim
#Set login foothold / usr/local
ENV MYPATH /usr/local/
#Configuring environment variables for java and tomcat
ENV JAVA_HOME /usr/local/jdk1.8.0_171
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.22
#The port on which the container listens while running
#Run tomcat at startup
# ENTRYPOINT ["/usr/local/apache-tomcat-9.0.22/bin/"]
# CMD ["/usr/local/apache-tomcat-9.0.22/bin/","run"]
CMD /usr/local/apache-tomcat-9.0.22/bin/ && tail -F /usr/local/apache-tomcat-9.0.22/bin/logs/catalina.out

Create image jar package for java project

FROM centos
#Copy the c.txt of the current context of the host into the / usr/local / path of the container
#Copy the tar package of jdk to the container
ADD ./jdk-8u171-linux-x64.tar.gz /usr/local/
#Set login foothold / usr/local
ENV MYPATH /usr/local/
#Configuring environment variables for java and tomcat
ENV JAVA_HOME /usr/local/jdk1.8.0_171
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
COPY ./yingx_zhangcn184s-0.0.1-SNAPSHOT.jar yingx_zhangcn184s-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","./yingx_zhangcn184s-0.0.1-SNAPSHOT.jar"]

3. Build image

$ docker build -t zcn/mytomcat .
It's not written here-f Because if it is in the current directory and file The file name is orthodox dockerfile that-f It can be omitted.

4. Operating the container

$ docker run -d -p 8888:8080 --name mytomcat -v /zcn/tomcat/test:/usr/local/apache-tomcat-9.0.22/webapps/test -v /zcn/tomcat/logs:/usr/local/apache-tomcat-9.0.22/logs --privileged=true zcn/tomcat

Explanation: create a data volume and make a mapping between the project directory in the webapps directory in the container and the test directory in the host

10.3 summary

11. Install mysql

11.1 find mysql image on DockerHub

$ docker search mysql

11.2 pull mysql locally

$ docker pull mysql:5.6

11.3 running mysql container

$ docker run -p 3333:3306 --name mysql -v /zcn/mysql/conf:/etc/mysql/conf.d -v /zcn/mysql/logs:/logs -v /zcn/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6 

11.4 specific operation

#Enter this container 
docker exec -it container id /bin/bash

#Enter mysql  
mysql -u root -p 
#Enter password 123456

#Query mysql database    
show databases;

#Create database    
create database ems;

#Switch to db01     
use ems;

#Build table
create table t_book(id int not null primary key, name varchar(20));

#Query all tables
show tables;

#Insert a piece of data into the table  
insert into t_book values(1,'java');

#Query the data of this table 
select * from t_book;

#Try to connect to this mysql with rhubarb navcat of our external win10

#Data backup test 
docker exec mysql container id sh -c 'exec mysqldump --all-database -u root -p "123456"' > /gzcusr/all-databases.sql

12. Install redis

12.1 find redis image on DockerHub

$ docker search redis

12.2 pull redis to local

$ docker pull redis:4.0.14

12.3 running redis container

$ docker run -p 6666:6379 --name myredis -v /zcn/redis/data:/data -v /zcn/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf -d redis:4.0.14 redis-server /usr/local/etc/redis/redis.conf --appendonly yes

12.4 specific operation

On the host/zcn/redis/conf/redis.conf New under directory redis.conf Documents,
$ vim /zcn/redis/conf/redis.conf/redis.conf

Write in it redis to configure.(To virtual machine redis Find the relevant configuration file and copy it OK Yes)
#Connect to redis  
$ docker exec -it container id redis-cli

#Write several key values in it. Close and leave.
#Go back to the host / zcn/redis/data directory and check whether there are aof files.

13. Use of remote warehouse

1. Prepare the image

2. Alicloud creates an image library

3. Push the local image to the alicloud image library

$ sudo docker login 
#Alicloud login password is required
$ sudo docker tag [ImageId][Mirror version number]
$ sudo docker push[Mirror version number]

$ sudo docker login
$ sudo docker tag zcn/mytomcat
$ sudo docker push

4. Pull the Alibaba cloud remote warehouse image to the local location

$ docker pull Image name of alicloud address:Version number
 Just copy it from the official website
 Public network address:

$ docker pull 1.0

14. Install elasticsearch

Note: increase the JVM thread limit

#In the centos window, modify the configuration sysctl.conf
	$ vim /etc/sysctl.conf
#Add the following configuration
	$ vm.max_map_count=262144 
#Enable configuration
	$ sysctl -p
#Note: this step is to prevent the following errors from being reported when starting the container:
bootstrap checks failed max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

14.1 pull es to local

$ docker pull elasticsearch:6.8.2

14.2 start es container

$ docker run -d --name es -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms128m -Xmx128m" -v /usr/local/IKAnalyzer:/usr/share/elasticsearch/plugins elasticsearch:6.8.2

14.3 installing IK word splitter

1.Download the corresponding version of IK Tokenizer 
$ wget

2.Unzip to plugins/elasticsearch Folder
	$ yum install -y unzip
	$ unzip -d plugins/elasticsearch

3.Add custom extension words and stop words
	cd plugins/elasticsearch/config
	vim IKAnalyzer.cfg.xml
		<comment>IK Analyzer Extended configuration</comment>
		<!--Users can configure their own extended dictionary here -->
		<entry key="ext_dict">ext_dict.dic</entry>
		<!--Users can configure their own extended stop word dictionary here-->
		<entry key="ext_stopwords">ext_stopwords.dic</entry>

4.stay ik Under the word splitter directory config Create in directory ext_dict.dic The file code must be UTF-8 To take effect
	$ vim ext_dict.dic Add extension words
5. stay ik Under the word splitter directory config Create in directory ext_stopword.dic file 
	$ vim ext_stopwords.dic Just add a stop word
6.Commit this container as a new image
	$ docker commit -a="zk" -m="with IKAnalyzer" b35d35f72b8d zk/elasticsearch:6.8.2
7.Use the newly generated es Mirror creates a container and mounts data volumes
	$ docker run -d --name es -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms128m -Xmx128m" -v /usr/local/IKAnalyzer:/usr/share/elasticsearch/plugins/elasticsearch/config zcn/elasticsearch:6.8.2

14.3 installation of kibana kibana Mirror to local
$ docker pull kibana:6.8.2

2.start-up kibana container
$ docker run -d --name kibana -e ELASTICSEARCH_URL= -p 5601:5601 kibana:6.8.2

15. Image creation of springboot project

1. Import maven dependency


        <!--support jsp Plug in for-->

        <!--  springboot Provided project compilation and packaging plug-ins  -->


matters needing attention:

1) The version of the springboot packaging plug-in must be 1.4.2.RELEASE. Only this version supports jsp, not higher versions
2) All front-end resources under webapp must be packaged in the META-INF/resources directory of the jar package, otherwise they cannot be recognized

2. Type the project into a jar package

matters needing attention:

After the jar package is completed, use the local test

java -jar jar Package name.jar

3. Put the jar package into the host

4. Configure dockerfile

Preparation under current directory: jdk-8u171-linux-x64.tar.gz yingx_zhangcn184s-0.0.1-SNAPSHOT.jar

FROM centos
#Copy the tar package of jdk to the container
ADD ./jdk-8u171-linux-x64.tar.gz /usr/local/
#Set login foothold / usr/local
ENV MYPATH /usr/local/
#Configuring java environment variables
ENV JAVA_HOME /usr/local/jdk1.8.0_171
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
COPY ./yingx192_zhangcns-0.0.1-SNAPSHOT.jar yingx192_zhangcns-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","./yingx192_zhangcns-0.0.1-SNAPSHOT.jar"]

5. Build image

docker build -t yingx .
It's not written here-f Because if it is in the current directory and file The file name is orthodox dockerfile that-f It can be omitted.

6. Run image

Note: the mapping port is the port number of the project

docker run -it --name yingx -p 9292:9090 yingx

7. Visit the project

Under the directory of r esources, otherwise it cannot be recognized

2. Type the project into a jar package

[external chain picture transferring... (IMG rnyprnxa-1632983479129)]

matters needing attention:

After the jar package is completed, use the local test

java -jar jar Package name.jar

3. Put the jar package into the host

4. Configure dockerfile

Preparation under current directory: jdk-8u171-linux-x64.tar.gz yingx_zhangcn184s-0.0.1-SNAPSHOT.jar

FROM centos
#Copy the tar package of jdk to the container
ADD ./jdk-8u171-linux-x64.tar.gz /usr/local/
#Set login foothold / usr/local
ENV MYPATH /usr/local/
#Configuring java environment variables
ENV JAVA_HOME /usr/local/jdk1.8.0_171
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
COPY ./yingx192_zhangcns-0.0.1-SNAPSHOT.jar yingx192_zhangcns-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","./yingx192_zhangcns-0.0.1-SNAPSHOT.jar"]

5. Build image

docker build -t yingx .
It's not written here-f Because if it is in the current directory and file The file name is orthodox dockerfile that-f It can be omitted.

6. Run image

Note: the mapping port is the port number of the project

docker run -it --name yingx -p 9292:9090 yingx

7. Visit the project

Tags: Operation & Maintenance Docker

Posted on Thu, 30 Sep 2021 20:15:21 -0400 by DaveLinger