第一步,安装docker
- 下载docker安装文件
https://download.docker.com/linux/static/stable/x86_64/ - 将文件上传至内网服务器,解压
tar -xvf docker-19.03.6.tar - 将解压的文件cp到/usr/bin/
cp docker/* /usr/bin/ - 将docker配置为服务。开机启动
vim /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
- 默认安装完成 docker 后,所有 images 及相关信息存储位置为:/var/lib/docker,比如每个容器的日志默认都会以 json-file 的格式存储于 /var/lib/docker/containers/<容器id>/<容器id>-json.log 里面。一般情况,/var 目录是在根分区之下,而根分区之下的磁盘空间一般不会较大,所以在生产环境中,经常会因为这个原因导致磁盘空间不足,然后服务或者消息中间件之类的崩掉。
①在 /data 目录下创建 docker 目录
②修改 docker.service 文件,在里面的EXECStart的后面增加 --graph 选项指定新目录:ExecStart=/usr/bin/dockerd --graph /data/docker
/data 为新添加的磁盘分区的挂载点
chmod +x /etc/systemd/system/docker.service #添加文件权限
systemctl daemon-reload #重载unit配置文件
systemctl start docker #启动Docker
systemctl enable docker.service #设置开机自启
systemctl status docker #查看Docker状态
docker -v #查看Docker版本
第二步,安装Django服务依赖的镜像
- 将本地测试好的镜像文件导出
docker save test_web:latest -o test_web.tar #将java 8的镜像导出成tar文件 - 将导出的镜像文件上传至服务器,docker load -i test_web.tar
- docker images 查看镜像是否已经导入成功,可能会出现镜像导入成功了。但镜像名称和tag都是none的情况,这时需要手动写入镜像名称和tag:docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签),例子:docker tag ccbcea8a6757 test:test
第三步,安装docker-compose
下载地址:https://github.com/docker/compose/releases
下载之后,移动到/usr/local/bin/下,重命名,加权限:
cd /usr/local/bin
mv docker-compose-Linux-x86_64 docker-compose
sudo chmod +x docker-compose
第四步,安装nginx
- 下载安装包:http://nginx.org/packages/centos/7/x86_64/RPMS/
- 将下载的安装包上传至服务器,运行:yum install -y nginx-1.16.1-1.el7.ngx.x86_64.rpm
- systemctl start nginx.service 启动nginx
systemctl enable nginx.service 设置开机启动
systemctl status nginx.service 查看nginx状态 - 如果访问服务器没有出现nginx页面,则检查防火墙是否开启80端口, 或者关闭防火墙:
1.查看已开放的端口(默认不开放任何端口)
firewall-cmd --list-ports
2.开启80端口
firewall-cmd --zone=public(作用域) --add-port=80/tcp(端口和访问类型) --permanent(永久生效)
3.重启防火墙
firewall-cmd --reload
4.停止防火墙
systemctl stop firewalld.service
5.禁止防火墙开机启动
systemctl disable firewalld.service
6.删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent
lsof -i 查看端口占用
查看防火墙状态:systemctl status firewalld running即已开启
开始防火墙:systemctl start firewalld 没有任何提示即开启成功
关闭防火墙: systemctl stop firewalld
- 配置nginx.conf, 目录:/etc/nginx/nginx.conf
在该文件中,增加:include /etc/nginx/conf.d/*.conf;
创建/etc/nginx/conf.d/目录。在此目录下配置自己的服务的nginx
vim /etc/nginx/conf.d/test.conf
server {
listen 80;
location /test/ {
proxy_pass http://177.77.77.77:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
proxy_pass_header X-CSRFToken;
}
}
nginx -t 测试自己添加的配置格式是否正确
nginx -s reload 加载配置