Docker with Spring Boot and MySQL: Introduction (Part 1)

You Rui java class by learning to share, we look with Docker tutorial Spring Boot and MySQL. Very practical reference for everyone to share learning.

Docker is a technology developer or DevOps team can use containers to build, deploy and manage applications. Docker is an open source software, so anyone can run the operating system on its own operating system, the operating system should support virtualization and applies to Mac / Windows / Linux of Docker.

Docker will be ready image transported from one computer to another computer. Docker container is a set of processes isolated from the host OS in the rest of the process. Docker shared hosting operating system kernel. On the other hand, rely on the VM is a guest OS in the art, the client and the host OS are completely separated. Virtual machine hypervisor OS communicates with the host. VM requires a lot of hardware resources and processes. It is not container-based technology.

Docker abstract resources from the hardware level to enhance the operating system level. This is the reason why the easier application portability and infrastructure separation.

Technical occupy fewer resources container-based host system. Most of the time, using the host-based container technology core. The main objective of this paper is to achieve Docker Spring Boot application and MySQL.

basic concept

Docker Engine as a client - server architecture. Docker act as a server daemon, which is the core part of the Docker and run on the host operating system. This service provides a number of remaining available to the client API's. Command Line Interface (CLI) client to use the service provided by the daemon Docker Docker command.

 

 

Docker daemon

Important Terms

Image : The image is inside the Docker executable application package file. It contains everything needed to run the application, including libraries, configuration files, runtime and so on. It is a snapshot of the container. Container : example of an image called a container. When the image is occurred in the memory and executed, examples of the image is called a container. It performs in a fully isolated environment.

With OOP terms, a class is Image, and the container is an instance of the class - runtime objects.

Registry : The registry is stored in storage and content delivery system Docker image. Docker Hub is a popular Docker registry. We can have different versions of different tag number of images stored in the Docker Hub.

Dockerfile : This is a text file that contains all of the commands used to assemble the image. 

Dockerfile  > (Build) >  Image  > (Run) >  Container

installation

In this article, we will use the Docker Hub as a Docker registry. We will then follow the instructions to set the desktop Docker applications. I use the Mac OS as the host. After installation, we can see Docker application is running. Then, I have to use

Docker Hub credentials.

 

 

 

 

 

 

After Docker Desktop installation and log

Now, we can open a terminal to check some of Docker command.

docker --version command should return Docker version. In my case, this command returns

Docker version 19.03.2, build 6a30dfc

docker info命令应返回主机已安装的Docker计算机的详细信息。 最初,有零个图像和容器。

docker pull hello-world此命令应从Docker注册表中提取hello-world映像。 再一次,如果我们运行Docker info命令,我们可以看到映像计数为1。但是容器计数仍然为零。

docker run hello-world此命令应创建hello-world映像的实例。 它将返回一条长消息:

好,Docker!

此消息表明的安装似乎正常工作。

如果运行Docker info命令,我们可以看到映像计数为1,容器计数也为1。 至此Docker安装完成

安装MySQL

现在,我们将创建并运行MySQL数据库的映像。 在我们的终端上,我们将运行以下命令。 此处,此命令中的-d表示Docker命令将以分离模式运行。

1 docker run -d -p 6033:3306 --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=book_manager" mysql

希望将MySQL映像拉出并作为容器运行。 要检查这一点,我们可以运行

docker image ls和docker container ls命令。 就我而言,这些命令的响应是:

1 mysql latest b8fd9553f1f0 3 days ago 445MB 
2 hello-world latest fce289e99eb9 8 months ago 1.84kB
3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
4 5db66654ba6a mysql "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 33060/tcp, 0.0.0.0:6033->3306/tcp docker-mysql

现在我们可以通过登录MySQL进行检查。

1 docker exec -it docker-mysql bash;

它将带我们进入docker-mysql容器。 然后我们将使用密码为root的mysql -uroot -p登录到MySQL。 然后,我们将运行show数据库; 命令以查看数据库设置是否完成。

就我而言,它返回以下结果:

 

1 +--------------------+ | Database | +--------------------+ | book_manager | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)

 

因此,可以说book_manager数据库是在docker容器内创建的。 我们可以通过端口6033从主机外部使用此数据库。现在,我们必须将数据库脚本导入Docker MySQL数据库。 SQL脚本在此处可用。 运行以下命令将此脚本导入到docker-mysql中。

1 docker exec -i docker-mysql mysql -uroot -proot book_manager <book_manager.sql

 

希望book_manager脚本成功执行。 可以通过执行以下命令进行确认。

 

 1 $ docker exec -it docker-mysql bash;
 2 
 3 
 4 root@5db66654ba6a:/# mysql -uroot -p
 5 
 6 
 7 Enter password:
 8 
 9 
10 mysql> show databases;
11 
12 
13 +--------------------+ | Database | +--------------------+ | book_manager | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use book_manager Database changed
14 
15 
16 mysql> show tables;
17 
18 
19 +------------------------+ | Tables_in_book_manager | +------------------------+ | author | | book | | book_author | | book_publisher | | book_tag | | bookshelf | | publisher | | tag | +------------------------+ 8 rows in set (0.01 sec) mysql>

 

 

应用程序克隆和构建项目

 

我已经将代码推送到GitHub存储库中。 任何人都可以从此处克隆代码库。 我认为的主机已安装Gradle。 所以现在我们运行gradle build命令来构建项目。 因此,可执行的jar文件是在克隆项目的build / jar目录中创建的。

 

现在打开Dockerfile。 我们可以看到该文件包含以下命令:

 

 1 FROM java:8
 2 
 3 
 4 VOLUME /tmp
 5 
 6 
 7 EXPOSE 10222
 8 
 9 
10 ADD /build/libs/book-manager-1.0-SNAPSHOT.jar book-manager-1.0-SNAPSHOT.jar
11 
12 
13 ENTRYPOINT ["java","-jar","book-manager-1.0-SNAPSHOT.jar"]

 

 

该文件包含要在docker中执行的顺序命令。 它将创建Java 8的映像,还将jar文件从主机复制到docker映像,然后运行在入口点参数处给出的命令。 现在我们将使用此Dockerfile构建一个Docker映像。

 

1 docker build -f Dockerfile -t book_manager_app .

 

 

该命令将为Docker计算机创建一个名为book_manager_app的Docker镜像。 -f命令表示Docker文件名。 现在,我们将该图像作为容器运行。

 

1 docker run -t --link docker-mysql:mysql -p 10222:10222 book_manager_app

 

 

--link命令将允许book_manager_app容器使用MySQL容器的端口,而-t代表--tty,它将分配一个伪终端。

 

运行此命令后,我们将在主机浏览器中访问http:// localhost:10222 / book,它将返回书籍列表。

 

这些都是通过简单的Spring Boot实现和MySQL数据库提供的Docker入门知识的。

文章写道这里,如有不足之处,欢迎补充评论。

抽丝剥茧,细说架构那些事--优锐课

 

 

 

 

Guess you like

Origin www.cnblogs.com/youruike1/p/12066140.html