开始安装
docker安装
# 1、yum 包更新到最新
yum update
# 2、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
# 3、 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 4、 安装docker,出现输入的界面都按 y
yum install -y docker-ce
# 5、 查看docker版本,验证是否验证成功
docker -v
#启动docker
/bin/systemctl start docker.service
docker pull nginx
docker pull redis:5.0.3
docker pull java:8
docker pull mysql:5.7
配置mysql之前,希望大家先在自己的宿主机上安装MySQL,linux上保姆级的mysql安装教程,跟着做就可以了。
之所以这么做,是因为我希望将mysql数据存储在宿主机上,docker中只是配置一个mysql环境,如果将数据库文件存储在docker上,一旦删除容器(通过rm指令),所有的数据文件都会消失,很不安全。
mkdir -p /mnt/docker/mysql
cd /mnt/docker/mysql
run
有着[创建并运行]mysql容器的作用docker run -id \
-p 3307:3306 \
--name=mysql \
-v /mnt/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
mysql:5.7
接下来我们需要进入容器,开放权限
docker ps
查看容器是否已经运行
docker exec -it mysql bash
mysql -uroot -p
show databases;
use mysql ;
show tables;
select user,host from user;
(可能会报sql错误,加上``包裹字段即可)
update user set host ='%'where user ='root';
update user set host ='%'where user ='root' and host='localhost';
grant all privileges on *.* to root@'%' identified by 'root' with grant option;
)flush privileges;
exit
退出MySQL,再exit
退出MySQL的容器检查mysql的Docker容器是否配置成功,并查看其暴露出来的docker ip地址
docker logs -f mysql
看看有没有配置成功docker inspect mysql
→查看对应的mysql 的docker容器的ip地址172.17.0.2,请记住这个地址,下面打包后台应用需要用到。环境无误后我们需要导入我们的数据库文件blog.sql
/mnt/docker/mysql/data
目录和docker容器的/var/lib/mysql
地址之间实现了映射,因此,我们如果将blog.sql文件导入宿主机的该目录下,blog.sql同样也会出现在docker的对应目录下,具体操作如下:cd /mnt/docker/mysql/data
rz
docker exec -it mysql bash
cd /var/lib/mysql
ls
,理论上可以看到blog.sql也在该目录下。mysql -uroot -p
,输入密码,进入docker中的数据库create database blog;
exit
mysql -uroot -p blog < blog.sql;
use blog;
source blog.sql;
docker run -id --name=redis -p 6379:6379 redis:5.0.3
docker inspect redis
,后面打包项目时要用,我这里就是172.17.0.3首先,我们需要在idea上配置参数并且打包
接下来,我们需要在 /mnt/docker/app目录下导入jar包,构建镜像的配置文件并运行镜像。
mkdir /mnt/docker/app
yum -y install lrzsz
cd /mnt/docker/app
rz
mv blog-api-1.0-SNAPSHOT.jar blog_api.jar
touch blog_dockerfile
vim blog_dockerfile
FROM java:8
MAINTAINER zhangshidi
ADD ./blog_api.jar /app.jar
CMD java -jar /app.jar --spring.profiles.active=prod
docker build -f ./blog_dockerfile -t app .
docker images
应该可以看到app的docker已经在运行了修改前端的配置,打包,上传,并微调文件位置。
#获取安装列表 yum安装列表中搜索zip/unzip是否存在
yum list | grep zip/unzip
#执行安装支持zip命令 根据提示输入y允许安装
yum install zip
#执行安装支持unzip命令 根据提示输入y允许安装
yum install unzip
mkdir /mnt/zhang
mkdir /mnt/zhang/blog
cd /mnt/zhang/blog
rz
,把我们的前端dist压缩包上传上去unzip dist.zip
ls
,应该是以下三个文件:dist.zip,index.html,static,其实有用的应该就是index.html以及static
首选,我们安装Docker Compose
# Compose目前已经完全支持Linux、Mac OS和Windows,在我们安装Compose之前,需要先安装Docker。下面我 们以编译好的二进制包方式安装在Linux系统中。
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 设置文件可执行权限
chmod +x /usr/local/bin/docker-compose
# 查看版本信息
docker-compose -version
创建对应的目录,并且把docker-compose.yml配置文件写好
vim
是修改文件的意思mkdir /mnt/docker/docker-compose
cd /mnt/docker/docker-compose
touch docker-compose.yml
vim docker-compose.yml
:wq
,就可以保存并退出~version: '3'
services:
nginx:
image: nginx
container_name: nginx
ports:
- 80:80
- 443:443
links:
- app
depends_on:
- app
volumes:
- /mnt/docker/docker-compose/nginx/:/etc/nginx/
- /mnt/zhang/web:/zhang/web
- /mnt/zhang/blog:/zhang/blog
network_mode: "bridge"
app:
image: app
container_name: app
expose:
- "8888"
network_mode: "bridge"
接下来,我们需要配置nginx的文件目录以及内部的相关配置文件
nginx.conf
文件mkdir -p ./nginx
cd /mnt/docker/nginx
touch nginx.conf
vim nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
blog.conf
文件mkdir conf.d
cd conf.d
touch blog.conf
vim blog.conf
修改配置,将以下文件放入到blog.conf
中
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_vary off;
upstream appstream{
server app:8888;
}
server{
# 监听端口
listen 80;
# 主机名称/地址
server_name www.zhangshidi.space zhangshidi.space;
index index.html;
# 前端服务
location / {
root /zhang/blog/;
# 刷新页面后跳转到当前页面
try_files $uri $uri/ /index.html;
index index.html;
}
# 后端api
location /api {
proxy_pass http://appstream;
}
location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$ {
root /zhang/blog/;
index index.html;
add_header Access-Control-Allow-Origin *;
}
}
mime.types
mime.types
文件然后rz
进去的,大家也可以直接新建文件把代码复制进去,代码如下。
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
font/woff woff;
font/woff2 woff2;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
docker-compose up #直接启动
docker-compose up -d #代表后台启动
docker-compose down #停止并删除容器
docker-compose start #启动已有容器
docker-compose stop #停止运行的容器
docker-compose up
直接启动看看网站有没有问题,如果有问题,通过以下两个指令来完善。
docker logs nginx
docker-compose logs
感谢你的来访,如果觉得写的不错,麻烦点个赞鼓励一下,有问题也请留言,欢迎指出问题。