实验

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": "",

 

 

 

 

热门相关:帝少夜宠:小甜妻,乖!   我真的是正派   学霸你女朋友掉了   龙皇武神   战斗就变强