1. 程式人生 > 實用技巧 >Docker基本操作

Docker基本操作

一.Dock基本操作

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狀態