Linux学习--Docker学习

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习--Docker学习相关的知识,希望对你有一定的参考价值。

容器技术

Docker安装和使用

#安装yum工具包
[root@docker ~]# yum install -y yum-utils
#配置阿里云镜像
[root@docker ~]# yum-config-manager --add-repo  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装最新版本的 Docker Engine-Community 和 containerd
[root@docker ~]# yum install -y docker-ce docker-ce-cli containerd.io
[root@docker ~]# systemctl start docker 
[root@docker ~]# ps -ef | grep docker
root       2446      1  0 21:07 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root       2623   2145  0 21:08 pts/0    00:00:00 grep --color=auto docker
#运行一个HelloWorld
[root@docker ~]# docker run ubuntu:15.10 /bin/echo "Hello World"
Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete 
759aa75f3cee: Pull complete 
3fa871dc8a2b: Pull complete 
224c42ae46e7: Pull complete 
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
Hello World
#查看本地镜像列表
[root@docker ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       15.10     9b9cb95443b5   5 years ago   137MB
#查看镜像详细信息
[root@docker ~]# docker image inspect 9b9cb95443b5
#从仓库拉取镜像
[root@docker ~]# docker pull centos
#查看镜像的历史信息
[root@docker ~]# docker image history centos
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
5d0da3dc9764   3 weeks ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB   
#保存镜像到本地磁盘(使用-o参数)
[root@docker ~]# docker image save centos  -o /root/centos.tar.gz
[root@docker ~]# ls /root
anaconda-ks.cfg  nginx-1.14.2         openssl-1.1.1k         Python-3.8.6
centos.tar.gz    nginx-1.14.2.tar.gz  openssl-1.1.1k.tar.gz  Python-3.8.6.tar.xz
#删除一个镜像(可以使用镜像ID或REPOSITORY|REPOSITORY:TAG)
[root@docker ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   3 weeks ago   231MB
ubuntu       15.10     9b9cb95443b5   5 years ago   137MB
[root@docker ~]# docker image rm centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@docker ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       15.10     9b9cb95443b5   5 years ago   137MB
#载入本地镜像
[root@docker ~]# docker load -i /root/centos.tar.gz 
74ddd0ec08fa: Loading layer  238.6MB/238.6MB
Loaded image: centos:latest
[root@docker ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   3 weeks ago   231MB
ubuntu       15.10     9b9cb95443b5   5 years ago   137MB
#给镜像打标签
[root@docker ~]# docker image tag 87a94228f133 tye:lotus
[root@docker ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    87a94228f133   47 hours ago   133MB
tye          lotus     87a94228f133   47 hours ago   133MB
centos       latest    5d0da3dc9764   4 weeks ago    231MB
ubuntu       15.10     9b9cb95443b5   5 years ago    137MB
#--------------------------------------容器-----------------------------------------------
#交互式容器退出后会自动销毁
#交互式启动容器(-i[interactive]-t[tty])
[root@docker ~]# docker container run -it centos
[root@f414580a3ee7 /]# 
#交互式指定名称启动容器
[root@docker ~]# docker container run -it --name="tyecentos8.4" 5d0da3dc9764
[root@4c529d4d07d0 /]# 
#守护式容器启动(-d)
[root@docker ~]# docker container run -d nginx
32e1524eec78a21c8c000a6eaa67916f0cd7415f095ac062856b21d7698090da

#在容器中运行一个镜像
[root@docker ~]# docker container run -id centos
b2ea3d62e02c8c556eb7f4ce70c614ff7dfaa9a450a84a37385243c96b08448b
#查看容器状态信息
[root@docker ~]# docker container ls
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
b2ea3d62e02c   centos    "/bin/bash"   8 seconds ago   Up 7 seconds             dazzling_shirle
#----CONTAINER ID:容器ID
#----NAMES:容器的名称
#----STATUS:容器的状态(Exited,Up)
#显示所有容器列表
[root@docker ~]# docker container ls -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                       PORTS     NAMES
4c529d4d07d0   5d0da3dc9764   "/bin/bash"              2 minutes ago    Exited (130) 5 seconds ago             tyecentos8.4
f414580a3ee7   centos         "/bin/bash"              10 minutes ago   Up 10 minutes                          lucid_fermi
a9b4dac8c4d8   nginx          "/docker-entrypoint.…"   25 minutes ago   Exited (0) 19 minutes ago              vibrant_darwin
#查看镜像列表,只显示镜像ID
[root@docker ~]# docker image ls -q
5d0da3dc9764
9b9cb95443b5
#查看容器的信息
[root@docker ~]# docker container inspect 32e1524eec78
#访问容器中启动的nginx
[root@docker ~]# curl -I 172.17.0.3
HTTP/1.1 200 OK
Server: nginx/1.21.3
Date: Thu, 14 Oct 2021 01:09:59 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 07 Sep 2021 15:21:03 GMT
Connection: keep-alive
ETag: "6137835f-267"
Accept-Ranges: bytes

#停止正在运行的容器(stop后面跟上容器ID)
[root@docker ~]# docker container stop b2ea3d62e02c
b2ea3d62e02c
#批量删除所有容器
[root@docker ~]# docker container rm $(docker container ls -aq)
48e7f9eb9684
b2ea3d62e02c
cc505b7a8cbb
a36c554b5763
cd0b6e22a0af
31c563251b17
07778cf50433
#映射端口启动nginx服务
[root@docker ~]# docker container run -d -p 8080:80 --name=tye_nginx nginx:1.14
#启动停止的容器,-i交互式启动容器
[root@docker ~]# docker container start -i f414580a3ee7
[root@f414580a3ee7 /]# 
#获取容器中的进程运行情况
[root@docker ~]# docker container top 23f92b18e75b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2114                2094                0                   20:34               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 2145                2114                0                   20:34               ?                   00:00:00            nginx: worker process
#查看日志
[root@docker ~]# docker container logs 23f92b18e75b
2021/10/15 00:49:58 [error] 8#8: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.88.101:8080", referrer: "http://192.168.88.101:8080/"
172.17.0.1 - - [15/Oct/2021:00:49:58 +0000] "GET /favicon.ico HTTP/1.1" 404 571 "http://192.168.88.101:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36" "-"
#--------------------------------------------------数据卷(持久化存储)-----------------------------------------
#复制宿主机中的文件至容器
[root@docker opt]# docker container cp test.html nginx_out:/usr/share/nginx/html/index.html
#复制容器中的文件至当前宿主机
[root@docker opt]# docker container cp nginx_out:/usr/share/nginx/html/50x.html .
#----volume实现宿主机和容器的数据共享
#通过-v参数实现volume宿主机和容器的映射
[root@docker ~]# docker run -d --name="nginx_out2" -p 80:80 -v /opt/html/:/usr/share/nginx/html/ nginx:1.14
c60283008b499b48e10c25ad99c83d1e7d10bb2e07bc847d05cdf3de6f43f73d
#----------映射完成后查看映射情况
#-----------容器中的情况
[root@docker ~]# docker container exec -it nginx_out2 /bin/bash
root@c60283008b49:/# ls /usr/share/nginx/html/
50x.html  index.html
#----------宿主机情况
[root@docker html]# ls
50x.html  index.html
[root@docker html]# pwd
/opt/html

容器的应用场景
交互式容器:

  • 工具类
  • 开发、测试
  • 临时性任务
    守护式容器(网络服务[ssh,]):
  • 服务,对外暴露端口,

容器的连接方式

  • attach
#通过attach方式连接一个启动的容器
[root@docker ~]# docker container ls
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                   NAMES
6e68ca03c74b   nginx:1.14   "nginx -g 'daemon of…"   12 minutes ago   Up 12 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   tye_nginx
f414580a3ee7   centos       "/bin/bash"              14 hours ago     Up 5 seconds                                            lucid_fermi
[root@docker ~]# docker container attach f414580a3ee7
[root@f414580a3ee7 /]# 
  • exec[建议使用]:通过子进程的方式连接一个启动的容器(在已有工作容器中生成子进程,进行登录,可以进行容器的调试,退出时不会影响到当前容器)
[root@docker ~]# docker container exec -it f414580a3ee7 /bin/bash

容器的前后台运行

  • ctrl+p,Q切换出容器,使用attach或exec调用到前台
  • 死循环
  • 让程序前台一直运行(夯在前台)
    • 制作守护式容器时常用的方法

容器的网络访问

默认情况只能内部访问
– 指定映射(docker会自动在宿主机上添加一条iptables规则来实现端口映射)
-p hostPort:containerPort
-p ip:hostPort:containerPort
-p ip::containerPort(随机端口)
-p hostPort:containerPort/udp
-p 81:80 -p 443:443多端口映射
– 随机映射
docker run -p 80(随机端口)

#启动一个nginx容器,并将容器的80端口映射至宿主机的8080端口上
[root@docker ~]# docker container run -d -p 8080:80 nginx:1.14
23f92b18e75b689603aed94c78372ca3850a57c36c1b8ce024a8b40b41f101c7
[root@docker ~]# docker container ls
CONTAINER ID   IMAGE        COMMAND                  CREATED         STATUS         PORTS                                   NAMES
23f92b18e75b   nginx:1.14   "nginx -g 'daemon of…"   5 seconds ago   Up 4 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   vigilant_mayer
[root@docker ~]# netstat -tnlp | grep 8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      2428/docker-proxy   
tcp6       0      0 :::8080                 :::*                    LISTEN      2434/docker-proxy   
[root@docker ~]# docker container run -d -p 80  nginx:1.14
cdda00b89e14084b3d71bb37802d74c16e3a54f57ce3a194b86b0b0327ac7166
[root@docker ~]# docker container run -d -p 192.168.88.101::80  nginx:1.14
[root@docker ~]# docker container run -d -p 192.168.88.101:8081:80  nginx:1.14
cc2f54a773147f49f70e90702af3cee529ddc5586539a5d1ea5441bbf6ae2045
[root@docker ~]# docker container ls
CONTAINER ID   IMAGE        COMMAND                  CREATED              STATUS          PORTS                                     NAMES
cc2f54a77314   nginx:1.14   "nginx -g 'daemon of…"   4 seconds ago        Up 4 seconds    192.168.88.101:8081->80/tcp               jovial_mendeleev
1c54377fc9ed   nginx:1.14   "nginx -g 'daemon of…"   30 seconds ago       Up 30 seconds   192.168.88.101:49165->80/tcp              loving_feistel
cdda00b89e14   nginx:1.14   "nginx -g 'daemon of…"   About a minute ago   Up 59 seconds   0.0.0.0:49164->80/tcp, :::49154->80/tcp   magical_golick
63e546650dcb   nginx:1.14   "nginx -g 'daemon of…"   2 minutes ago        Up 2 minutes    0.0.0.0:49163->80/tcp, :::49153->80/tcp   zealous_shaw
23f92b18e75b   nginx:1.14   "nginx -g 'daemon of…"   11 minutes ago       Up 11 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp     vigilant_mayer
#-------------------------------数据卷-------------
#(1)在宿主机创建好环境
[root@docker opt]# mkdir -p volume/a volume/b
[root@docker opt]# touch /opt/volume/a/a.txt /opt/volume/b/b.txt
[root@docker volume]# tree
.
├── a
│   └── a.txt
└── b
    └── b.txt
#(2)生成数据卷容器
[root@docker volume]# docker run -it --name "nginx_volumes" -v /opt/volume/a:/opt/a -v /opt/volume/b:/opt/b centos /bin/bash
#(3)挂载数据卷容器(--volumes-from)
[root@docker volume]# docker run -d -p 8085:80 --volumes-from nginx_volumes --name "n8085" nginx:1.14
ff6194215711b679e581ab65c58e30e1aa41c7871945986540e9adb8944acd31
[root@docker volume]# docker run -d -p 8086:80 --volumes-from nginx_volumes --name "n8086" nginx:1.14
7cfc2255bd5c0d763400142d32ad0ba9750a9a6c4859e8f875727b3f955446cb
#--------------------------------------制作本地局域网yum源
#(1)安装vsftpd软件
[root@docker volume]# yum install -y vsftpd
[root@docker volume]# systemctl start vsftpd
[root@docker volume]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
#(2)上传系统至虚拟机
#(3)配置yum仓库
[root@docker mnt]# mkdir -p /var/ftp/centos7
[root@docker mnt]# mount /mnt/CentOS-7-x86_64-DVD-2003.iso /var/ftp/centos7/
mount: /dev/loop0 is write-protected, mounting read-only
[root@docker mnt]# ls /var/ftp/centos7/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

错误1

[root@docker ~]# docker pull centos7
Using default tag: latest
Error response from daemon: pull access denied for centos7, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

解决方法

1.使用docker login进行docker用户名,密码登录(注:如无用户,在[Docker Hub](https://hub.docker.com/)上注册)
2.有可能是后面的镜像名称错误

错误2

#删除Centos镜像,报错
[root@docker ~]# docker image rm centos
Error response from daemon: conflict: unable to remove repository reference "centos" (must force) - container b2ea3d62e02c is using its referenced image 5d0da3dc9764

解决方法

1.因有容器正在使用当前镜像运行,需先停止容器的运行,然后再进行镜像删除
2.可以使用docker image rm -f centos强制删除

以上是关于Linux学习--Docker学习的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习--Docker学习

markdown [Docker] Docker片段列表和命令#linux #docker #snippets

docker学习

向Linus学习,让代码具有good taste

Linux学习----docker使用

Docker学习1使用 Linux(CentOS7)搭建 Docker 基础环境