不指定版本的话默认拉取最新版,这里指定mysql5.7版本。
docker pull mysql:5.7
# -p 3306:3306 : 指定端口映射
# --name mysql : 容器别名
# -e MYSQL_ROOT_PASSWORD=my-pwd1 :参数设置,配置mysql密码
# -d : 容器后台运行
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=my-pwd1 -d mysql:5.7
或者使用数据卷挂载数据、配置和日志
docker run -p 3306:3306 --name mysql \
-v /usr/local/mysql/log:/var/log/mysql \
-v /usr/local/mysql/data:/var/lib/mysql \
-v /usr/local/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
命令解释:
-p
3306:3306:指定宿主机端口与容器端口映射关系--name
mysql:创建的容器名称--restart=always
:总是跟随docker启动--privileged=true
:获取宿主机root权限-v
/usr/local/mysql/log:/var/log/mysql:映射日志目录,宿主机:容器-v
/usr/local/mysql/data:/var/lib/mysql:映射数据目录,宿主机:容器-v
/usr/local/mysql/conf:/etc/mysql:映射配置目录,宿主机:容器-v
/etc/localtime:/etc/localtime:ro:让容器的时钟与宿主机时钟同步,避免时区的问题,ro是read only的意思,就是只读。-e MYSQL_ROOT_PASSWORD=123456
:指定mysql环境变量,root用户的密码为123456-d
mysql:5.7:后台运行mysql容器,版本是5.7docker exec -it mysql bash
mysql -u root -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
选择mysql库
use mysql;
查看用户权限
select host, user, plugin, authentication_string, password_expired from user;
结果如下:
mysql> select user, host, plugin, authentication_string, password_expired from user;
+---------------+-----------+-----------------------+-------------------------------------------+------------------+
| user | host | plugin | authentication_string | password_expired |
+---------------+-----------+-----------------------+-------------------------------------------+------------------+
| root | localhost | mysql_native_password | *52D9199692F024F4BCA62B7EF35D4E205589C7D6 | N |
| mysql.session | localhost | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N |
| mysql.sys | localhost | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N |
| root | % | mysql_native_password | *52D9199692F024F4BCA62B7EF35D4E205589C7D6 | N |
+---------------+-----------+-----------------------+-------------------------------------------+------------------+
user
:代表用户名
host
:代表允许登录的ip地址,%代表此用户在所有地址均可登录,localhost代表只能本地登录。
如果root
的host
字段不为%
,可以修改登录权限
#任何主机均可连接
ALTER USER root@'%' IDENTIFIED WITH mysql_native_password BY 'password';
#指定主机可以连接
ALTER USER root@'192.168.111.222' IDENTIFIED WITH mysql_native_password BY 'password';
-- 刷新权限
FLUSH PRIVILEGES;
使用数据库连接工具即可连接。
下面将部署springboot
项目,项目中使用的数据库就是上述部署的数据库,连接如下:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.154.128:3306/demo?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=SW0087
使用maven工具,或在终端执行:
mvn clean package -Dmaven.test.skip=true
将生成的jar文件放在一个单独的目录中,例如/usr/local/myapp/
新建Dokcerfile文件,
touch Dokcerfile
注
:Dockerfile
必须和jar
包在同一个文件,文件下尽量不要带其他内容,因为上传制作时会将该目录下所有内容上传。且文件名必须是Dockerfile
。
# Docker image for springboot file run
# VERSION 0.0.1
# Author:
# 基础镜像使用java
FROM openjdk:8
# 作者
MAINTAINER shallwe
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
#VOLUME /tmp
# 将当前目录下的jar包添加到容器根目录中并更名为app.jar
ADD ./demo.jar /app.jar
# 运行jar包
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
#相当于执行 java -jar /app.jar,
#-Djava.security.egd=file:/dev/./urandom 是一种安全保障,可以不用。
# 镜像名称为:my-aqq:v1.0.0
docker build -t my-aqq:v1.0.0 .
注
:注意命令后面的.
,代表镜像目录为当前目录,制作时间比较久。
查看镜像:
[root@shallwe ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-app v1.0.0 60197d7a9f4c 15 hours ago 551MB
busybox latest beae173ccac6 20 months ago 1.24MB
mysql 5.7 c20987f18b13 20 months ago 448MB
[root@shallwe ~]# docker run -p 8088:8088 -d my-app:v1.0.0
832eff082e901d2a6496a7c860b88b24ef9cb625a277c66c2fb31763c928e8c6
#查看日志
[root@shallwe ~]# docker logs -f -t --tail 100 83
如下所示,已正常启动
2023-08-23T03:06:37.309719422Z
2023-08-23T03:06:37.309871407Z . ____ _ __ _ _
2023-08-23T03:06:37.309876568Z /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2023-08-23T03:06:37.309879281Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2023-08-23T03:06:37.309881371Z \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2023-08-23T03:06:37.309883356Z ' |____| .__|_| |_|_| |_\__, | / / / /
2023-08-23T03:06:37.309885282Z =========|_|==============|___/=/_/_/_/
2023-08-23T03:06:37.309887132Z :: Spring Boot :: (v2.1.7.RELEASE)
2023-08-23T03:06:37.309889038Z
2023-08-23T03:06:37.962829665Z 2023-08-23 03:06:37.939 INFO 1 --- [ main] com.example.demo1.Demo1Application : Starting Demo1Application v0.0.1-SNAPSHOT on 832eff082e90 with PID 1 (/app.jar started by root in /)
2023-08-23T03:06:37.962853436Z 2023-08-23 03:06:37.958 INFO 1 --- [ main] com.example.demo1.Demo1Application : No active profile set, falling back to default profiles: default
2023-08-23T03:06:44.506872511Z 2023-08-23 03:06:44.506 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8088 (http)
2023-08-23T03:06:44.590091858Z 2023-08-23 03:06:44.589 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-08-23T03:06:44.590114177Z 2023-08-23 03:06:44.589 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2023-08-23T03:06:44.847843029Z 2023-08-23 03:06:44.845 INFO 1 --- [ main] o.a.c.c.C.[.[.[/my-project/v1] : Initializing Spring embedded WebApplicationContext
2023-08-23T03:06:44.847866028Z 2023-08-23 03:06:44.845 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6697 ms
2023-08-23T03:06:45.322776597Z Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
2023-08-23T03:06:45.510722277Z Property 'mapperLocations' was not specified.
2023-08-23T03:06:45.521660281Z _ _ |_ _ _|_. ___ _ | _
2023-08-23T03:06:45.521681877Z | | |\/|_)(_| | |_\ |_)||_|_\
2023-08-23T03:06:45.521684988Z / |
2023-08-23T03:06:45.531705230Z 3.4.2
2023-08-23T03:06:46.717197450Z 2023-08-23 03:06:46.715 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-08-23T03:06:47.515086706Z 2023-08-23 03:06:47.504 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path '/my-project/v1'
2023-08-23T03:06:47.515113532Z 2023-08-23 03:06:47.509 INFO 1 --- [ main] com.example.demo1.Demo1Application : Started Demo1Application in 11.539 seconds (JVM running for 12.882)