2024年大数据最新【Docker】docker日常工作总结(涉及基础命令、基础名词概念(2),熬夜整理蚂蚁金服大数据开发高级笔试题

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

sudo docker ps -a:查看所有的容器
sudo docker container ls:查看正在运行的容器
sudo docker container ls --all :查看所有的容器

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker container ls
CONTAINER ID   IMAGE                 COMMAND                  CREATED        STATUS        PORTS                                                                                        NAMES
bca70facba90   nginx:alpine          "/docker-entrypoint.…"   3 hours ago    Up 3 hours    0.0.0.0:49160->80/tcp, :::49160->80/tcp                                                      web4
ff2d3c951eed   nginx:alpine          "/docker-entrypoint.…"   3 hours ago    Up 3 hours    0.0.0.0:49159->80/tcp, :::49159->80/tcp                                                      web3
4011f219f403   nginx:alpine          "/docker-entrypoint.…"   3 hours ago    Up 3 hours    0.0.0.0:49158->80/tcp, :::49158->80/tcp                                                      web2
d992bc0fa330   nginx                 "/docker-entrypoint.…"   3 hours ago    Up 3 hours    80/tcp                                                                                       nginx_test
2c4108268aa9   jenkins/jenkins:lts   "/sbin/tini -- /usr/…"   25 hours ago   Up 25 hours   0.0.0.0:49154->8080/tcp, :::49154->8080/tcp, 0.0.0.0:49153->50000/tcp, :::49153->50000/tcp   jenkins
ae1fd6dd1b00   mysql                 "docker-entrypoint.s…"   3 weeks ago    Up 3 days     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                         mysql

🌙8、运行容器

1、sudo docker run redis:redis是一个镜像,首先去本地查看有没有redis的镜像,如果有直接去运行,如果没有,默认去docker hub官方镜像中拉取到本地
2、如果后面没有指名版本号,等价于sudo docker run redis:latest(最新的)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run redis
Unable to find image 'redis:latest' locally
latest: Pulling from library/redis
42c077c10790: Already exists 
a300d83d65f9: Pull complete 
ebdc3afaab5c: Pull complete 
31eec7f8651c: Pull complete 
9c6a6b89d274: Pull complete 
5c8099a4b45c: Pull complete 
Digest: sha256:1b90dbfe6943c72a7469c134cad3f02eb810f016049a0e19ad78be07040cdb0c
Status: Downloaded newer image for redis:latest
1:C 02 Jun 2022 05:36:13.616 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 02 Jun 2022 05:36:13.616 # Redis version=7.0.0, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 02 Jun 2022 05:36:13.616 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 02 Jun 2022 05:36:13.616 \* monotonic clock: POSIX clock_gettime
1:M 02 Jun 2022 05:36:13.617 \* Running mode=standalone, port=6379.
1:M 02 Jun 2022 05:36:13.617 # Server initialized
1:M 02 Jun 2022 05:36:13.617 # WARNING overcommit\_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit\_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit\_memory=1' for this to take effect.
1:M 02 Jun 2022 05:36:13.618 \* The AOF directory appendonlydir doesn't exist
1:M 02 Jun 2022 05:36:13.618 \* Ready to accept connections

^C1:signal-handler (1654148179) Received SIGINT scheduling shutdown...
1:M 02 Jun 2022 05:36:19.649 # User requested shutdown...
1:M 02 Jun 2022 05:36:19.649 \* Saving the final RDB snapshot before exiting.
1:M 02 Jun 2022 05:36:19.649 \* DB saved on disk
1:M 02 Jun 2022 05:36:19.649 # Redis is now ready to exit, bye bye...

默认运行一个容器时容器名称是随机生成的

在这里插入图片描述

运行的时候指定容器名称,容器名称必须唯一
- -name :给容器指定名称
- -name与容器名称在一块,不能分开

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name centos_t1 centos 

-it:运行容器,并且进入容器中
退出容器:exit

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run -it --name centos_t2 centos
[root@90f0e8d75990 /]# 

✨例如:进入到centos容器中了

在这里插入图片描述

选择tag,建议使用alpine:非常小

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name nginx_1 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/06/02 05:58:04 [notice] 1#1: using the "epoll" event method
2022/06/02 05:58:04 [notice] 1#1: nginx/1.21.6
2022/06/02 05:58:04 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/06/02 05:58:04 [notice] 1#1: OS: Linux 5.13.0-40-generic
2022/06/02 05:58:04 [notice] 1#1: getrlimit(RLIMIT\_NOFILE): 1048576:1048576
2022/06/02 05:58:04 [notice] 1#1: start worker processes
2022/06/02 05:58:04 [notice] 1#1: start worker process 31
2022/06/02 05:58:04 [notice] 1#1: start worker process 32
2022/06/02 05:58:04 [notice] 1#1: start worker process 33
2022/06/02 05:58:04 [notice] 1#1: start worker process 34
2022/06/02 05:58:06 [notice] 1#1: signal 28 (SIGWINCH) received

宿主机(ubuntu)的端口映射容器端口(访问宿主机中的1111端口相当于访问容器中的80端口)
-p:手动指定端口
-P:不需要手动指定端口,会随机的指定端口

✨例如启动nginx手动指定端口

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name nginx_2 -p 1111:80 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/06/02 06:06:19 [notice] 1#1: using the "epoll" event method
2022/06/02 06:06:19 [notice] 1#1: nginx/1.21.6
2022/06/02 06:06:19 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/06/02 06:06:19 [notice] 1#1: OS: Linux 5.13.0-40-generic
2022/06/02 06:06:19 [notice] 1#1: getrlimit(RLIMIT\_NOFILE): 1048576:1048576
2022/06/02 06:06:19 [notice] 1#1: start worker processes
2022/06/02 06:06:19 [notice] 1#1: start worker process 32
2022/06/02 06:06:19 [notice] 1#1: start worker process 33
2022/06/02 06:06:19 [notice] 1#1: start worker process 34
2022/06/02 06:06:19 [notice] 1#1: start worker process 35
^C2022/06/02 06:07:31 [notice] 1#1: signal 2 (SIGINT) received, exiting
2022/06/02 06:07:31 [notice] 33#33: exiting
2022/06/02 06:07:31 [notice] 34#34: exiting
2022/06/02 06:07:31 [notice] 32#32: exiting
2022/06/02 06:07:31 [notice] 35#35: exiting
2022/06/02 06:07:31 [notice] 34#34: exit
2022/06/02 06:07:31 [notice] 33#33: exit
2022/06/02 06:07:31 [notice] 32#32: exit
2022/06/02 06:07:31 [notice] 35#35: exit
2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 35
2022/06/02 06:07:31 [notice] 1#1: worker process 32 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: worker process 35 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: signal 29 (SIGIO) received
2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 33
2022/06/02 06:07:31 [notice] 1#1: worker process 33 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: signal 29 (SIGIO) received
2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 34
2022/06/02 06:07:31 [notice] 1#1: worker process 34 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: exit

✨小写p:宿主机访问1111端口
在这里插入图片描述
✨大写P:宿主机访问49161端口(随机指定的)
在这里插入图片描述

🌏 目前存在的问题:1、启动nginx,终端会展示日志;2、一旦关闭终端,容器将停止工作。
解决:

🌙9、后台运行容器

-d:指定容器在后台执行
特别注意:如果是手动指定端口,需要将防火墙中的这个端口放通

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name nginx_4 -P -d nginx
fa855451e519418cf4348a16dcaf52dcbd53e971bc934e196bdb3130acd72026

- -rm:退出容器后,会自动将容器删除

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run -it --name centos_1 --rm centos
[root@f992808f12cc /]# exit
exit

🌙10、创建容器的时候指定环境变量

-e:设置全局变量

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --rm -it -e USERNAME=kobe -e PASSWORD=123123 centos /bin/bash
[root@0489627cb70f /]# env
LANG=en_US.UTF-8
HOSTNAME=0489627cb70f
USERNAME=kobe
PASSWORD=123123
PWD=/
HOME=/root
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env


🌙11、进入正在运行中的容器

exec:进入正在运行中的容器

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker exec -it centos_1 /bin/bash
[root@7e1cf2a4bec7 /]# 

✨当容器太多时,查看需要的容器

zhilong@zhilong-virtual-machine:~/桌面$ sudo docker ps | awk '/Up/ {print $1,$NF}'
95cf126972d5 centos_2
7e1cf2a4bec7 centos_1
d992bc0fa330 nginx_test
2c4108268aa9 jenkins
ae1fd6dd1b00 mysql

🌙12、查看某个容器的进程

zhilong@zhilong-virtual-machine:~/桌面$ sudo docker top centos_1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                52297               52276               0                   14:31               pts/0               00:00:00            /bin/bash
zhilong@zhilong-virtual-machine:~/桌面$ 

🌙13、查看容器资源利用率

CONTAINER ID   NAME       CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O     PIDS
7e1cf2a4bec7   centos_1   0.00%     2.566MiB / 3.798GiB   0.07%     3.79kB / 0B   1.43MB / 0B   1

停止容器:sudo docker stop 容器id或者容器名称
重启容器:sudo docker restart 容器id或者容器名称
暂停容器:sudo docker paush 容器id或者容器名称
取消暂停容器:sudo docker unpaush 容器id或者容器名称
删除容器:sudo docker rm 容器id或者容器名称(前提是先暂停容器)
强制删除容器:sudo docker rm -f 容器id或者容器名称

二、网络

🌙1、网络类型:

✨bridge:

  • nat网络模型
  • 虚拟交换机

✨host

  • 与宿主机共享网络
  • –net=host

✨none

  • 不配置网络
  • –net=none

✨overlay

  • 不同网络进行通信

🌙2、查看容器的ip地址

sudo docker inspect nginx_1:查看容器的详细信息
sudo docker exec -it nginx_1 ip a:进入容器查看容器的ip
172.17.0.5

✨第一种方法:

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker exec -it nginx_1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
112: eth0@if113: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.5/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever


✨第二方法:

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker inspect nginx_1 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.5",
                    "IPAddress": "172.17.0.5",
zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker inspect nginx_2 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.6",
                    "IPAddress": "172.17.0.6",
zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker inspect nginx_3 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.7",
                    "IPAddress": "172.17.0.7",


✨查看nginx_1、nginx_2、nginx_3三个容器的ip地址

在这里插入图片描述

🌏查看nginx_1、nginx_2、nginx_3三个容器的网关地址,得到结论:用sudo docker run创建的容器会默认挂载到docker0上面,docker0作为一个桥梁进行上网。所以可以上外网

在这里插入图片描述

🌙3、创建网络:默认指定的网关地址和子网地址

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network create -d bridge mynet2
80a7f08272dde3d0b7dd7a91b67b823415e554dd5d8bae8bee0293697b055680

✨查看详情

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network inspect mynet2
[
    {
        "Name": "mynet2",
        "Id": "80a7f08272dde3d0b7dd7a91b67b823415e554dd5d8bae8bee0293697b055680",
        "Created": "2022-06-02T19:44:30.465164936+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

🌙4、创建网络:手动指定网关

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network create -d bridge --subnet 172.16.200.0/24 mynet3
0e062b2fd2340178cc7e6fc72377418314236f9f2be826cb504d45e18be124da

🌏使用sudo docker run 创建的容器会默认加入到docker0桥接网络里面,这个桥接网络里面是不支持通过容器的名字去访问的;
但是我们自己创建的桥接网络,除了可以使用ip地址去访问以外,还可以通过容器名称去访问。

🌙5、删除网络

sudo docker network rm 网络
如果容器正在启动中,不能直接删除网络,需要先将容器删除,再删除网络

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network rm mynet1
[sudo] zhilong 的密码: 
mynet1


sudo docker network prune:把无用的网络删除

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
mynet
mynet2
mynet3

三、Volume

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

top/shell_text/docker$ sudo docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
mynet
mynet2
mynet3


## 三、Volume


[外链图片转存中...(img-hSWhhGrb-1715614990946)]
[外链图片转存中...(img-5rK0h7JA-1715614990946)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值