docker-web部署demo(springboot)思路
1. ⾸先构建⼀个 含有 jdk 的镜像 并将本机的⼀个⽂件夹挂载在这个镜像中
2. 再构建⼀个 mysql 的镜像 , 并进⾏相应的配置
3. 运⾏这两个镜像
实现
⼀. jdk镜像部分
1. 创建⼀个Dockerfile,创建基于centos7的含有jdk8的镜像
vim Dockerfile
from centos:centos7
#作者
maintainer  183********@163
#创建⽂件夹
run mkdir /usr/local/jdk
#⼯作空间
workdir /usr/local/jdk
#将本地的 jdk 上传并且 add 命令含有解压功能
#注意本地存放jdk的位置,我的是放在和Dockerfile⽂件同⼀级的⽬录下的
add /usr/local/jdk
#配置 jdk 的环境
env JAVA_HOME /usr/local/jdk/jdk1.8.0_211
mysql下载后的初次使用env JRE_HOME /usr/local/jdk/jdk1.8.0_211/jre
env PATH $JAVA_HOME/bin:$PATH
注意: Dockerfile与jdk是处于同⼀⽂件夹中的
2. 构建这个镜像
docker build -t nyist/centos7_jdk8:v1.0 .
构建前
构建后
3. 进⼊运⾏这个镜像,进⼊容器查看配置是否成功
docker run -it --name myjdk8.01 -v /root/Tools/jar:/var/lib/jar -p 8090:8080 nyist/centos7_jdk8:v1.0 /bin/bash 解释说明:
-it 以前台的⽅式启动运⾏
--name ⾃定义容器的名称
-v 将本地的⽂件夹与容器中的⽂件夹挂载上
-p 容器暴露的端⼝号
执⾏完毕
进⼊容器成功,构建的jdk运⾏正常
容器外的⽂件夹挂载正常
4. 将⽬标⽂件拷贝⼀份到容器中
cp /var/lib/jar/zk-0.0.1-SNAPSHOT.jar /usr/local/jdk/
拷贝前
拷贝后
这个⼩demo中的mysql配置⽂件如下:
5.现在就可以运⾏这个⼩demo了
java -jar zk-0.0.1-SNAPSHOT.jar
此时外部端⼝与容器中的8080就是⼀⼀对应的状态了
注意:现在还不能访问,因为数据库还没有配置完成
⼆. 数据库 mysql 的配置
1. 直接 pull ⼀个 mysql 的相应版本( 5.7 )
docker pull mysql:5.7
2. 启动此镜像,暴露相应端⼝
docker run -d --name mysql5.7 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7解释:
-e  MYSQL_ROOT_PASSWORD=123456
作⽤是设置 mysql 的启动初始密码
执⾏结果:
进⼊并查看结果:
初次进⼊ mysql 的时候需要修改初始密码
alter user 'root'@'localhost' identified by '123456';
设置远程登录(在本地的 navicat 连接⼯具登录)
ALTER  USER  'root'  IDENTIFIED  WITH  mysql_native_password  BY  '123456';
修改docker中mysql的配置⽂件,设置⽂件格式为 utf-8
将docker中的mysql配置⽂件拷贝到本地
docker cp mysql5.7:/etc/f.d/mysqldf /root/mysqldf
在本地修改此⽂件:内容如下
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the # GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# The MySQL  Server configuration file.
#
# For explanations see
# sql/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir        = /var/lib/mysql
log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address  = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
character_set_server=utf8
init_connect='SET NAMES utf8'
max_allowed_packet = 20M
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。