1. 程式人生 > 實用技巧 >4,docker-ce的安裝

4,docker-ce的安裝

docker-ce的安裝:
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo   https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-edge
yum install docker-ce



進入正在進行的容器方法:

#! /bin/bash
# Use nsenter to access docker
docker_in() {
   Name_Id=$1
   PID=$(docker inspect -f "{{ .State.Pid }}" $Name_Id)
   nsenter --target $PID --mount --uts --ipc --net --pid
}
docker_in $1


資料卷容器
如果使用者需要在多個容器之間共享一些持續更新的資料,最簡單的方式是使用資料卷容器。資料卷容器也是一個容器,但是它的目的是專門用來提供資料卷供其他容器掛載。
首先,建立一個數據卷容器dbdata,並在其中建立一個數據卷掛載到/dbdata:
$ docker run -it -v /dbdata --name dbdata ubuntu
root@3ed94f279b6f:/#
檢視/dbdata目錄:
root@3ed94f279b6f:/# ls
bin  boot  dbdata  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run sbin  srv  sys  tmp  usr  var
然後,可以在其他容器中使用--volumes-from來掛載dbdata容器中的資料卷.
例如建立db1和db2兩個容器,並從dbdata容器掛載資料卷:
$ docker run -it --volumes-from dbdata --name db1 ubuntu
$ docker run -it --volumes-from dbdata --name db2 ubuntu
此時,容器db1和db2都掛載同一個資料捲到相同的/dbdata目錄。三個容器任何一方在該目錄下的寫入,其他容器都可以看到。
用--volumes-from引數所掛載資料卷的容器自身並不需要保持在執行狀態。
如果刪除了掛載的容器(包括dbdata、db1和db2),資料卷並不會被自動刪除。如果要刪除一個數據卷,必須在刪除最後一個還掛載著它的容器時顯式使用docker rm -v命令來指定同時刪除關聯的容器。



構建自己的映象:
1,手動構建映象(方式一):

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              cd5239a0906a        7 days ago          109 MB
docker.io/centos    latest              49f7960eb7e4        8 days ago          200 MB
[root@localhost ~]# docker run --name mynginx -it centos

[root@85eda84b5668 /]#
[root@85eda84b5668 /]# rpm -ivh http://download.fedoraproject.org/pub/epel/7/ppc64le/Packages/e/epel-release-7-11.noarch.rpm
Retrieving http://download.fedoraproject.org/pub/epel/7/ppc64le/Packages/e/epel-release-7-11.noarch.rpm
warning: /var/tmp/rpm-tmp.ue5mKo: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:epel-release-7-11                ################################# [100%]
[root@85eda84b5668 /]# yum install nginx
[root@85eda84b5668 /]# vi /etc/nginx/nginx.conf
新增前端啟動:daemon off ;
[root@85eda84b5668 /]# exit
exit
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
85eda84b5668        centos              "/bin/bash"         16 minutes ago      Exited (0) 46 seconds ago                       mynginx
[root@localhost ~]#
[root@localhost ~]# docker commit -m "My Nginx" 85eda84b5668 oldboy/mynginx:v1
sha256:0c843695d9c3e4215fd969d9c9a065c4683836204f6ba7186e1fb6b408b79a4d
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
oldboy/mynginx      v1                  0c843695d9c3        17 seconds ago      386 MB
docker.io/nginx     latest              cd5239a0906a        7 days ago          109 MB
docker.io/centos    latest              49f7960eb7e4        8 days ago          200 MB
[root@localhost ~]#
(oldboy 為倉庫的名稱,mynginx映象的名稱,v1打的一個標籤)
接下來啟動我做的這個映象:
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
oldboy/mynginx      v1                  0c843695d9c3        17 seconds ago      386 MB
docker.io/nginx     latest              cd5239a0906a        7 days ago          109 MB
docker.io/centos    latest              49f7960eb7e4        8 days ago          200 MB
[root@localhost ~]# docker run --name mynginxv1 -d -p 81:80 oldboy/mynginx:v1 nginx (這裡必須加v1不加的話他就會去找,oldboy/mynginx:laster的映象,找不到的話,就會去遠端下載,nginx表示的是啟動命令)
f79352f4de7c6e310c0425b9b9e57c6cecb146cdaf8c207ac7f8f4c1dab5ac2d
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
f79352f4de7c        oldboy/mynginx:v1   "nginx"             23 seconds ago      Up 23 seconds       0.0.0.0:81->80/tcp   mynginxv1
[root@localhost ~]#


[root@192 ~]# docker run -it --name volume-test1 -h nginx -v /data centos  #-h主機名
[root@nginx /]#
[root@192 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
614e14bf974c        centos              "/bin/bash"         15 minutes ago      Up 15 minutes                           volume-test1
[root@192 ~]# docker inspect 614e14bf974c  ##將本機的這個目錄掛載到docker的/data目錄.
"Mounts": [
            {
                "Type": "volume",
                "Name": "851887d1490066427879f14c0ea5295ee8eb56fac8d1dc725960b719fef63075",
                "Source": "/var/lib/docker/volumes/851887d1490066427879f14c0ea5295ee8eb56fac8d1dc725960b719fef63075/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
手動建立映象(把容器做成映象)
[root@192 _data]#
[root@192 _data]#
[root@192 _data]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ff6877fc518a        centos              "/bin/bash"         22 minutes ago      Up 22 minutes                           nginx-man
[root@192 _data]# docker commit -m "my-nginx" ff6877fc518a liuqi/my-my-nginx:v1
sha256:d51bd3c6a58fcbb3fc0bbd7e24c357ee01baf0a114ddbcadc9010039a78636ed
[root@192 _data]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ff6877fc518a        centos              "/bin/bash"         23 minutes ago      Up 23 minutes                           nginx-man
[root@192 _data]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
liuqi/my-my-nginx   v1                  d51bd3c6a58f        22 seconds ago      370 MB
docker.io/nginx     latest              5699ececb21c        6 days ago          109 MB
docker.io/centos    latest              49f7960eb7e4        4 weeks ago         200 MB
[root@192 _data]#

啟動手動建立的映象的容器
[root@192 _data]#
[root@192 _data]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
liuqi/my-my-nginx   v1                  d51bd3c6a58f        3 minutes ago       370 MB
docker.io/nginx     latest              5699ececb21c        6 days ago          109 MB
docker.io/centos    latest              49f7960eb7e4        4 weeks ago         200 MB
[root@192 _data]#
[root@192 _data]# docker run -d -p 92:80 liuqi/my-my-nginx  #必須加tag標記
Unable to find image 'liuqi/my-my-nginx:latest' locally
Trying to pull repository docker.io/liuqi/my-my-nginx ...
^C
[root@192 _data]# docker run -d -p 92:80 liuqi/my-my-nginx:v1
0ca5069c5b6dbc4175e644333bb81d6f61bea48a74af7b2e716c796666956243
[root@192 _data]#


[root@192 _data]#
[root@192 _data]# docker ps -l
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                     PORTS               NAMES
0ca5069c5b6d        liuqi/my-my-nginx:v1   "/bin/bash"         4 minutes ago       Exited (0) 4 minutes ago                       upbeat_saha
[root@192 _data]# docker commit -m "v2" 0ca5069c5b6d liuqi/my-nginx:v2
sha256:db15e701ff3d3f8c3a154507d466a6a16dced9adca46695037b18d4291c6b951
[root@192 _data]#
[root@192 _data]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
liuqi/my-nginx      v2                  db15e701ff3d        28 seconds ago      370 MB
liuqi/my-my-nginx   v1                  d51bd3c6a58f        10 minutes ago      370 MB
docker.io/nginx     latest              5699ececb21c        6 days ago          109 MB
docker.io/centos    latest              49f7960eb7e4        4 weeks ago         200 MB
[root@192 _data]#