Docker with Spring Boot

Some time ago in my factory under the guidance of the master of the Docker in my actual project landing, the last few small demo are familiar with the use of docker, hope that through this article to share my current use of experience (if not accurate Statement, please help me to point out). The main content of this paper is on the docking of Java applications, the first brief description of the docker and docker-compose, and then use two cases to practice.

Just talk about Docker, cloud computing field is now a mess of it is it. Docker's appearance is to solve the problem of PaaS: the operating environment and the specific language version, the project path strong correlation, so simply use lxc technology for resource isolation, to build the application with the release of the operating environment, thus solving the language version of the problem The PaaS is the emergence of the operation and maintenance personnel do not need to manage a virtual machine, IaaS appears to allow the operation and maintenance personnel do not need to manage the physical machine. Cloud computing, in the final analysis are two words – operation and maintenance.

Cloud computing technology is divided into virtualization technology and resource management in two areas, just corresponding to today we have to talk about the two tools: Docker and docker-compose. Docker's main concepts are: container, mirror, warehouse; docker-compose is a subsequent version of fig, responsible for multiple docker services together, to provide consistent service.

1. Springer application docker

First look at the Springer application docker, due to Spring Boot embedded tomcat, Jetty and other containers, so we have the requirements of the docker mirror is the need for java operating environment. My application code for the Dockerfile file is as follows:

Base mirror: warehouse is java, tag with 8u66-jdk

FROM java: 8u66-jdk

The current mirror of the maintainers and contact information

MAINTAINER duqi duqi@example.com

Copy the packaged spring program to the specified location in the container

ADD target / bookpub-0.0.1-SNAPSHOT.jar /opt/bookpub-0.0.1-SNAPSHOT.jar

The container exposes the port 8080

EXPOSE 8080

The command to be executed after the container is started

CMD java -Djava.security.egd = file: / dev /./ urandom -jar /opt/bookpub-0.0.1-SNAPSHOT.jar
Because the current sample program is relatively simple, the dockerfile does not store the application's data on the host. If your application needs to write a file system, such as a log, it is best to use the VOLUME /tmp command. The effect of this command is to create a temporary file in the / var / lib / docker directory of the host and link it to the container The / tmp directory.

Put this Dockerfile in the root directory of the project, and follow the docker-compose build to build: The base image is read-only, and then add a new, writable layer on the base image for us to use, so the java image Only need to download once.

Docker-compose is used to do docker service arrangement, see "Docker from entry to practice" in the explanation:

The Compose project is currently maintained on Github, and the latest version is 1.2.0. Compose positioning is "define and running complex applications with Docker", the predecessor is Fig, compatible Fig template file.

Dockerfile allows the user to manage a single application container; and Compose allows the user to define a set of associated application containers (called a project, project) in a template (YAML format), such as a Web service container plus On the back of the database service containers and so on.

A single docker is really useless to use, docker technology is the key to continuous delivery, through the combination with jekins, you can achieve this effect: the developer to submit push, and then jekins automatically build and test the code just submitted, which is what I understand Continuous delivery.

Spring boot + redis + mongodb

In this project, I started three containers: web, redis and mongodb, then the web and redis connection, web and mongodb connection. First to be redis and mongodb docker, redis mirror Dockerfile content is:

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get -y install redis-server
EXPOSE 6379
ENTRYPOINT ["/usr/bin/redis-server"]

Mongodb image of the Dockerfile image is, docker official to the mongodb docker of the tutorial, I used directly to see, see Dockerizing MongoDB :

Format: FROM repository [: version]

FROM ubuntu: 14.04

Format: MAINTAINER Name < email@addr.ess >

MAINTAINER duqi duqi@example.com

Installation:

Import MongoDB public GPG key AND create a MongoDB list file

RUN apt-key adv –keyserver hkp: //keyserver.ubuntu.com: 80 –recv 7F0CEB10
RUN echo & quot; deb http://repo.mongodb.org/apt/ubuntu & quot; $ (lsb_release -sc) & quot; /mongodb-org/3.0 multiverse & quot; | tee /etc/apt/sources.list.d/mongodb -org-3.0.list

Update apt-get sources AND install MongoDB

RUN apt-get update & amp; & amp; apt-get install -y mongodb-org

Create the MongoDB data directory

RUN mkdir -p / data / db

Expose port 27017 from the container to the host

EXPOSE 27017

Set usr / bin / mongod as the dockerized entry-point application

ENTRYPOINT [& quot; / usr / bin / mongod & quot;]

Use docker-compose to arrange three services, the specific template file is as follows:
web:
build: .
ports:
- &quot;49161:8080&quot;
links:
- redis
- mongodb
redis:
image: duqi/redis
ports:
- &quot;6379:6379&quot;
mongodb:
image: duqi/mongodb
ports:
- &quot;27017:27017&quot;

Architecture is relatively simple, the first block of the build, said the docker in the order "docker build.", Used to build the web image; ports this container will be 8080 port and the host (IP address is: 192.168.99.100) Of the 49161 correspondence. Because docker does not support the original osx, so the use of dock in the mac, is actually in the mac on a virtual machine (docker-machine) on the use of docker, this machine is 192.168.99.100 address. See also: Use the docker in mac

Links indicate the service to be connected, redis corresponds to the redis block below, mongodb corresponds to the mongodb block below. Redis and mongodb similar, the first description of the mirror to use, and then specify the port mapping.

So how do you run it?
1. Command docker-compose build , said the construction of web services, I used to compare soil, is to compile jar after the need to re-update the docker, elegant point should not be the case.
Docker-compose build
2. command docker-compose up , said the start web service, you can see mongodb, redis and web followed by start, start with docker ps view the current running container.
Docker ps

Special attention, in the configuration file to write redis and mongodb url, use the virtual machine address, that is, 192.168.99.100. For example, a configuration of redis should be: spring.redis.host = 192.168.99.100.

3. spring boot + mysql

Pull mysql mirror instructions: docker run --name db001 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -d mysql:5.7 , said the starting docker container name is db001, login password must be set, -d said set Mysql version.

My docker-compose template file is:

web:
build: .
ports:
- &quot;49161:8080&quot;
links:
- mysql
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: springbootcookbook
ports:
- &quot;3306:3306&quot;

The main content is similar to the previous, mainly on the mysql part, through environement to set up into the mysql container after the environment variable, that is, connect the database password MYSQL_ROOT_PASSWORD, the use of the database name MSYQL_DATABASE and so on.

Always want to write this article to be a summary, write to find a bit thin, for the docker I also need to learn the system, but for the above example, I have personally practiced, we have any questions can contact me.

Reference materials

1. Docker from entry to practice
2. Docker – Initialize mysql database with schema
3. Use Docker to build the foundation of the mysql application
4. Spring Boot with docker

A
Author introduction Du Qi, now working in Alibaba, R & D engineers, responsible for Yunos system cloud services – PMS (personal mobile services), the current research direction is Spring Boot, micro-service, Docker and other technologies. Love writing, advocate writing – driven learning, personal technology blog address: link

Heads up! This alert needs your attention, but it's not super important.