Docker基本操作
1. 对已启动容器进行文件挂载
<1> 关闭容器
<2> 切换Root用户跳转到容器信息目录:cd /var/lib/docker/containers/容器id/
<3> vi hostconfig.json 修改如下,将容器目录/import绑定到主机/data目录
"Binds": ["data:/import"]
<4>vi config.v2.json,修改如下,添加MountPoints:
"/import": {
"Source": "/data",
"Destination": "/import",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/data",
"Target": "/import"
},
"SkipMountpointCreation": false
}
<5>启动容器
2. Docker启动nginx容器并挂载配置文件
命令:
docker run -v /usr/local/nginx/default.conf:/etc/nginx/conf.d/default.conf \
-p 80:9443 \
--name nginx \
--restart always \
-d nginx
如果想使用80端口转发则配置文件命名default.conf不可修改,需要此文件覆盖容器中的相同文件
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /api/basic_management {
proxy_pass: http://10.22.30.20:31734
}
location /api/data_center {
proxy_pass: http://10.25.1.184:9000
}
location /api/digital-bank-web {
proxy_pass: http://10.25.1.184:8800
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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;
#}
}
3. 修改报错容器中的配置文件
# 查看容器报错原因
[root@pro nginx]# docker logs nginx
nginx: [emerg] unknown directive "gizp" in /etc/nginx/nginx.conf:29
# 在此目录找到nginx容器的配置文件
[root@pro nginx]# cd /var/lib/docker/overlay2/
[root@pro overlay2]# find ./ -name nginx.conf
./7baeb968df6b073708cce37a182cf54fd033023a5eda6bb6d1077438d950ce6e/diff/etc/nginx/nginx.conf
# 将文件修改正确
[root@pro overlay2]# vim ./7baeb968df6b073708cce37a182cf54fd033023a5eda6bb6d1077438d950ce6e/diff/etc/nginx/nginx.conf
# 重启容器
[root@pro overlay2]# docker restart nginx
nginx
二. 文件拷贝
1. 从容器中拷贝文件到宿主机
执行命令格式:docker cp 容器名/容器ID:容器里面的文件路径 宿主机对应的路径
2.从宿主机拷贝文件到容器
执行命令格式:docker cp 宿主机文件对应路径 容器名/容器ID:容器里面对应的路径
三.Centos7安装
1.更新本地YUM
执行命令:yum update -y
2.安装wget(已安装的可跳过)
安装命令:yum install wget
替换阿里镜像:wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
生成本地仓库缓存:yum makecache
3.安装docker
命令:yum install docker -y
初始化命令:
systemctl start docker #启动docker
systemctl enable docker #开机启动docker
systemctl status docker #查看docker状态