当前位置:   article > 正文

docker镜像和容器操作、四种网络模式介绍_镜像中添加命令怎么设置

镜像中添加命令怎么设置

目录

一.镜像操作

1.搜索官方仓库镜像

2.拉取镜像

3.查看当前主机镜像列表

4.导出镜像

5.删除镜像

6.导入镜像

二.容器操作

1.启动容器

(1)docker run 镜像名,container可省略

(2)可选参数

2.关闭容器

3.激活关闭的容器

(1)docker start/restart 容器名

(2)可选参数

4.查看容器详细信息

5.测试容器并查看日志

6.对运行中的容器执行操作

(1)进入正在运行的容器

(2)可选参数

7.删除容器

8.管理容器内数据

9.主机与容器内文件的拷贝

三.四种网络模式

1.bridge模式

2.host模式

3.container模式

4.none模式简介

四.容器和镜像仓库练习


 

 

一.镜像操作

1.搜索官方仓库镜像

(1)示例

  1. [root@localhost ~]# docker search httpd
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. httpd The Apache HTTP Server Project 4512 [OK]
  4. clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 5
  5. paketobuildpacks/httpd 0
  6. vulhub/httpd 0
  7. jitesoft/httpd Apache httpd on Alpine linux. 0
  8. openquantumsafe/httpd Demo of post-quantum cryptography in Apache … 0
  9. wodby/httpd 0
  10. avenga/httpd-static 0
  11. dockette/httpdump 0
  12. betterweb/httpd 0
  13. dockette/apache Apache / HTTPD 1 [OK]
  14. centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 45
  15. manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
  16. centos/httpd-24-centos8 1
  17. dockerpinata/httpd 1
  18. 19022021/httpd-connection_test This httpd image will test the connectivity … 0
  19. publici/httpd httpd:latest 1 [OK]
  20. centos/httpd 36 [OK]
  21. httpdocker/kubia 0
  22. e2eteam/httpd 0
  23. manasip/httpd 0
  24. httpdss/archerysec ArcherySec repository 0 [OK]
  25. solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
  26. patrickha/httpd-err 0
  27. hypoport/httpd-cgi httpd-cgi 2 [OK]

(2)可选参数

--automated只列出automated build类型的镜像
--no-trunc显示完整的镜像描述
-f <匹配条件>只列出收藏数不小于条件的镜像

(3)各列参数含义

NAME镜像名称
DESCRIPTION镜像说明
STARS点赞数量,越多越受欢迎
OFFICIALOK表示是docker官方发布的
AUTOMATED是否是自动构建的

2.拉取镜像

(1)docker pull从docker镜像仓库获取镜像,添加-a参数表示拉去所有匹配的镜像

  1. [root@localhost ~]# docker pull nginx:1.14-alpine #alpine表示构建容器小镜像的发行版本
  2. 1.14-alpine: Pulling from library/nginx
  3. bdf0201b3a05: Pull complete
  4. 3d0a573c81ed: Pull complete
  5. 8129faeb2eb6: Pull complete
  6. 3dc99f571daf: Pull complete
  7. Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
  8. Status: Downloaded newer image for nginx:1.14-alpine
  9. docker.io/library/nginx:1.14-alpine

(2)标记本地镜像,将它放进某一仓库

[root@localhost ~]# docker tag nginx:1.14-alpine /data/nginx:v1   #tag指定镜像标签

(3)查看镜像层数

  1. [root@localhost ~]# docker history nginx:1.14-alpine
  2. IMAGE CREATED CREATED BY SIZE COMMENT
  3. 8a2fb25a19f5 4 years ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
  4. <missing> 4 years ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B
  5. <missing> 4 years ago /bin/sh -c #(nop) EXPOSE 80 0B
  6. <missing> 4 years ago /bin/sh -c #(nop) COPY file:ebf4f0eb33621cc0… 1.09kB
  7. <missing> 4 years ago /bin/sh -c #(nop) COPY file:4c82b9f10b84c567… 643B
  8. <missing> 4 years ago /bin/sh -c GPG_KEYS=B0F4253373F8F6F510D42178… 10.5MB
  9. <missing> 4 years ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.14.2 0B
  10. <missing> 4 years ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
  11. <missing> 4 years ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
  12. <missing> 4 years ago /bin/sh -c #(nop) ADD file:2e3a37883f56a4a27… 5.53MB

3.查看当前主机镜像列表

(1)查看,默认只显示顶层

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB
  4. [root@localhost ~]# docker image ls
  5. REPOSITORY TAG IMAGE ID CREATED SIZE
  6. nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB
  7. [root@localhost ~]# docker image ls nginx
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB
  10. [root@localhost ~]# docker image --help
  11. Usage: docker image COMMAND
  12. Manage images
  13. Commands:
  14. build Build an image from a Dockerfile
  15. history Show the history of an image
  16. import Import the contents from a tarball to create a filesystem image
  17. inspect Display detailed information on one or more images
  18. load Load an image from a tar archive or STDIN
  19. ls List images
  20. prune Remove unused images
  21. pull Download an image from a registry
  22. push Upload an image to a registry
  23. rm Remove one or more images
  24. save Save one or more images to a tar archive (streamed to STDOUT by default)
  25. tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
REPOSITORY镜像的仓库源
TAG镜像的标签
IMAGE ID镜像ID
CREATED镜像创建时间
SIZE镜像大小

镜像大小说明:docker image ls显示的是镜像拉去到本地后展开的大小(即展开后各层占空间总合),不是在docker hub中的压缩状态的大小,而且这个大小并非四=是实际消耗的硬盘空间大小。

(2)显示没有仓库名,没有标签的“虚悬镜像”,这些镜像可以放心删除

  1. [root@localhost ~]# docker image ls -f dangling=true
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. [root@localhost ~]# docker image prune #删除虚悬镜像

(3)显示包括中间层镜像在内的所有镜像,中间层镜像不能删除

[root@localhost ~]# docker image ls -a

(4)查看镜像的详细信息

[root@localhost ~]# docker image inspect nginx:1.14-alpine

(5)其他可选参数

-q只显示镜像ID
--no-trunc显示完整的镜像信息
--f <匹配条件>显示满足条件的镜像

4.导出镜像

使用“>”或“-o”参数指定导出镜像位置,需要导出的镜像可以为多个,在指定导出的文件名后缀为“.tar.gz”时可以让文件直接压缩,在需要指定多个镜像时就直接在save后面将多个镜像列出来即可。

  1. [root@localhost ~]# docker image save nginx > docker-nginx.tar.gz
  2. [root@localhost ~]# ll
  3. total 16860
  4. -rw-r--r-- 1 root root 0 Jul 23 09:59 abc
  5. -rw-------. 1 root root 1386 Jul 23 09:02 anaconda-ks.cfg
  6. -rw-r--r-- 1 root root 17258496 Aug 20 20:25 docker-nginx.tar.gz

5.删除镜像

(1)指定拉取时的名称,也可以通过ID删除

  1. [root@localhost ~]# docker image rm nginx:1.14-alpine
  2. #rm或rmi

(2)可选参数

-f强制删除
-l删除容器间的网络连接,并非容器本身
-v删除与容器关联的卷

6.导入镜像

 -i指定先前导出的镜像文件,可加“-q”参数精简输出信息

[root@localhost ~]# docker image load -i docker-nginx.tar.gz

二.容器操作

  1. [root@localhost ~]# docker container --help
  2. Usage: docker container COMMAND
  3. Manage containers
  4. Commands:
  5. attach Attach local standard input, output, and error streams to a running container
  6. commit Create a new image from a container's changes
  7. cp Copy files/folders between a container and the local filesystem
  8. create Create a new container
  9. diff Inspect changes to files or directories on a container's filesystem
  10. exec Execute a command in a running container
  11. export Export a container's filesystem as a tar archive
  12. inspect Display detailed information on one or more containers
  13. kill Kill one or more running containers
  14. logs Fetch the logs of a container
  15. ls List containers
  16. pause Pause all processes within one or more containers
  17. port List port mappings or a specific mapping for the container
  18. prune Remove all stopped containers
  19. rename Rename a container
  20. restart Restart one or more containers
  21. rm Remove one or more containers
  22. run Create and run a new container from an image
  23. start Start one or more stopped containers
  24. stats Display a live stream of container(s) resource usage statistics
  25. stop Stop one or more running containers
  26. top Display the running processes of a container
  27. unpause Unpause all processes within one or more containers
  28. update Update configuration of one or more containers
  29. wait Block until one or more containers stop, then print their exit codes
  30. Run 'docker container COMMAND --help' for more information on a command.

1.启动容器

(1)docker run 镜像名,container可省略

  1. [root@localhost ~]# docker container run -d -p 80:80 nginx:1.14-alpine
  2. d2a903c987ac122186f950b55fc5716564aec096f5ad2376e285853638ce718c
  3. [root@localhost ~]# docker ps #查询容器运行状态
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. d2a903c987ac nginx:1.14-alpine "nginx -g 'daemon of…" 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp dreamy_cerf
  6. [root@localhost ~]# docker port dreamy_cerf #查看docker端口映射,这个名字在启动时没有指定时会自动创建一个,通过docker ps查询
  7. 80/tcp -> 0.0.0.0:80
  8. 80/tcp -> [::]:80

注:容器内的第一个进程必须一致处于运行状态,否则容器将退出 

(2)可选参数

-t打开一个终端来使用容器
-i

交互式访问

-d后台运行容器,返回容器ID,不后台运行则会一直占用终端
-p端口映射,将容器内服务的端口映射在宿主机的指定端口,“宿主机端口:容器端口”
-P随机端口映射,容器内部端口随机映射到主机的端口
--name容器名字
--network指定网络
--rm容器停止即自动删除

2.关闭容器

docker stop/kill 容器名,kill可以像Linux杀进程一样使用信号控制(-s参数 信号)

3.激活关闭的容器

(1)docker start/restart 容器名

(2)可选参数

-a附加到当前终端
-i交互式激活进入

4.查看容器详细信息

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. d2a903c987ac nginx:1.14-alpine "nginx -g 'daemon of…" 24 minutes ago Up 24 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp dreamy_cerf
  4. [root@localhost ~]# docker inspect dreamy_cerf

5.测试容器并查看日志

  1. [root@localhost ~]# docker inspect dreamy_cerf | grep -i address
  2. "LinkLocalIPv6Address": "",
  3. "SecondaryIPAddresses": null,
  4. "SecondaryIPv6Addresses": null,
  5. "GlobalIPv6Address": "",
  6. "IPAddress": "172.17.0.2",
  7. "MacAddress": "02:42:ac:11:00:02",
  8. "IPAddress": "172.17.0.2",
  9. "GlobalIPv6Address": "",
  10. "MacAddress": "02:42:ac:11:00:02",
  11. [root@localhost ~]# curl 172.17.0.2
  12. <!DOCTYPE html>
  13. <html>
  14. <head>
  15. <title>Welcome to nginx!</title>
  16. <style>
  17. body {
  18. width: 35em;
  19. margin: 0 auto;
  20. font-family: Tahoma, Verdana, Arial, sans-serif;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h1>Welcome to nginx!</h1>
  26. <p>If you see this page, the nginx web server is successfully installed and
  27. working. Further configuration is required.</p>
  28. <p>For online documentation and support please refer to
  29. <a href="http://nginx.org/">nginx.org</a>.<br/>
  30. Commercial support is available at
  31. <a href="http://nginx.com/">nginx.com</a>.</p>
  32. <p><em>Thank you for using nginx.</em></p>
  33. </body>
  34. </html>
  35. [root@localhost ~]# docker logs dreamy_cerf
  36. 172.17.0.1 - - [20/Aug/2023:13:32:50 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
  37. 172.17.0.1 - - [20/Aug/2023:13:33:10 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
  38. 172.17.0.1 - - [20/Aug/2023:13:34:53 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
  39. 172.17.0.1 - - [20/Aug/2023:13:44:23 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

6.对运行中的容器执行操作

(1)进入正在运行的容器

docker exec 容器名

  1. [root@localhost ~]# docker exec -it dreamy_cerf /bin/sh #指定运行环境
  2. / # ls /
  3. bin etc lib mnt proc run srv tmp var
  4. dev home media opt root sbin sys usr
  5. / # exit #exit退出
  6. [root@localhost ~]# docker exec dreamy_cerf echo hello
  7. hello

(2)可选参数

-d在后台运行命令
-e设置环境变量
-i交互式
-t打开一个终端

7.删除容器

在删除正在运行的容器前需要先停止该容器

(1)docker kill 容器名

(2)docker rm (-f)容器名,(强制)删除容器

(3)docker rm -f  `docker ps -a -q`,强制删除所有容器

(4)docker container prune,删除处于终止状态的容器

8.管理容器内数据

数据卷——类似于Linux文件系统中目录的挂载,docker镜像中被指定为挂载点的目录里的文件会隐藏,只显示挂载着数据卷。数据卷在容器间共享,可重用,数据卷的修改动态会立即生效,其更新不会影响到镜像。

(1)创建数据卷

  1. [root@localhost ~]# docker volume create vol1
  2. vol1
  3. [root@localhost ~]# docker volume ls
  4. DRIVER VOLUME NAME
  5. local vol1

(2)查看指定数据卷的信息

  1. [root@localhost ~]# docker volume inspect vol1
  2. [
  3. {
  4. "CreatedAt": "2023-08-20T21:58:06+08:00",
  5. "Driver": "local",
  6. "Labels": null,
  7. "Mountpoint": "/var/lib/docker/volumes/vol1/_data",
  8. "Name": "vol1",
  9. "Options": null,
  10. "Scope": "local"
  11. }
  12. ]

(3)启动一个挂载数据卷的容器

数据卷本地目录挂载以及使用“--volumes-from”新容器从现有容器挂载已有数据卷

  1. #通过-v指定数据卷,可以在卷名前加上路径表示将卷挂载到本地目录
  2. #:前是本地目录路径,:后是容器目录路径,表示将两处的目录挂载到一起,可以实现共享
  3. [root@localhost docker1]# docker volume ls
  4. DRIVER VOLUME NAME
  5. local vol1
  6. local vol2
  7. [root@localhost docker1]# docker run -it -v /docker1:/vol2 nginx:1.14-alpine /bin/sh
  8. / # ls
  9. bin etc lib mnt proc run srv tmp var
  10. dev home media opt root sbin sys usr vol2
  11. [root@localhost docker1]# ll
  12. total 4
  13. -rw-r--r-- 1 root root 6 Aug 21 12:38 a.txt
  14. / # cd vol2/
  15. /vol2 # ls
  16. a.txt
  17. /vol2 # cat a.txt
  18. hello #本地目录挂载完成
  19. #新容器挂载已有容器的已有卷
  20. [root@localhost ~]# docker run -itd --volumes-from quirky_ramanujan --name db1 nginx:1.14-alpine
  21. 31b2b8a7a31803aa8d239667491212991cf44cdcf909f460990d8440ed3d0e57
  22. [root@localhost ~]# docker ps -a
  23. CONTAINER ID IMAGE COMMAND CREATED S
  24. 31b2b8a7a318 nginx:1.14-alpine "nginx -g 'daemon of…" 44 seconds ago U
  25. 2cee63a76808 nginx:1.14-alpine "/bin/sh" 13 minutes ago U
  26. [root@localhost ~]# docker exec -it db1 /bin/sh
  27. / # ls
  28. bin etc lib mnt proc run srv tmp var
  29. dev home media opt root sbin sys usr vol2
  30. / # cd vol2/
  31. /vol2 # ls
  32. a.txt
  33. /vol2 # cat a.txt
  34. hello

(4)删除数据卷

可以直接删除卷,也可以在删除容器时加“-v”参数同时删除数据卷

[root@localhost ~]# docker volume rm 卷名

(5)删除无主数据卷

[root@localhost ~]# docker volume prune

(6)查看镜像、容器、数据卷占用空间

  1. [root@localhost ~]# docker system df
  2. TYPE TOTAL ACTIVE SIZE RECLAIMABLE
  3. Images 1 1 16.03MB 0B (0%) #镜像
  4. Containers 5 2 14B 0B (0%) #容器
  5. Local Volumes 1 1 1.149kB 0B (0%) #数据卷
  6. Build Cache 0 0 0B 0B

9.主机与容器内文件的拷贝

使用docker cp 容器文件路径/主机目标路径 主机文件路径/容器目标路径

在指定容器内的路径时需要使用到容器名称,如下将本地的html文件拷贝到mynginx的容器中并且修改配置文件进行测试

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9acec97ce635 nginx "/docker-entrypoint.…" 2 hours ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
  4. [root@localhost ~]# docker exec -it 9ac /bin/bash
  5. root@9acec97ce635:/#
  6. root@9acec97ce635:/etc/nginx# vim nginx.conf
  7. [root@localhost ~]# docker cp /root/index.html mynginx:/etc/nginx/html #把html文件拷进去
  8. [root@localhost ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 9acec97ce635 nginx "/docker-entrypoint.…" 2 hours ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
  11. [root@localhost ~]# docker restart 9ac #重启容器
  12. 9ac

4a23613ba71d53f81dbe7db9cd618711.png a3fc5b20d90a33de9b4093dfe345c0e7.png

三.四种网络模式

1.bridge模式

(1)简介

如图所示,docker完整安装后会自动创建一个docker0网桥,不指定网络模式的情况下,docker程序默认将程序与docker0虚拟网桥连接,通过docker0来与宿主机通信。每运行一个docker都会在主机中产生对应的虚拟veth网卡设备,运行的docker都是和docker0虚拟网桥位于同一网段,docker0的地址作为docker的网关地址

bridge模式下主机和docker,docker与docker之间可以互相通信

a8c8d69734d9f063ad5df571ce875edf.png

(2)演示

运行两个容器,不指定网络模式情况下默认是bridge模式

  1. [root@localhost ~]# docker run -d -P --name web1 nginx:1.14-alpine
  2. 46dfb334cc76f59cfdb1cafcdea0bbc7b59626607f4f42441a4a579a256fa6fd
  3. [root@localhost ~]# docker run -d -P --name web2 nginx:1.14-alpine
  4. e98078ffd26a37861dc0eb3e66d87a75bbe6d070917fd0970282372245acf8e2
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. e98078ffd26a nginx:1.14-alpine "nginx -g 'daemon of…" 10 seconds ago Up 10 seconds 0.0.0.0:32770->80/tcp, :::32770->80/tcp web2
  8. 46dfb334cc76 nginx:1.14-alpine "nginx -g 'daemon of…" 18 seconds ago Up 17 seconds 0.0.0.0:32769->80/tcp, :::32769->80/tcp web1

在主机中自动生成了两个veth设备

  1. [root@localhost ~]# ip a
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  9. link/ether 00:0c:29:b7:d0:79 brd ff:ff:ff:ff:ff:ff
  10. inet 192.168.2.190/24 brd 192.168.2.255 scope global noprefixroute ens33
  11. valid_lft forever preferred_lft forever
  12. inet6 fe80::c0a0:564:e1a7:2b9c/64 scope link noprefixroute
  13. valid_lft forever preferred_lft forever
  14. 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  15. link/ether 02:42:e5:5e:06:6a brd ff:ff:ff:ff:ff:ff
  16. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  17. valid_lft forever preferred_lft forever
  18. inet6 fe80::42:e5ff:fe5e:66a/64 scope link
  19. valid_lft forever preferred_lft forever
  20. 15: veth57180bd@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
  21. link/ether 6a:f0:0e:99:2e:b9 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  22. inet6 fe80::68f0:eff:fe99:2eb9/64 scope link
  23. valid_lft forever preferred_lft forever
  24. 17: veth8e2b70d@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
  25. link/ether 8e:72:5f:e8:b0:c5 brd ff:ff:ff:ff:ff:ff link-netnsid 1
  26. inet6 fe80::8c72:5fff:fee8:b0c5/64 scope link
  27. valid_lft forever preferred_lft forever

查看两个容器的IP地址

  1. [root@localhost ~]# docker inspect web1 | grep -i ipaddress
  2. "SecondaryIPAddresses": null,
  3. "IPAddress": "172.17.0.2",
  4. "IPAddress": "172.17.0.2",
  5. [root@localhost ~]# docker inspect web2 | grep -i ipaddress
  6. "SecondaryIPAddresses": null,
  7. "IPAddress": "172.17.0.3",
  8. "IPAddress": "172.17.0.3",

可以自定义网桥,自定义网桥网段会发生变化

  1. [root@localhost ~]# docker network create -d bridge mybridge1
  2. 9a90056a032815f7a7016ca34e38456a3a494ec470d869bcaf55b54a04a11b3b
  3. [root@localhost ~]# docker network ls
  4. NETWORK ID NAME DRIVER SCOPE
  5. 77e840367105 bridge bridge local
  6. 5e0d2b0cb5bd host host local
  7. 9a90056a0328 mybridge1 bridge local
  8. b1f06c666e9b none null local
  9. [root@localhost ~]# ip a
  10. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  11. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  12. inet 127.0.0.1/8 scope host lo
  13. valid_lft forever preferred_lft forever
  14. inet6 ::1/128 scope host
  15. valid_lft forever preferred_lft forever
  16. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  17. link/ether 00:0c:29:b7:d0:79 brd ff:ff:ff:ff:ff:ff
  18. inet 192.168.2.190/24 brd 192.168.2.255 scope global noprefixroute ens33
  19. valid_lft forever preferred_lft forever
  20. inet6 fe80::c0a0:564:e1a7:2b9c/64 scope link noprefixroute
  21. valid_lft forever preferred_lft forever
  22. 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  23. link/ether 02:42:e5:5e:06:6a brd ff:ff:ff:ff:ff:ff
  24. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  25. valid_lft forever preferred_lft forever
  26. inet6 fe80::42:e5ff:fe5e:66a/64 scope link
  27. valid_lft forever preferred_lft forever
  28. 15: veth57180bd@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
  29. link/ether 6a:f0:0e:99:2e:b9 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  30. inet6 fe80::68f0:eff:fe99:2eb9/64 scope link
  31. valid_lft forever preferred_lft forever
  32. 17: veth8e2b70d@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
  33. link/ether 8e:72:5f:e8:b0:c5 brd ff:ff:ff:ff:ff:ff link-netnsid 1
  34. inet6 fe80::8c72:5fff:fee8:b0c5/64 scope link
  35. valid_lft forever preferred_lft forever
  36. 18: br-9a90056a0328: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  37. link/ether 02:42:73:2f:84:71 brd ff:ff:ff:ff:ff:ff
  38. inet 172.18.0.1/16 brd 172.18.255.255 scope global br-9a90056a0328
  39. valid_lft forever preferred_lft forever

2.host模式

(1)简介

host模式下,容器没有地理的网络命名空间(不会新增网卡设备),和宿主机共用网络命名空间,使用宿主机的IP和端口

(2)演示

运行一个容器,并且可以在容器内只看到和宿主机共用的IP

  1. [root@localhost ~]# docker run -it -d -p 80:80 --name web1 --network host nginx:1.14-alpine
  2. WARNING: Published ports are discarded when using host network mode
  3. fde1b4af966fc7f774185cf5895be0876cd3d06c8576815f105b1e1d33512360
  4. [root@localhost ~]# docker exec -it web1
  5. "docker exec" requires at least 2 arguments.
  6. See 'docker exec --help'.
  7. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  8. Execute a command in a running container
  9. [root@localhost ~]# docker exec -it web1 /bin/sh
  10. / # ls
  11. bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
  12. / # ip a
  13. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
  14. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  15. inet 127.0.0.1/8 scope host lo
  16. valid_lft forever preferred_lft forever
  17. inet6 ::1/128 scope host
  18. valid_lft forever preferred_lft forever
  19. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  20. link/ether 00:0c:29:b7:d0:79 brd ff:ff:ff:ff:ff:ff
  21. inet 192.168.2.190/24 brd 192.168.2.255 scope global ens33
  22. valid_lft forever preferred_lft forever
  23. inet6 fe80::c0a0:564:e1a7:2b9c/64 scope link
  24. valid_lft forever preferred_lft forever
  25. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
  26. link/ether 02:42:e5:5e:06:6a brd ff:ff:ff:ff:ff:ff
  27. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  28. valid_lft forever preferred_lft forever
  29. / # cd /usr/share/nginx/html/
  30. /usr/share/nginx/html # ls
  31. 50x.html index.html
  32. /usr/share/nginx/html # echo hello > index.html
  33. [root@localhost ~]# curl 192.168.2.190
  34. hello

3.container模式

(1)简介

指定这个模式下新运行的容器和已经存在的容器共享命名空间、IP、端口等,不和宿主机共享。可以理解为这两个容器之间在网络方面产生绑定关系,可以通过lo环回网卡设备通信。

(2)演示

  1. [root@localhost ~]# docker run --name nginx1 -it --network container:web1 nginx:1.14-alpine
  2. #web1为被共享的docker

4.none模式简介

none模式下,docker容器拥有自己的网络命名空间,此时不为任何docker容器进行网络配置,需要自定义网卡和IP等等。此模式下docker不参与网络通信,仅能访问本地环回接口,适用于无需网络的环境中的离线任务。

四.容器和镜像仓库练习

1.安装docker服务,配置镜像加速器

  1. [root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 #安装必要工具
  2. [root@localhost~]#yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #新增源
  3. [root@localhost ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo #修改源文件
  4. [root@localhost ~]# yum makecache fast #更新
  5. [root@localhost ~]# yum -y install docker-ce #安装docker-ce
  6. [root@localhost ~]# yum list docker-ce.x86_64 --showduplicates | sort -r #可以通过这行命令找到自己想要安装的具体版本,默认是安装最新版本
  7. [root@localhost ~]# systemctl start docker
  8. [root@localhost ~]# docker version #查看docker版本
  9. Client: Docker Engine - Community
  10. Version: 24.0.5
  11. API version: 1.43
  12. Go version: go1.20.6
  13. Git commit: ced0996
  14. Built: Fri Jul 21 20:39:02 2023
  15. OS/Arch: linux/amd64
  16. Context: default
  17. Server: Docker Engine - Community
  18. Engine:
  19. Version: 24.0.5
  20. API version: 1.43 (minimum version 1.12)
  21. Go version: go1.20.6
  22. Git commit: a61e2b4
  23. Built: Fri Jul 21 20:38:05 2023
  24. OS/Arch: linux/amd64
  25. Experimental: false
  26. containerd:
  27. Version: 1.6.22
  28. GitCommit: 8165feabfdfe38c65b599c4993d227328c231fca
  29. runc:
  30. Version: 1.1.8
  31. GitCommit: v1.1.8-0-g82f18fe
  32. docker-init:
  33. Version: 0.19.0
  34. GitCommit: de40ad0
  35. [root@localhost ~]# cd /etc/docker/
  36. [root@localhost docker]# ll
  37. total 0
  38. [root@localhost docker]# vim daemon.json
  39. {
  40. "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
  41. }
  42. #使用的是中国科技大学的镜像加速器,也可以选择阿里云的
  43. [root@localhost docker]# systemctl daemon-reload #重载配置并重启
  44. [root@localhost docker]# systemctl restart docker

2.下载系统镜像(ubuntu和centos)

  1. [root@localhost ~]# docker pull centos
  2. Using default tag: latest
  3. latest: Pulling from library/centos
  4. a1d0c7532777: Pull complete
  5. Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
  6. Status: Downloaded newer image for centos:latest
  7. docker.io/library/centos:latest
  8. [root@localhost ~]# docker pull ubuntu
  9. Using default tag: latest
  10. latest: Pulling from library/ubuntu
  11. b237fe92c417: Pull complete
  12. Digest: sha256:ec050c32e4a6085b423d36ecd025c0d3ff00c38ab93a3d71a460ff1c44fa6d77
  13. Status: Downloaded newer image for ubuntu:latest
  14. docker.io/library/ubuntu:latest

3.基于下载的镜像创建两个容器(一个为名字全拼一个为名字首字母)

  1. [root@localhost ~]# docker run -it --name sulibao -d -P ubuntu
  2. b7d1e332cdad2844250054c4fc5c78856139af2117464843251d04974f0ec8d9
  3. [root@localhost ~]# docker run -it --name slb -d -P centos
  4. d51febc7bc7cc7673e5f539d3ed535fffbd1c72aed29ac838dfc59ce99380511
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. d51febc7bc7c centos "/bin/bash" 27 seconds ago Up 26 seconds slb
  8. b7d1e332cdad ubuntu "/bin/bash" 44 seconds ago Up 43 seconds sulibao

4.容器启动、停止和重启

  1. [root@localhost ~]# docker stop slb
  2. slb
  3. [root@localhost ~]# docker stop sulibao
  4. sulibao
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. [root@localhost ~]# docker restart slb
  8. slb
  9. [root@localhost ~]# docker restart sulibao
  10. sulibao
  11. [root@localhost ~]# docker ps
  12. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  13. d51febc7bc7c centos "/bin/bash" About a minute ago Up 6 seconds slb
  14. b7d1e332cdad ubuntu "/bin/bash" About a minute ago Up 2 seconds sulibao

5.查看正在运行的容器和所有容器

  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. d51febc7bc7c centos "/bin/bash" 6 minutes ago Up 5 minutes slb
  4. b7d1e332cdad ubuntu "/bin/bash" 6 minutes ago Up 5 minutes sulibao

6.两种方法退出容器

(1)交互模式下exit

  1. [root@localhost ~]# docker exec -it slb /bin/sh
  2. sh-4.4# ls
  3. bin etc lib lost+found mnt proc run srv tmp var
  4. dev home lib64 media opt root sbin sys usr
  5. sh-4.4# exit
  6. exit
  7. [root@localhost ~]#

(2)交互模式下“ctrl+d”

  1. [root@localhost ~]# docker exec -it sulibao /bin/sh
  2. # ls
  3. bin dev home lib32 libx32 mnt proc run srv tmp var
  4. boot etc lib lib64 media opt root sbin sys usr
  5. #
  6. [root@localhost ~]#

7.连接到运行的容器

  1. [root@localhost ~]# docker exec -it sulibao /bin/sh
  2. # ls
  3. bin dev home lib32 libx32 mnt proc run srv tmp var
  4. boot etc lib lib64 media opt root sbin sys usr

8.查看容器或镜像的内部信息

  1. [root@localhost ~]# docker inspect slb
  2. [root@localhost ~]# docker inspect ubuntu:latest

9.如何查看所有镜像

  1. [root@localhost ~]# docker images -a
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. ubuntu latest 01f29b872827 2 weeks ago 77.8MB
  4. centos latest 5d0da3dc9764 23 months ago 231MB

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/1008146
推荐阅读
相关标签
  

闽ICP备14008679号