第四章 Docker容器

4.1 容器运行

使用 docker run命令可以运行rongqi。该命令底层其实是docker create 与docker start 两条命令的结合体。


1、##基于镜像启动一个新容器 并且打印当月日历 示例代码如下  

[root@localhost ~]# docker run centos cal
   September 2024   
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 

 2、##ps命令在linux系统中被用来查看进程

                    
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
8d3d2add2774   centos        "cal"         54 seconds ago   Exited (0) 53 seconds ago             affectionate_cohen
4e3d4694f7c3   centos        "/bin/bash"   2 days ago       Exited (0) 2 days ago                 boring_aryabhata
4639ddabd86b   centos        "/bin/bash"   2 days ago       Exited (1) 2 days ago                 gifted_feynman
95919e1cb4ad   centos        "/bin/bash"   2 days ago       Exited (0) 2 days ago                 admiring_goldstine
94f8878aa838   centos        "/bin/bash"   2 days ago       Exited (0) 2 days ago                 funny_edison
7651c8608318   centos        "/bin/bash"   2 days ago       Exited (0) 2 days ago                 vigilant_khorana
6880237fa33e   centos/vim    "/bin/bash"   2 days ago       Exited (0) 2 days ago                 distracted_montalcini

 3、##通过指定参数 启动一个bash交互终端 

      -i 表示捕获标准输入输出

      -t 表示分配一个终端或控制台 

[root@localhost ~]# docker run -it centos /bin/bash
[root@df97a48bf04b /]# cal
   September 2024   
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30               
                    
[root@df97a48bf04b /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
df97a48bf04b   centos        "/bin/bash"   31 seconds ago   Exited (0) 11 seconds ago             busy_golick
8d3d2add2774   centos        "cal"         17 minutes ago   Exited (0) 17 minutes ago             affectionate_cohen

 4、下面运行一个容器 并为其设置环境变量 示例如下

#设置环境变量 key=100
[root@localhost ~]# docker run -d -it -e key=1000 centos
e2997076450ae15c55187b9e79a0abad7c9425d6a932b1965e2238d374829c9f
#e299 是上面容器的开头几位符号
[root@localhost ~]# docker exec -it e2997 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=e2997076450a
TERM=xterm
key=1000  //结果
HOME=/root

5、设置一个自动重启的容器

#启动一个可交互的容器
[root@localhost ~]# docker run -it centos
#推出容器
[root@c910cb965a26 /]# exit
exit
#查看容器状态是Exited
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
c910cb965a26   centos        "/bin/bash"   15 seconds ago   Exited (0) 10 seconds ago             trusting_chaplygin
e2997076450a   centos        "/bin/bash"   7 minutes ago    Up 7 minutes
5  
#添加--restart参数                        
[root@localhost ~]# docker run -it --restart=always centos
#退出容器
[root@8c9d37a193ac /]# exit
exit
#查看容器状态是运行中
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED              STATUS                      PORTS     NAMES
8c9d37a193ac   centos        "/bin/bash"   15 seconds ago       Up 6 seconds                          friendly_raman
c910cb965a26   centos        "/bin/bash"   About a minute ago   Exited (0) 56 seconds ago             trusting_chaplygin

 6、自定义名称的容器


[root@localhost ~]# docker run -d  --name=test centos
46d3c4987f5a06c248dc28d02ae225c572254802d8d85719d64fcda0d71c52e9
#name==test
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED             STATUS                         PORTS     NAMES
46d3c4987f5a   centos        "/bin/bash"   16 seconds ago      Exited (0) 15 seconds ago                test

 7、开启端口容器

[root@localhost ~]# docker run -d -p 80:80 nginx  //开始端口命令
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a2318d6c47ec: Pull complete 
095d327c79ae: Pull complete 
bbfaa25db775: Pull complete 
7bb6fb0cfb2b: Pull complete 
0723edc10c17: Pull complete 
24b3fdc4d1e3: Pull complete 
3122471704d5: Pull complete 
Digest: sha256:04ba374043ccd2fc5c593885c0eacddebabd5ca375f9323666f28dfd5a9710e3
Status: Downloaded newer image for nginx:latest
d86edc088459c9b1dd6a2c44c5fffb0d9dc4f5d8ecaa8a7aa8b0342879e7a5b7
#报错我们解决一下
docker: Error response from daemon: driver failed programming external connectivity on endpoint confident_pare (2fef79934bb6ae95fb9936700a89932c4a12457c29559dbcbd6515d78ec9d9db): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use.

[root@localhost ~]# curl -I 虚拟机的IP:80   //例192.128.190.1:80  使用curl工具访问测试
HTTP/1.1 403 Forbidden
Date: Thu, 26 Sep 2024 13:13:55 GMT
Server: Apache/2.4.37 (centos) OpenSSL/1.1.1k
Last-Modified: Sun, 27 Jun 2021 23:47:13 GMT
ETag: "30c0b-5c5c7fdeec240"
Accept-Ranges: bytes
Content-Length: 199691
Content-Type: text/html; charset=UTF-8

 错误问题: docker: Error response from daemon: driver failed programming external connectivity on endpoint confident_pare (2fef79934bb6ae95fb9936700a89932c4a12457c29559dbcbd6515d78ec9d9db): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use.

检查端口是否被占用,错误信息是端口tcp4 0.0.0.0:80端口被占用啦

netstat -tunlp | grep 端口号      查看端口号对应的进程,用于排查端口号是否被占用

netstat -anp | grep 端口号         查看端口号的使用情况:

netstat -pt                                   显示pid和进程:

netstat -l                                      只显示监听端口

netstat -lt                                     只列出所有监听 tcp 端口

netstat -lu                                    只列出所有监听 udp 端口

netstat -lx                                     只列出所有监听 UNIX 端口

sudo lsof -i :端口号                      获取具有所有TCP监听端口

[root@localhost ~]# sudo lsof -i :80   //查找占用端口的进程
sudo: lsof:找不到命令
[root@localhost ~]#  sudo yum install -y lsof  //找不到该命令 用yum安装

[root@localhost ~]# sudo lsof -i :80
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd    980   root    4u  IPv6  29738      0t0  TCP *:http (LISTEN)
[root@localhost ~]# sudo netstat -tuln | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN     
[root@localhost ~]# sudo netstat -anp | grep 80
tcp6       0      0 :::443                  :::*                    LISTEN      980/httpd           
tcp6       0      0 :::80                   :::*                    LISTEN      980/httpd           
unix  2      [ ACC ]     STREAM     LISTENING     29871    1105/httpd           /etc/httpd/run/cgisock.980
unix  2      [ ACC ]     STREAM     LISTENING     74422    6980/zabbix_server:  /var/run/zabbix/zabbix_server_alerter.sock
unix  3      [ ]         STREAM     CONNECTED     28011    1/systemd            /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     23088    808/systemd-udevd    

上面可以知道 我们的端口被占用啦

kill -9 进程id     //杀掉占用端口的进程

#杀死占用80端口的进程
[root@localhost ~]# kill -9 980   
#查看端口情况 
[root@localhost ~]# sudo netstat -anp | grep 80
unix  3      [ ]         STREAM     CONNECTED     23088    808/systemd-udevd    
unix  3      [ ]         STREAM     CONNECTED     83807    7946/zabbix_server:  
unix  3      [ ]         STREAM     CONNECTED     83803    7943/zabbix_server:  
unix  3      [ ]         STREAM     CONNECTED     83805    7952/zabbix_server:  
unix  3      [ ]         STREAM     CONNECTED     83806    7945/zabbix_server:  
unix  3      [ ]         STREAM     CONNECTED     28034    1/systemd            /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     28023    1/systemd            /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     27080    948/sssd             /var/lib/sss/pipes/private/sbus-monitor
unix  3      [ ]         STREAM     CONNECTED     83808    7947/zabbix_server:  
unix  3      [ ]         STREAM     CONNECTED     29880    1068/systemd-logind  
unix  2      [ ]         STREAM                   83802    7929/zabbix_server:  
unix  3      [ ]         DGRAM                    23115    808/systemd-udevd    
unix  3      [ ]         DGRAM                    23116    808/systemd-udevd    
unix  2      [ ]         STREAM                   83809    7946/zabbix_server:  
unix  2      [ ]         DGRAM                    23095    808/systemd-udevd    
unix  3      [ ]         STREAM     CONNECTED     83804    7951/zabbix_server:  
[root@localhost ~]# lsof -i :80   //没有占用的进程

我们再次开启端口

#使用开启端口命令
[root@localhost ~]# docker run -d -p 80:80 nginx
0408f38122d72b534c98ba90504d7cf7ffc196adea435805a7ec406078c38d97
#可以看到HTTP后面是OK 就是成功啦
[root@localhost ~]# curl -I 192.168.190.128:80
HTTP/1.1 200 OK
Server: nginx/1.27.1
Date: Thu, 26 Sep 2024 13:58:19 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 12 Aug 2024 14:21:01 GMT
Connection: keep-alive
ETag: "66ba1a4d-267"
Accept-Ranges: bytes

 8、与宿主机共享目录的容器

[root@localhost ~]# mkdir test   //创建共享目录
[root@localhost ~]# touch /root/test/a.txt /root/test/b.txt  //创建共享文件
[root@localhost ~]# ls
anaconda-ks.cfg  apache.yml  backup  dockerfile  Dockerfile  hello.sh  test  test0
[root@localhost ~]# ls test
a.txt  b.txt
[root@localhost ~]# docker run -it -v  /root/test/:/root/test/ --privileged docker.io/nginx /bin/bash   //运行容器,挂载目录,冒号前是宿主机目录,后面是容器目录
root@2b263a4aab7f:/# ls /root/test/
a.txt  b.txt
root@2b263a4aab7f:/# exit
exit

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值