實驗

q_7發表於2024-04-22

1:在離線的環境中匯入映象

在無法訪問外網的情況下,透過將docker映象匯出為一個包,然後匯入到另外的一臺計算機上面,從而實現了不用訪問外網就能拉取映象了

#將映象輸出到這個tar包
[root@cleint ~]# docker save -o centos.tar centos

#透過第三方的工具將這個tar包傳輸到其他機器上面,然後進行匯入
[root@cleint ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@cleint ~]# ls
anaconda-ks.cfg  centos.tar  docker-hello  project  qcy-centos
[root@cleint ~]# docker load -i centos.tar 
Loaded image: centos:latest
[root@cleint ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   2 years ago   231MB

#因此實現了不能訪問外網的情況下,拉取映象了

2:使用dockercommit命令基於容器構建映象

有一個容器做成了web頁面,所有的虛擬機器都想要這個web頁面的功能,直接基於這個提供web頁面的容器做成一個映象,然後虛擬機器直接基於這個web容器映象構建一個容器,從而提供了web頁面

#搭建一個提供web頁面的容器
[root@cleint ~]# docker run -tid --name 8080-nginx -p 8080:80 nginx /bin/bash
a9161cf2c12713f4ec492b2e9c32aa75d1c8556457db07f33689282adcaf4d9c

#進行訪問
[root@cleint ~]# curl http://127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


#基於這個容器做成一個映象
[root@cleint ~]# docker commit 8080-nginx  web-nginx
sha256:c072b790098d75f0082adb87b0abc0d9757c4db74f1ce9018854cae7ae22dccf
[root@cleint ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
web-nginx    latest    c072b790098d   3 seconds ago   187MB
nginx        latest    92b11f67642b   6 weeks ago     187MB
centos       latest    5d0da3dc9764   2 years ago     231MB

[root@cleint ~]# docker run -tid --name q1 -p 7777:80 web-nginx /bin/bash
a66b1611c6cea9276f904f5fb410f632d325066939c72e221906e6607f0ef3b8
[root@cleint ~]# docker ps 
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                   NAMES
a66b1611c6ce   web-nginx   "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds   0.0.0.0:7777->80/tcp, :::7777->80/tcp   q1
a9161cf2c127   nginx       "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   8080-nginx
#進入這個容器裡面,需要開啟這個容器的服務nginx
#進行訪問
[root@cleint ~]# curl http://127.0.0.1:7777
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#從而實現了基於這個已有的容器做成一個映象,然後再來生成一個容器,這個容器會保留之前所做的一些操作  

3:使用第三方容器倉庫

使用第三方的倉庫,進行上傳映象,下載映象

這個非常的遍歷,就是使用這個倉庫的話,因為網路的容器倉庫有的屬於國外的,所以的話拉取映象的速度就比較的慢,需要自己的倉庫即可

操作步驟:

#登入第三方的容器倉庫
docker login uhub.service.ucloud.cn 
username 郵箱
password 密碼

#對於要推送的映象打上標籤,只有標籤是一致的,才能實現上傳
#格式為 uhub.service.ucloud.cn這個
docker tag centos:latest uhub.service.ucloud.cn/admins/centos:latest

#然後拉取映象,從映象倉庫裡面進行拉取
[root@cleint yum.repos.d]# docker pull uhub.service.ucloud.cn/admins/centos:latest
latest: Pulling from admins/centos
Digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc
Status: Downloaded newer image for uhub.service.ucloud.cn/admins/centos:latest
uhub.service.ucloud.cn/admins/centos:latest

#檢視容器映象倉庫
可以使用skopeo這個命令進行檢視遠端的容器倉庫

4:使用docker build命令基於dockerfile構建映象

案例:使用dockerfile來構建一個映象,然後裡面有nginx.repo源,然後進行訪問即可

#首先建立一個nginx.repo檔案
#建立一個dockerfile檔案
[root@cleint dockerfile-test]# ls
dockerfile  nginx.repo
[root@cleint dockerfile-test]# pwd
/root/dockerfile-test
[root@cleint dockerfile-test]# cat dockerfile 
FROM centos
RUN rm -rf /etc/yum.repos.d/*.repo 
COPY ./nginx.repo  /etc/yum.repos.d/
RUN  yum clean all && yum makecache
RUN yum -y install nginx
RUN echo "Hello this is nginx server" > /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
[root@cleint dockerfile-test]# cat nginx.repo 
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/$releasever/$basearch
gpgcheck=0
enabled=1

#然後使用dockerfile來建立一個映象
[root@cleint dockerfile-test]# docker build  -t centos-nginx .

#建立一個容器
[root@cleint dockerfile-test]# docker run -tid --name c1  -p 8080:80 centos-nginx
110106833b9569158312a08a32898da18af450d93ab135c080a23337486c06b1
[root@cleint dockerfile-test]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS        PORTS                                   NAMES
110106833b95   centos-nginx   "nginx -g 'daemon of…"   2 seconds ago   Up 1 second   0.0.0.0:8080->80/tcp, :::8080->80/tcp   c1

#訪問
[root@cleint dockerfile-test]# curl 0.0.0.0:8080
Hello this is nginx server

5:當容器內部無法進行上網的操作

yum -y install  autoconf
yum -y install gcc
wget  http://mirrors.edge.kernel.org/pub/linux/utils/net/bridge-utils/bridge-utils-1.6.tar.gz
tar  -xvf bridge-utils-1.6.tar.gz 
cd bridge-utils-1.6/
autoconf 
./configure 
make
make install
systemctl stop docker
ip link  set dev docker0 down
brctl delbr docker0
systemctl restart docker

6:建立一個自定義的橋接網路

網段為192.168.107.0/24 閘道器為192.168.107.254

[root@cleint dockerfile-test]# docker network create  --driver bridge  --subnet 192.168.107.0/24 --gateway 192.168.107.254 n1
8aec3d57d238b34eb6e0cb31b71775511bddfefebe8f87cb309150909c1fc646
[root@cleint dockerfile-test]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
d1ea2ab3dab8   bridge    bridge    local
5f3fcf22894d   host      host      local
8aec3d57d238   n1        bridge    local
b99add30e794   none      null      local
[root@cleint dockerfile-test]# docker inspect n1
[
    {
        "Name": "n1",
        "Id": "8aec3d57d238b34eb6e0cb31b71775511bddfefebe8f87cb309150909c1fc646",
        "Created": "2024-04-23T20:06:37.31247228+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.107.0/24",
                    "Gateway": "192.168.107.254"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
#建立一個容器,連線到這個n1的橋接網路卡上面
[root@cleint dockerfile-test]# docker run -tid --name q1 --network n1  centos /bin/bash
d098c15e25afd7d39a672f8193a658249016cfecd3e62f309cae4c7a317b94a9
[root@cleint dockerfile-test]# docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
d098c15e25af   centos         "/bin/bash"              3 seconds ago   Up 2 seconds                                           q1
110106833b95   centos-nginx   "nginx -g 'daemon of…"   6 minutes ago   Up 6 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   c1
[root@cleint dockerfile-test]# docker exec -ti q1 /bin/bash
[root@d098c15e25af /]# ping www.baidu.com
PING www.a.shifen.com (183.2.172.42) 56(84) bytes of data.
64 bytes from 183.2.172.42 (183.2.172.42): icmp_seq=1 ttl=127 time=48.10 ms
^C
--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 48.952/48.952/48.952/0.000 ms
#檢視容器的ip資訊
[root@cleint dockerfile-test]# docker inspect q1 | grep  -i address
            "LinkLocalIPv6Address": "",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "IPAddress": "",
            "MacAddress": "",
                    "MacAddress": "02:42:c0:a8:6b:01",
                    "IPAddress": "192.168.107.1",
                    "GlobalIPv6Address": "",

7:建立一個卷,並讓一個容器掛載

[root@docker ~]# docker volume  create  v1
v1
[root@docker ~]# docker run -tid --name q1 -v v1:/opt/data centos /bin/bash
c57bce59a329cc750bc9c4b1ea4985fa16806d7610cfcfd16b9b547d0846d856
[root@docker ~]# docker volume  inspect  v1
[
    {
        "CreatedAt": "2024-04-25T09:09:43+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/v1/_data",
        "Name": "v1",
        "Options": null,
        "Scope": "local"
    }
]