Linux离线安装docker及nginx方法

1,892 阅读1分钟

背景

由于工作要求,需离线安装docker,在线方式将不再重复介绍,此版本适用于麒麟操作系统。

1.下载docker文件

下载地址:download.docker.com/linux/stati…

image.png

2.解压

下载到本地后,上传到服务器/opt/docker目录中,解压命令:

tar -zxvf docker-18.06.3-ce.tgz

3.将解压出来的docker文件复制到 /usr/bin/ 目录下

cp docker/* /usr/bin/

4. 进入 /etc/systemd/system/ 目录,并创建docker.service文件

[root@localhost java]# cd /etc/systemd/system/

[root@localhost system]# touch docker.service
  1. 打开docker.service文件,将以下内容复制
[root@localhost system]# vi 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 --selinux-enabled=false --insecure-registry=192.168.200.128
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
  1. 给docker.service文件添加执行权限
[root@localhost system]# chmod 777 /etc/systemd/system/docker.service 
  1. 重新加载配置文件(每次有修改docker.service文件时都要重新加载下)
[root@localhost system]# systemctl daemon-reload 
  1. 启动
[root@localhost system]# systemctl start docker
  1. 设置开机启动
[root@localhost system]# systemctl enable docker.service
  1. 查看docker状态
[root@localhost system]# systemctl status docker

image.png 出现以上表示docker 启动成功。

11.docker-compose 安装 下载地址:github.com/docker/comp…

12、下载好离线包,把离线包放在指定目录下就不需要配置环境变量

复制代码

mv docker-compose-linux-x86_64 /usr/local/bin/

//修改文件名
 mv docker-compose-linux-x86_64 docker-compose

//授权
sudo chmod +x /usr/local/bin/docker-compose

//查看安装是否成功
docker-compose -v

image.png

13.在docker中离线安装nginx容器

image.png 14

docker load -i 镜像名.tar

image.png 15.给镜像重名命

docker tag 镜像名id 命名的镜像名:版本

提示: latest:表示最新的,使用镜像名直接启动容器时默认使用该版本

docker tag docker:latest

image.png

在home下面创建分别创建以下四个目录

(1)挂载容器里面的配置,即nginx.conf

mkdir -p /home/nginx/conf 

(2)挂载容器里面的子配置,即nginx.conf里面include的配置文件

mkdir -p /home/nginx/conf.d

(3)挂载容器里面的代理的日志文件

mkdir -p /home/nginx/logs

(4)挂载容器里面的界面的访问

mkdir -p /home/nginx/html

注意;

挂载:即将宿主的文件和容器内部目录相关联,相互绑定,在宿主机内修改文件的话也随之修改容器内部文件 启动nginx(先不挂载nginx.con配置文件) 启动nginx默认配置

docker run -d -p 80:80 --name nginx -v /home/nginx/logs:/var/log/nginx nginx

docker ps 查看正在运行的容器 docker ps -a 查询所有容器(正在运行和其他状态的)

image.png 出现以上内容表示启动成功

使用如下命令进入交互式终端:

docker exec -it nginx /bin/bash

1.进入后可以看到在nginx的容器中:

/etc/nginx/目录下存在nginx.conf,

/etc/nginx/conf.d/目录下存在default.conf文件

image.png

退出容器,使用下面的命令,将容器中的两个文件拷贝到我们前面准备的指定挂载目录中

docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf/
docker cp nginx:/etc/nginx/conf.d/default.conf /home/nginx/conf.d/

停止和删除正在运行的nginx容器

docker stop nginx

docker rm -f nginx

编辑启动脚本使用挂载目录的方式去启动nginx容器:


#!/bin/bash

docker run -d -p 80:80 --name nginx -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/conf.d:/etc/nginx/conf.d -v /home/nginx/logs:/var/log/nginx -v /home/nginx/html:/usr/share/nginx/html nginx:latest

命令解读: run:启动一个docker容器

name:设置启动后容器的名称

d: 后台启动 p: 绑定别的端口 -p a:b 将宿主机器的a端口绑定到容器的b端口 -P 为随机绑定到端口

v : 挂载的内容 宿主机器的文件夹:容器的文件夹

在/usr/bin目录下新建docker-nginx.sh文件,将该内容全部复制到文件中。并将文件的权限修改为可执行。 编写docker-nginx.sh脚本

image.png image.png

使用脚本启动

image.png

随便写一个html页面放到/home/nginx/html下,否则会报403;(可以将nginx自带的index文件复制到这里)

前端项目发布

(1)上传dist文件

image.png

(2)在/home/nginx/conf.d/文件下编写配置默认配置文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    


  location /{
	  root /usr/share/nginx/html/dist;
          index  index.html index.htm;
	  try_files $uri $uri/ /index.html;

    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


image.png

以上为docker+nginx离线教程,其他中间安装类似,感谢docker离线镜像nginx docker离线安装部署 linux_mob64ca13f38b94的技术博客_51CTO博客提供的思路。