Sprboot / cloud (18) Use docker to quickly build local environments

Posted by ohjay on Wed, 22 May 2019 04:16:57 +0200

Sprboot / cloud (18) Use docker to quickly build local environments

In peacetime development work, the construction of the environment has always been a very troublesome thing.

Especially now, the system is more and more complex, and there are more and more middleware to connect.

However, to install these middleware smoothly is also a time-consuming and laborious work.

As the saying goes, "If you want to do a good job, you must first use the right tools." This article will share with you some common methods of quickly building some middleware containers in the local environment or test environment using docker.

(There is no mention of the production environment, not that the production environment can not use docker, but that in order to use it in the production environment, you must make a full assessment according to your own actual situation.)

docker installation

docker supports multiple operating systems. The content of this paper is based on centos7 operating system.

docker's documentation is very clear, you can

https://docs.docker.com/engine/installation/linux/docker-ce/centos/

The docker is installed step by step on the site according to the document steps. If everything works, you can type version on the command line and get the following results:

$ sudo docker version
# Results Output: 
Client:
 Version:      17.09.0-ce-rc2
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   363a3e7
 Built:        Thu Sep 14 01:54:43 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.0-ce-rc2
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   363a3e7
 Built:        Thu Sep 14 01:56:05 2017
 OS/Arch:      linux/amd64
 Experimental: false

Commonly used docker basic commands

  • Docker images: List all current docker images

  • Docker ps-a: List the currently running containers of docker, remove - a, and list all containers

  • docker rmi {Mirror ID}: Delete the docker image and append the - f instruction to force the deletion of the image (after deleting the image, the container associated with the image will also be deleted)

  • docker rm {Container ID/Name}: Delete containers and append-f instructions to force the removal of containers

  • Docker run XXXXXX: Running containers. This article will explain the instructions of run command in detail later.

  • docker stop {container ID/container name}: Stop container

  • docker start {container ID/container name}: Start container

  • docker stats: View the running status of all docker containers

  • docker log {container ID/container name}: View the container's run log

  • docker cp {container ID/container name}: {container path} {host path}: Copy files from container to host

  • Docker exec-it {container ID/container name}/ bin/bash: Enter the specified container for operation and exit

There are many more docker commands, you can:

https://docs.docker.com/reference/

Documentation of other commands found on the site

Main parameters of docker run command

In general, we use the run command to create a container. The main format of this command is as follows:

docker run {instruction/parameter} {registry address} /{organization name} /{mirror name}: {version}

Unique identification of docker image

The string {registry address}/{organization name}/{mirror name}: {version} constitutes a unique identity for the docker image. The specific value may be as follows:

wjrfy32i.mirror.aliyuncs.com/library/nginx:latest

The above string describes the nginx image of Aliyun, followed by the latest version (referring to the latest version)

Common instructions for docker run commands

  • - d: Container Background Start

  • - it: Container interactive startup

  • - p {Host Port}: {Container Port}: Specifies the mapping relationship between host and container ports, which can be multiple

  • - name {container name}: specify the name of the container

  • - v {Host Directory}: {Container Directory}: Mapping Host File Directory to Container File Directory can be multiple

  • - restart=always: Specifies that the container will always start with the docker service

  • - hostname {container hostname}: The host name of the specified container

  • --link={container name}: {hostname}: Adding other containers to the hosts file of the current container and specifying the hostname it accesses can be multiple

** Other directives

  • - e TZ="Asia/Shanghai": The time zone of the specified container

  • - v/etc/localtime:/etc/localtime:ro: Setting the container clock is the same as the host machine

Create mysql containers

Get ready

$ sudo mkdir /var/lib/mysql && chown -R 200 /var/lib/mysql

The above command creates a directory for storing mysql database files

Establish

$ sudo docker run \ 
                -d \ 
                --restart=always \ 
                -p 3306:3306 \ 
                -e TZ="Asia/Shanghai" \ 
                -v /etc/localtime:/etc/localtime:ro \ 
                -v /var/lib/mysql:/var/lib/mysql \ 
                --name mysql \ 
                -e MYSQL_ROOT_PASSWORD=XXX \ 
                wjrfy32i.mirror.aliyuncs.com/library/mysql:latest

Where - e MYSQL_ROOT_PASSWORD=XXX specifies the root password of the mysql database

Create rabbitmq container

Get ready

$ sudo mkdir /var/lib/rabbitmq && chown -R 200 /var/lib/rabbitmq
$ sudo mkdir /var/log/rabbitmq && chown -R 200 /var/log/rabbitmq

The above command creates a path for storing rabbitmq data files and log files

Establish

$ sudo docker run \ 
                -d \ 
                --restart=always \ 
                --hostname rabbitmq \ 
                --name rabbitmq \ 
                -p 15672:15672 \ 
                -p 5672:5672 \ 
                -e TZ="Asia/Shanghai" \ 
                -v /etc/localtime:/etc/localtime:ro \ 
                -v /var/lib/rabbitmq:/var/lib/rabbitmq \ 
                -v /var/log/rabbitmq:/var/log/rabbitmq \ 
                -e RABBITMQ_DEFAULT_USER=admin \ 
                -e RABBITMQ_DEFAULT_PASS=xxx \ 
                wjrfy32i.mirror.aliyuncs.com/library/rabbitmq:3.6.10-management-alpine

Where - e RABBITMQ_DEFAULT_USER and - e RABBITMQ_DEFAULT_PASS are used to specify the account and password of the rabbitmq console administrator

Create redis containers

Get ready

$ sudo mkdir /var/lib/redis && chown -R 200 /var/lib/redis
$ sudo mkdir /var/lib/redis_conf && chown -R 200 /var/lib/redis_conf

The above command creates a directory for storing redis data files and configuration files

In addition, the configuration file can be obtained from redis.conf from redis official website and uploaded to the host's / var/lib/redis_conf directory.

Establish

$ sudo docker run \ 
                -d \ 
                --restart=always \ 
                --hostname redis \ 
                --name redis \ 
                -p 6379:6379 \ 
                -e TZ="Asia/Shanghai" \ 
                -v /etc/localtime:/etc/localtime:ro \ 
                -v /var/lib/redis:/data \ 
                -v /var/lib/redis_conf/redis.conf:/usr/local/etc/redis/redis.conf \ 
                wjrfy32i.mirror.aliyuncs.com/library/redis:latest \ 
                redis-server /usr/local/etc/redis/redis.conf

Mapping the host's / var/lib/redis_conf/redis.conf to the container and specifying the configuration file used by redis-server

Create nginx containers

Get ready

$ sudo mkdir /var/lib/nginx && chown -R 200 /var/lib/nginx
$ sudo mkdir /var/lib/nginx/conf.d && chown -R 200 /var/lib/nginx/conf.d
$ sudo mkdir /var/lib/nginx_conf && chown -R 200 /var/lib/nginx_conf
$ sudo mkdir /var/lib/nginx_content && chown -R 200 /var/lib/nginx_content
$ sudo mkdir /var/log/nginx && chown -R 200 /var/log/nginx
$ sudo mkdir /var/lib/nginx_file && chown -R 200 /var/lib/nginx_file
$ sudo docker run -d --hostname nginx --name nginx -p 80:80 wjrfy32i.mirror.aliyuncs.com/library/nginx:latest
$ sudo docker cp nginx:/etc/nginx/nginx.conf /var/lib/nginx/nginx.conf
$ sudo docker cp nginx:/etc/nginx/conf.d/default.conf /var/lib/nginx/conf.d/default.conf
$ sudo docker cp nginx:/usr/share/nginx/html/50x.html /var/lib/nginx_content/50x.html
$ sudo docker cp nginx:/usr/share/nginx/html/index.html /var/lib/nginx_content/index.html
$ sudo docker rm -f nginx

The above command creates column directories for storing static files and configuration files of nginx.

And use the cp command to copy the original configuration file and the default html file from nginx

Finally, the containers created for copying files are deleted.

Establish

$ sudo docker run \ 
                -d \ 
                --restart=always \ 
                --hostname nginx \ 
                --name nginx \ 
                --link=rabbitmq:rabbitmq \ 
                -p 81:81 \ 
                -e TZ="Asia/Shanghai" \ 
                -v /etc/localtime:/etc/localtime:ro \ 
                -v /var/lib/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \ 
                -v /var/lib/nginx/conf.d:/etc/nginx/conf.d:ro \ 
                -v /var/lib/nginx_content:/usr/share/nginx/html:ro \ 
                -v /var/lib/nginx_file:/usr/share/nginx/file:ro \ 
                -v /var/log/nginx:/var/log/nginx \ 
                wjrfy32i.mirror.aliyuncs.com/library/nginx:latest

The above commands map a series of directories to containers, mainly static files for different scenarios

Packaging spring boot applications into docker images

Add plug-ins to pom

<!-- docker -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
        <imageName>192.168.1.10:5000/${project.groupId}/${project.artifactId}:latest</imageName>
        <baseImage>192.168.1.10:5000/org.itkk/java:8</baseImage>
        <maintainer>wangkangit@163.com</maintainer>
        <exposes>${server.port}</exposes>
        <cmd></cmd>
        <entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>
        <resources>
            <resource>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
  • imageName: Specifies the unique flag for the image, which is set using maven's groupId and artifactId

  • baseImage: Specifies the parent image of the mirror, which is the mirror of java8

  • maintainer: Specifies the author of the mirror

  • Expos: Specify the port open to the mirror, which is the same as the port number of the application

  • entryPoint: A command that should be executed when a container is in use. Here, a spring boot application is launched with the java-jar command

structure

$ sudo mvn clean install package docker:build

Execute the above command, docker:build builds the maven into a docker image after the maven build is complete

Be careful

Creating your own image requires docker registry support, either public or self-built.

For space reasons, without explaining the construction process of docker registry, interested students can Baidu by themselves.

In addition, since the constructed image is usually large (at least about 100M), the students who use the public docker registry should pay attention to the problem of network speed.

End

In the above content, we have created rabbitmq,mysql,nginx,rabbitmq, which are four commonly used middleware.

With the docker command, we can create these services very quickly.

Compared with the traditional way, it really saves a lot of time and experience.

Of course, all of the above are basic ways of playing. docker can do more than that.

Interested students can learn more about it.

On the content of this article, we welcome your comments and suggestions.

Code Warehouse (Blog Matching Code)

For the fastest update, please pay attention to the public number.

Topics: Java Docker Nginx sudo RabbitMQ