0


8.26-docker创建容器+打包镜像+docker文件的学习

一、回顾

创建容器:docker run -it --name a1 centos:latest /bin/bash 

查看容器:docker ps(查看正在up的容器) 

 docker ps -a(查看所有的容器) 

切回宿主机:ctrl p q 

启动容器:docker start a1 

停止容器:docker stop a1 

删除容器:docker rm a1 

 docker rm -f a1(强制删除容器) 

如果容器正在运行,容器是无法删除的,如果容器没有删除,对应的镜像也无法删除 

镜像的迁移: 

save:docker save -o centos.tar centos:latest  

load:docker load -i centos.tar 

生成镜像: 

export(通过容器生成tar包):docker export -o centos_httpd.tar a1  

import(通过tar引入镜像):docker import -m httpd centos_httpd.tar centos:httpd

获得镜像的ip地址: 

1、进入镜像的bash:yum -y install iprouter,net-tools 

2、使用exec指令:dockers exec a1 yum -y install iprouter,net-tools 

 docker exec a1 ip add 

3、直接使用inspect命令:docker inspect a1 

端口映射 -p: 

docker run -it --name a2 -p80:80 centos:latest /bin/bash 

docker run -it --name a2 -p80 centos:latest /bin/bash 宿主会指定一个大于32767的端口给容器 

docker ps -a 查看实际给予的端口 

docker run -it --name a2 -p10.0.0.101::80 centos:latest /bin/bash 

数据持久化: 

docker run -it --name a2 -v /zhenji:/rongqi centos:latest /bin/bash 

二、部署一个考试项目

# 1、将资源上传到服务器
[root@docker ~]# yum -y install unzip
[root@docker ~]# rz -E
rz waiting to receive.
[root@docker ~]# unzip project_exam_system.zip 

# 2、创建基础镜像
[root@docker ~]# systemctl start docker
[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Image is up to date for centos:latest
docker.io/library/centos:latest

# 3、在容器中修改yum源
[root@docker ~]# docker run -it --name a0 centos:latest /bin/bash
[root@d1d92bed0d9a /]# rm -rf /etc/yum.repos.d/*
[root@d1d92bed0d9a /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   5898      0 --:--:-- --:--:-- --:--:--  5898
[root@d1d92bed0d9a /]# yum clean all && yum makecache 

# 4、在容器中安装epel
[root@d1d92bed0d9a /]# yum -y install epel-release

# 5、在容器中安装nginx
[root@d1d92bed0d9a /]# yum -y install nginx

# 6、ctrl p q退出容器
[root@d1d92bed0d9a /]# [root@docker ~]# 

# 7、导出tar包
[root@docker ~]# docker export -o centos_nginx.tar a0
[root@docker ~]# ls
anaconda-ks.cfg         centos.tar      project_exam_system.sql        yum_httpd.tar
application.properties  centos_yum.tar  Project_ExamSystem-V1.0.0.war
centos_nginx.tar        dist            project_exam_system.zip

# 8、引入tar包,成为镜像
[root@docker ~]# docker import -m "nginx" centos_nginx.tar centos:nginx
sha256:a0a4655eb590b16aace6c33d78862992ccdf615f1d517d236d173ff501086ff8

# 9、查看镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       nginx     a0a4655eb590   18 seconds ago   366MB
centos       httpd     92dfb2054854   3 days ago       310MB
centos       yum       d79f0f2ff79e   3 days ago       260MB
mysql        latest    a82a8f162e18   4 weeks ago      586MB
centos       latest    5d0da3dc9764   2 years ago      231MB

# 10、删除容器
[root@docker ~]# docker rm -f a0
a0

# 11、创建新的容器,在创建的同时指定端口,以及挂载目录
[root@docker ~]# docker run -it --name a0 -p80:80/tcp -v /opt/:/usr/share/nginx/html centos:nginx /bin/bash

# 12、在容器内部启动nginx服务
[root@8dea218de2f7 /]# nginx

# 13、curl p q退出镜像
[root@8dea218de2f7 /]# [root@docker ~]# 
[root@docker ~]# ls /opt
containerd

# 14、在宿主机上把dist目录中的文件复制到opt目录下
[root@docker ~]# cp -r dist/* /opt/
[root@docker ~]# ls /opt/
assets  containerd  favicon.ico  index.html
[root@docker ~]# curl localhost
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vite App</title>
    <script type="module" crossorigin src="/assets/index-C4kAShR5.js"></script>
    <link rel="stylesheet" crossorigin href="/assets/index-CSz7ARPP.css">
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
[root@docker ~]# systemctl stop firewalld
[root@docker ~]# systemctl disable firewalld
# 15、浏览器查看并登录

三、mysql镜像管理

# 拉取mysql镜像
[root@docker ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
6e839ac3722d: Pull complete 
ad912193ad5f: Pull complete 
25d13d87fd8d: Pull complete 
004d383c75ef: Pull complete 
6d9bbc82a0b8: Pull complete 
81fec07ea550: Pull complete 
83357cb2d3a5: Pull complete 
8ffe968b82c1: Pull complete 
30dfd9a7ed57: Pull complete 
35844ae33cbe: Pull complete 
Digest: sha256:86cdfe832c81e39a89cfb63c3fde1683c41cc00ef91e67653c9c1df0ba80f454
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

# 查看镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       httpd     92dfb2054854   3 days ago    310MB
centos       yum       d79f0f2ff79e   3 days ago    260MB
mysql        latest    a82a8f162e18   4 weeks ago   586MB
centos       latest    5d0da3dc9764   2 years ago   231MB

# 使用mysql镜像创建一个名为mysql01的容器,密码为root,用户为root,端口为3306
[root@docker ~]# docker run -d --name mysql01 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:latest
87136ab0b9a66e8d11be350cc11848db7bec4f914e45b889f77ff00aaa5b50fb

# -d 后台运行
# -e MYSQL_ROOT_PASSWRD=root 设置mysql密码
# -p 映射3306端口

# 查看up状态的容器
[root@docker ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND                   CREATED       STATUS       PORTS                                                  NAMES
87136ab0b9a6   mysql:latest   "docker-entrypoint.s…"   3 hours ago   Up 3 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql01

[root@docker ~]# docker exec -it mysql01 mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 9.0.1 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye
 
 
 运行sql文件

查看数据库和表

mysql> show databases;
+---------------------+
| Database            |
+---------------------+
| information_schema  |
| mysql               |
| performance_schema  |
| project_exam_system |
| sys                 |
+---------------------+
5 rows in set (0.00 sec)

mysql> use project_exam_system
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------------------+
| Tables_in_project_exam_system |
+-------------------------------+
| admin                         |
| category                      |
| com_city                      |
| com_nation                    |
| course                        |
| dept                          |
| dept_student                  |
| exam                          |
| exam_dept                     |
| exam_process                  |
| loginlog                      |
| major                         |
| menu                          |
| paper                         |
| paper_topic                   |
| role                          |
| role_admin                    |
| role_menu                     |
| semester                      |
| student                       |
| teacher                       |
| topic                         |
| user                          |
+-------------------------------+
23 rows in set (0.00 sec)

mysql> select * from user;
+-----+--------------+----------+--------+---------------------+----------+---------------------+--------+-------+
| id  | userName     | password | status | createTime          | createOr | editTime            | editOr | isDel |
+-----+--------------+----------+--------+---------------------+----------+---------------------+--------+-------+
|   1 | 21001189     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-22 22:04:44 | NULL   |     0 |
|   2 | 21001190     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-21 09:45:05 | NULL   |     0 |
|   3 | 21001191     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-21 09:45:05 | NULL   |     0 |
|   4 | 21001192     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-21 09:45:05 | NULL   |     0 |
|   5 | 21001193     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-21 09:45:05 | NULL   |     0 |
|   6 | 21001194     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-21 09:45:05 | NULL   |     0 |
|   7 | 21001195     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-21 09:45:05 | NULL   |     0 |
|   8 | 21001196     | 111111   |      0 | 2024-08-21 09:45:05 | NULL     | 2024-08-21

四、让容器和宿主机时间一致

让容器共享宿主机的/etc/localtime

[root@docker ~]# docker run -it --name c8 -v /etc/localtime:/etc/localtime centos:latest /bin/bash
[root@08f5525f8d55 /]# date
Mon Aug 26 14:39:46 CST 2024

五、exec语句

# 创建名称为c8的容器
[root@docker ~]# docker run -it --name c8 -v /etc/localtime:/etc/localtime centos:latest /bin/bash

# 退出容器不中断-ctrl+p+q
[root@08f5525f8d55 /]# [root@docker ~]# 

# 使用exec命令执行ls命令
[root@docker ~]# docker exec c8 ls /
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

# 使用exec命令执行yum clean all && yum makecache这条命令
[root@docker ~]# docker exec c8  yum clean all && yum makecache
0 files removed
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                           | 5.1 kB  00:00:00     
 * base: mirrors.aliyun.com
 * epel: repo.jing.rocks
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
a                                                              | 3.6 kB  00:00:00     
base                                                           | 3.6 kB  00:00:00     
docker-ce-stable                                               | 3.5 kB  00:00:00     
extras                                                         | 2.9 kB  00:00:00     
updates                                                        | 2.9 kB  00:00:00     
(1/13): a/other_db                                             | 2.5 MB  00:00:00     
(2/13): a/filelists_db                                         | 6.9 MB  00:00:01     
(3/13): docker-ce-stable/7/x86_64/filelists_db                 |  66 kB  00:00:00     
(4/13): docker-ce-stable/7/x86_64/other_db                     | 145 kB  00:00:01     
(5/13): epel/x86_64/prestodelta                                |  592 B  00:00:01     
(6/13): extras/7/x86_64/filelists_db                           | 305 kB  00:00:01     
(7/13): extras/7/x86_64/other_db                               | 154 kB  00:00:01     
(8/13): base/7/x86_64/other_db                                 | 2.6 MB  00:00:08     
(9/13): updates/7/x86_64/other_db                              | 1.6 MB  00:00:09     
(10/13): base/7/x86_64/filelists_db                            | 7.2 MB  00:00:34     
(11/13): updates/7/x86_64/filelists_db                         |  15 MB  00:00:59     
epel/x86_64/filelists_db       FAILED                                          07 ETA 
(12/13): epel/x86_64/other_db                                  | 4.1 MB  00:08:03     
(13/13): epel/x86_64/filelists_db                              |  15 MB  00:05:49     
元数据缓存已建立
  
  ​
  ​

六、容器之间的依赖

容器域名

容器的ip不稳定

加link模块

--link  容器名:别名
# 查看容器
[root@docker ~]# docker ps -all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# 创建名为m0容器
[root@docker ~]# docker run -d --name m0 -e MYSQL_ROOT_PASSWORD=root mysql:latest 
e6390afa961b4a3d8a3b3ff1e204073c0af4c8fed96198441d58f775e982c1e1

# 查看m0容器的ip
[root@docker ~]# docker inspect m0
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": null
            
# 使用link模块为m0容器创建名为mysqldb依赖的容器
[root@docker ~]# docker run -it --name c0 --link m0:mysqldb centos:latest  /bin/bash

# 在这个容器中pingmysqldb,会发现ping到的地址是m0的容器
[root@6c8098650212 /]# ping mysqldb
PING mysqldb (172.17.0.2) 56(84) bytes of data.
64 bytes from mysqldb (172.17.0.2): icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from mysqldb (172.17.0.2): icmp_seq=2 ttl=64 time=0.081 ms
^C
--- mysqldb ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.063/0.072/0.081/0.009 ms

# 在容器中查看etc/hosts文件 就看到mysqldb对应的地址是m0的地址
[root@6c8098650212 /]# cat etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    mysqldb e6390afa961b m0
172.17.0.3    6c8098650212

# 退出容器不中断
[root@6c8098650212 /]# [root@docker ~]# 

# 查看所有容器
[root@docker ~]# docker ps --all
CONTAINER ID   IMAGE           COMMAND                   CREATED         STATUS         PORTS                 NAMES
6c8098650212   centos:latest   "/bin/bash"               3 minutes ago   Up 3 minutes                         c0
e6390afa961b   mysql:latest    "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes   3306/tcp, 33060/tcp   m0

# 停止容器m0的运行,172.17.0.2这个ip地址就被让出来了
[root@docker ~]# docker stop m0
m0

# 重新创建c1容器
[root@docker ~]# docker run -it --name c1 centos:latest  /bin/bash

# 退出不中断
[root@337a08b8a20a /]# [root@docker ~]# 

# 查看容器c1的ip地址,就会看到c1占用了172.17.0.2这个地址
[root@docker ~]# docker inspect c1
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": null
                    
# 启动m0容器
[root@docker ~]# docker start m0
m0

# 查看m0的容器的地址,就看发现它的地址变成了172.17.0.4
[root@docker ~]# docker inspect m0
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": null
                    
# 挂载c0容器的终端
[root@docker ~]# docker attach c0

# 继续pingmysqldb,发现地址变成了172.17.0.4(m0现在的地址)
[root@6c8098650212 /]# ping mysqldb
PING mysqldb (172.17.0.4) 56(84) bytes of data.
64 bytes from mysqldb (172.17.0.4): icmp_seq=1 ttl=64 time=0.111 ms
64 bytes from mysqldb (172.17.0.4): icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from mysqldb (172.17.0.4): icmp_seq=3 ttl=64 time=0.101 ms
^C
--- mysqldb ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.052/0.088/0.111/0.025 ms

# 在c0容器中查看/etc/hosts文件,发现里面的地址也变成了172.17.0.4
[root@6c8098650212 /]# cat etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.4    mysqldb e6390afa961b m0
172.17.0.3    6c8098650212

# 退出不中断c0容器
[root@6c8098650212 /]# read escape sequence

# 查看所有容器
[root@docker ~]# docker ps --all
CONTAINER ID   IMAGE           COMMAND                   CREATED          STATUS         PORTS                 NAMES
337a08b8a20a   centos:latest   "/bin/bash"               5 minutes ago    Up 5 minutes                         c1
6c8098650212   centos:latest   "/bin/bash"               9 minutes ago    Up 9 minutes                         c0
e6390afa961b   mysql:latest    "docker-entrypoint.s…"   11 minutes ago   Up 3 minutes   3306/tcp, 33060/tcp   m0
[root@docker ~]# docker stop c0
c0
[root@docker ~]# docker stop c1
c1
[root@docker ~]# docker stop m0
m0

# 因为c0依赖m0,所以先启动被依赖的
[root@docker ~]# docker start c0
Error response from daemon: Cannot link to a non running container: /m0 AS /c0/mysqldb
Error: failed to start containers: c0

# 先启动被依赖的容器
[root@docker ~]# docker start m0
m0

# 再启动主动依赖的容器
[root@docker ~]# docker start c0
c0

小结:docker容器的ip地址不固定,如果要作为服务器使用,就必须能够直接访问服务,不能直接使用ip,为容器设置域名,做端口映射也能保证,确定是一旦停用之后端口被占用,就无法再次启动了

2.doker run --link ,容器的名称或者是id:自定义域名

3.被link容器必须处于启动状态,否则当前容器无法启

七、docker文件

停止docker服务
[root@docker ~]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
  
# 进到/var/lib/docker目录下
[root@docker ~]# cd /var/lib/docker/
[root@docker docker]# ls
buildkit    engine-id  network   plugins   swarm  volumes
containers  image      overlay2  runtimes  tmp

# 进到overlay2目录下
[root@docker docker]# cd overlay2/

# 这些就是镜像和创建的容器
[root@docker overlay2]# ls
00332a9bd8355d76ca2d7273fc36abc55005c3f7e9ea99de4f28cc0f8d671c70
0e3f2d728c00d19f087019b9c153cb64e545733396b74552ba291cba3bfd0179
2fd26d35d6b387e52918f623dd35ebf137bdf660811fca395878d0f1a8cb0cb2
4448856ec7b6f553a449484292e7d7e428146e741b0443eae3bbddecc8d8954a
4448856ec7b6f553a449484292e7d7e428146e741b0443eae3bbddecc8d8954a-init
45a913457c95069ec4ee6c661fed0550de5e47264d04dc87975125bc4e97028f
60266e2b47ee1c7aae2cd1357f4dc5cac79042838a826fe1f9614dca8a8f948a
629bd34c6a92d5cb2b14fbbde998e2ad8e1fa3082d606589711d19781aabd43d
6f4cc31b4373e6edc88448de17eadcb7305279a61426b6463ff73e36f128e9a4
7b1ebd4b9c0adff07edfe76ff1a9ee30cf8800086dd955e923d3de349a59e6ad
7b1ebd4b9c0adff07edfe76ff1a9ee30cf8800086dd955e923d3de349a59e6ad-init
8cc2d6fcd7fd4102bbc47d61e2c804bfe3df6e945ba1ab09958dfbb9d51b35d1
8cc2d6fcd7fd4102bbc47d61e2c804bfe3df6e945ba1ab09958dfbb9d51b35d1-init
98021934d740dcde71e3cc6f1b6e6d2d4ccb829100600fec612e186fedf6ca02
9c7e62ffb3fa89bc9cc947020fd8c94a2b09401aa0ace4748bbef9f9cca8da4f
a2a47749e86aded67a82094e63c639a43bcb48c83e561cbb8096fb04fa11d23c
backingFsBlockDev
c1276ed5fa7657bf06ab97225ac522057bfbe0532db3b733455d3fa68d44344a
c1276ed5fa7657bf06ab97225ac522057bfbe0532db3b733455d3fa68d44344a-init
c69ce12a6a86fbf63f1efbc20aa7a1609834a2bb66b665b4c1c28e2ecc8ce886
d7985a6af5f182bf09f004ad9cbd578d3b2775821c4d1569ca10bdc9390dbea9
eed0e720b5b09a7840b16e666f10bd2bb16d00dc465cf414fd72bdf3c6fd9f49
eed0e720b5b09a7840b16e666f10bd2bb16d00dc465cf414fd72bdf3c6fd9f49-init
f538b28aac30b79bd465d6628306db7cf148b227b987a2fdead5b1f9aae6f16d
f9271f08918724184b1bdb5c22157def0fb7ac8569a59de9df953e1d79d32092
l
[root@docker overlay2]# cd

# 删掉/var/lib/docker/这个目录
[root@docker ~]# rm -rf /var/lib/docker/

# 启动docker服务
[root@docker ~]# systemctl start docker

# 这个文件又会被重新创建
[root@docker ~]# ls /var/lib/docker/
buildkit    engine-id  network   plugins   swarm  volumes
containers  image      overlay2  runtimes  tmp

# 查看镜像-发现没有
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

# 查看容器-也没有
[root@docker ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# 查看/var/lib/docker/overlay2/目录,只有这两个文件
[root@docker ~]# ls /var/lib/docker/overlay2/
backingFsBlockDev  l

# 上传centos镜像
[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

# 再查看这个目录,就会发现有变化
[root@docker ~]# ls /var/lib/docker/overlay2/
951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5  l
backingFsBlockDev

# 查看这个镜像中有什么
[root@docker ~]# ls /var/lib/docker/overlay2/951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5/
diff  link

# 查看镜像的diff文件,会发现和linux系统一样
[root@docker ~]# ls /var/lib/docker/overlay2/951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5/diff
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

# 使用centos镜像创建容器
[root@docker ~]# docker run -it --name c0 centos:latest /bin/bash

# 使用ctrl+p+q退出容器
[root@d854380d0f54 /]# [root@docker ~]# 

# 再次查看这个文件看有什么变化
[root@docker ~]# ls /var/lib/docker/overlay2/
951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5
backingFsBlockDev
f893e9d148c1ca1c5ed9e950eb1217c2aef1b89457a7407ffcd35a51f67a99f7
f893e9d148c1ca1c5ed9e950eb1217c2aef1b89457a7407ffcd35a51f67a99f7-init
l
[root@docker ~]# ls /var/lib/docker/overlay2/f893e9d148c1ca1c5ed9e950eb1217c2aef1b89457a7407ffcd35a51f67a99f7
diff  link  lower  merged  work

# 使用docker的exec命令给c0容器创建一个zhangsan.txt文件
[root@docker ~]# docker exec c0 dd if=/dev/zero of=/zhangsan.txt bs=10M count=100
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 1.16346 s, 901 MB/s

# 查看c0容器的文件,就会看到创建的zhangsan.txt文件
[root@docker ~]# ls /var/lib/docker/overlay2/f893e9d148c1ca1c5ed9e950eb1217c2aef1b89457a7407ffcd35a51f67a99f7/merged/
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr  zhangsan.txt
[root@docker ~]# 

小结:

1.在docker daemon初始状态(没有状态的时候)/var/lib/docker不存在

2.当docker daemon服务器自启动创建docker目录

3.在没有镜像的情况下,查看overlay2这个目录是只有两个文件,管道文件,目录(管理镜像和容器的软链接的目录)

4.当我们pull一个镜像之后 /var/lib/docker/overlay2 下就有一个新的目录,这个目录就是镜像的基础目录,这个新的目录就是镜像,就是不可读的层

八、基础镜像制作

1.进程目录

[root@localhost ~]# ls /proc/
1     1531  279  385  5    657        crypto       kmsg          scsi
10    1535  28   386  512  664        devices      kpagecount    self
1014  1554  281  387  560  673        diskstats    kpageflags    slabinfo
1016  16    282  388  562  677        dma          loadavg       softirqs
1017  17    285  389  564  693        driver       locks         stat
11    18    286  390  565  7          execdomains  mdstat        swaps
1175  19    288  391  567  8          fb           meminfo       sys
1181  2     29   392  569  88         filesystems  misc          sysrq-trigger
1288  20    3    393  57   9          fs           modules       sysvipc
13    21    30   394  574  acpi       interrupts   mounts        timer_list
1316  263   359  40   578  asound     iomem        mpt           timer_stats
14    265   360  41   616  buddyinfo  ioports      mtrr          tty
15    268   370  42   639  bus        irq          net           uptime
1506  27    371  44   642  cgroups    kallsyms     pagetypeinfo  version
1511  275   38   460  643  cmdline    kcore        partitions    vmallocinfo
1529  276   383  483  644  consoles   keys         sched_debug   vmstat
1530  277   384  494  647  cpuinfo    key-users    schedstat     zoneinfo

2.sys目录是内核文件保存位置,不允许修改

[root@localhost ~]# cd /sys/
[root@localhost sys]# mkdir abc
mkdir: 无法创建目录"abc": 不允许的操作
[root@localhost sys]# 

3.排除以上两个目录,远程打包其他目录

[root@localhost~]# tar --numeric-owner --exclude=/proc --exclude=/sys -cvf haha.tar /

# 将打包好的镜像传到docker主机上
[root@localhost~]# scp haha.tar [email protected]:~

4.加载镜像

[root@docker ~]# ls
anaconda-ks.cfg         haha.tar    
[root@docker ~]# docker import -m "维护世界和平" haha.tar ha:v0

5.查看镜像

[root@docker ~]# docker images

6.使用镜像创建容器

[root@docker ~]# docker run -it --name ha01 ha:v0 /bin/bash
[root@5eaf8dd31774 /]# ls
bin  etc   lib      lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@5eaf8dd31774 /]# cd
[root@5eaf8dd31774 ~]# ls
anaconda-ks.cfg  anaconda-post.log  original-ks.cfg
[root@5eaf8dd31774 ~]# cd /etc/yum.repos.d/
[root@5eaf8dd31774 yum.repos.d]# ls
CentOS-Base.repo   epel-playground.repo       epel-testing.repo
epel-modular.repo  epel-testing-modular.repo  epel.repo

九、应用镜像

将容器导出镜像-commit

[root@docker ~]# ls /var/lib/docker/overlay2/
951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5  l
backingFsBlockDev

# 创建c0容器
[root@docker ~]# docker run -it --name c0 centos:latest /bin/bash

# 清空仓库
[root@200554f77ada /]# rm -rf /etc/yum.repos.d/*

# 安装阿里云镜像
[root@200554f77ada /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   4137      0 --:--:-- --:--:-- --:--:--  4130

# 清理且建立缓存
[root@200554f77ada /]# yum clean all && yum makecache
Failed to set locale, defaulting to C.UTF-8
0 files removed
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com           591 kB/s | 4.6 MB     00:07    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com         9.7 kB/s |  10 kB     00:01    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com      406 kB/s | 8.4 MB     00:21    
Metadata cache created.

# 安装epel
[root@200554f77ada /]# yum -y install epel-release

# 安装httpd
[root@200554f77ada /]# yum -y install httpd 

# 退出不中断
[root@200554f77ada /]# [root@docker ~]# 

# 导出httpd镜像
[root@docker ~]# docker commit c0 centos:httpd
sha256:5fa4e3480590e0548419b770a52acf565729c1795cba386315a484e05a5c1e4d
[root@docker ~]# ls /var/lib/docker/overlay2/
4364fe8cf78dd976b842e3d6f7832640b8fd624928665cff2fa4b3d57506490c
951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5
backingFsBlockDev
d94e1199536123c1dfb61d7de10b9d599898e59bf627005939d8392cb1da526f
d94e1199536123c1dfb61d7de10b9d599898e59bf627005939d8392cb1da526f-init
l

# 停止c0容器
[root@docker ~]# docker stop c0
c0
[root@docker ~]# ls /var/lib/docker/overlay2/
4364fe8cf78dd976b842e3d6f7832640b8fd624928665cff2fa4b3d57506490c
951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5
backingFsBlockDev
d94e1199536123c1dfb61d7de10b9d599898e59bf627005939d8392cb1da526f
d94e1199536123c1dfb61d7de10b9d599898e59bf627005939d8392cb1da526f-init
l

# 删除c0容器
[root@docker ~]# docker rm c0
c0
[root@docker ~]# ls /var/lib/docker/overlay2/
4364fe8cf78dd976b842e3d6f7832640b8fd624928665cff2fa4b3d57506490c  backingFsBlockDev
951ab11df405924a3e43fc6b1f56b20b528152a937aaa863c6203305794520a5  l

# 查看镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
centos       httpd     5fa4e3480590   About a minute ago   320MB
centos       latest    5d0da3dc9764   2 years ago          231MB
[root@docker ~]# ls /var/lib/docker/overlay2/4364fe8cf78dd976b842e3d6f7832640b8fd624928665cff2fa4b3d57506490c/diff/
etc  run  tmp  usr  var

# 查看httpd镜像的仓库
[root@docker ~]# ls /var/lib/docker/overlay2/4364fe8cf78dd976b842e3d6f7832640b8fd624928665cff2fa4b3d57506490c/diff/etc/yum.repos.d/
CentOS-Base.repo                     CentOS-Linux-Media.repo
CentOS-Linux-AppStream.repo          CentOS-Linux-Plus.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-PowerTools.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Sources.repo
CentOS-Linux-Debuginfo.repo          epel-modular.repo
CentOS-Linux-Devel.repo              epel-playground.repo
CentOS-Linux-Extras.repo             epel.repo
CentOS-Linux-FastTrack.repo          epel-testing-modular.repo
CentOS-Linux-HighAvailability.repo   epel-testing.repo
标签: docker 学习 容器

本文转载自: https://blog.csdn.net/qq_70752758/article/details/141572029
版权归原作者 堕入温柔~ 所有, 如有侵权,请联系我们删除。

“8.26-docker创建容器+打包镜像+docker文件的学习”的评论:

还没有评论