[spring boot 例子:https://github.com/sion567/spring-boot-demo/tree/master/hello-db]java
application.yml配置文件以下:mysql
spring: datasource: url: jdbc:mariadb://myddbb:3306/hello_db username: root password: root driver-class-name: org.mariadb.jdbc.Driver jpa: show-sql: true format_sql: true hibernate: ddl-auto: update database-platform: org.hibernate.dialect.MySQL5InnoDBDialect # LOGGING logging.file or logging.path logging: # file: ./logs/hello-db.log path: /app/logs
logback.xml配置文件以下:git
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.hibernate.SQL" level="DEBUG"/> </configuration>
工程目錄主要有2個文件,ooxx.jar和Dockerfile,其中Dockerfile 文件內容以下github
FROM java MAINTAINER demo RUN mkdir /app RUN mkdir /app/logs ADD hello-db-1.0-SNAPSHOT.jar /app/hello-db.jar RUN sh -c 'touch /app/hello-db.jar' VOLUME ["/app/logs"] WORKDIR /app ENV JAVA_OPTS="" ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar hello-db.jar" ]
root@lq-OptiPlex-755:/usr/local/docker_work/hello-db# ll
總用量 26656
drwxr-xr-x 2 root root 4096 3月 9 11:40 ./
drwxr-xr-x 4 root root 4096 3月 9 11:39 ../
-rw-r--r-- 1 root root 255 3月 9 11:39 Dockerfile
-rw-r--r-- 1 root root 27282044 3月 9 11:39 hello-db-1.0-SNAPSHOT.jarspring
root@lq-OptiPlex-755:/usr/local/docker_work/hello-db# docker build -t demo/hello-db:1 --rm=true .
Sending build context to Docker daemon 28.25 MB
Step 1/9 : FROM java
---> d23bdf5b1b1b
......
Removing intermediate container 279d1646a52d
Successfully built d4dd64348475
root@lq-OptiPlex-755:/usr/local/docker_work/hello-db# docker run --name h1 -v /tmp/logs:/app/logs --link=mariadb:myddbb d4dd64348475sql
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
......docker
2017-03-10 02:49:52.843 INFO 8 --- [main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2017-03-10 02:49:53.850 INFO 8 --- [main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2017-03-10 02:49:53.867 INFO 8 --- [main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: hotel
2017-03-10 02:49:53.868 INFO 8 --- [main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: hotel
2017-03-10 02:49:54.141 INFO 8 --- [main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: review
2017-03-10 02:49:54.142 INFO 8 --- [main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: review數據庫
......bash
程序啓動完成,而且建立了數據庫app
查看下數據庫
MariaDB [hello_db]> show tables;
+--------------------+
| Tables_in_hello_db |
+--------------------+
| hotel |
| review |
+--------------------+
2 rows in set (0.00 sec)
表自動生成了,證實數據庫鏈接上了。以前配置上使用的是myddbb,是容器運行時--link=mariadb:myddbb 保障的,這個參數就是告訴Docker容器須要使用「mariadb」容器,並將其別名命名爲myddbb,這樣在這兩個容器裏就能夠使用「myddbb」來做爲提供mariadb數據庫服務的機器名。
-v /tmp/logs:/app/logs,新增一個掛載,Dockerfile文件裏VOLUME ["/app/logs"]要與之對應,
查看下宿主機的/tmp/logs文件
lq@lq-OptiPlex-755:/tmp/logs$ ll
總用量 20
drwxr-xr-x 2 root root 4096 3月 10 10:46 ./
drwxrwxrwt 5 root root 4096 3月 10 10:50 ../
-rw-r--r-- 1 root root 12254 3月 10 10:49 spring.log
lq@lq-OptiPlex-755:/tmp/logs$
docker 下spring boot +db 就OK了,下面是工具的安裝:
安裝java(docker版)
root@lq-OptiPlex-755:/# docker search java
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
java Java is a concurrent, class-based, and obj... 1314 [OK]
anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.23 over... 193 [OK
直接docker pull java
安裝mariadb(docker版)
root@lq-OptiPlex-755:/# docker search mariadb
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mariadb MariaDB is a community-... 1200 [OK]
paintedfox/mariadb A docker image for r ... 29 [OK]
...
root@lq-OptiPlex-755:/# docker pull mariadb
Using default tag: latest
...
root@lq-OptiPlex-755:/# mkdir -p /data/db
root@lq-OptiPlex-755:/# docker run -v /data/db:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root --name mariadb -d mariadb:latest
368107de40e9b039b0e33eff4e605423e94650865ee4a1251a162309a1597562
root@lq-OptiPlex-755:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
368107de40e9 mariadb:latest "docker-entrypoint..." About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp mariadb
root@lq-OptiPlex-755:/# docker exec -it mariadb bash
root@368107de40e9:/# mysql -uroot -proot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.21-MariaDB-1~jessie mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> Create DATABASE IF NOT EXISTS hello_db default charset utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use hello_db
Database changed
MariaDB [hello_db]>
-v <宿主機目錄>:<容器目錄>
這個命令把宿主機一個目錄mount到container裏,剛纔使用sql建了數據庫,也會在這個目錄下
root@lq-OptiPlex-755:/data/db# ll
總用量 110656
drwxr-xr-x 5 999 docker 4096 3月 9 11:12 ./
drwxr-xr-x 4 root root 4096 3月 9 11:03 ../
-rw-rw---- 1 999 docker 16384 3月 9 11:04 aria_log.00000001
-rw-rw---- 1 999 docker 52 3月 9 11:04 aria_log_control
drwx------ 2 999 docker 4096 3月 9 11:12 hello_db/ -rw-rw---- 1 999 docker 12582912 3月 9 11:04 ibdata1 -rw-rw---- 1 999 docker 50331648 3月 9 11:04 ib_logfile0 -rw-rw---- 1 999 docker 50331648 3月 9 11:04 ib_logfile1 -rw-rw---- 1 999 docker 0 3月 9 11:04 multi-master.info drwx------ 2 999 docker 4096 3月 9 11:04 mysql/ drwx------ 2 999 docker 4096 3月 9 11:04 performance_schema/ -rw-rw---- 1 999 docker 24576 3月 9 11:04 tc.log root@lq-OptiPlex-755:/data/db#