docker 镜像

docker 镜像


文章目录

  • docker 镜像
    • 1.docker镜像的制作
      • 1.1Docker Hub
      • 1.2docker镜像的获取‎
      • 1.3镜像的生成
      • 1.4基于容器制作镜像
    • 2.镜像的导入与导出

1.docker镜像的制作


多数情况下,我们做镜像是基于别人已存在的某个基础镜像来实现的,我们把它称为base image。比如一个纯净版的最小化的centos、ubuntu或debian。

那么这个最小化的centos镜像从何而来呢?其实这个基础镜像一般是由Docker Hub的相关维护人员,也就是Docker官方手动制作的。这个基础镜像的制作对于Docker官方的专业人员来说是非常容易的,但对于终端用户来说就不是那么容易制作的了。

1.1Docker Hub

‎Docker Hub 是一项基于云的注册表服务,允许您链接到代码存储库、构建映像并对其进行测试、存储手动推送的映像以及指向 Docker Cloud 的链接,以便将映像部署到主机。‎

‎它为容器映像发现、分发和更改管理、用户和团队协作以及整个开发管道中的工作流自动化提供了集中式资源。‎
docker 镜像_第1张图片

‎Docker Hub 提供以下主要功能:‎

  • ‎图像存储库‎
    - 从社区和官方库中查找和提取映像,以及管理、推送到您有权访问的私有映像库以及从中拉取映像库。‎
  • 自动构建‎
    • 在对源代码存储库进行更改时自动创建新映像。‎
  • ‎网络钩子‎
    • Webhooks 是自动构建的一项功能,可让您在成功推送到存储库后触发操作。
  • 组织‎
    • ‎创建工作组以管理对映像存储库的访问
  • GitHub 和 Bitbucket 集成‎
    • ‎将中心和 Docker 映像添加到当前工作流。‎

1.2docker镜像的获取‎

‎要远程注册表(例如您自己的 Docker 注册表)获取 Docker 映像并将其添加到本地系统,请使用 docker pull 命令:‎

# docker pull [:]/[/]:

是一个在 TCP 上提供 docker 分发服务的主机‎‎(默认值:5000)‎

‎一起 ‎识别由 ‎注册表‎‎控制的特定映像‎

  • 一些注册表也支持原始‎‎;对于这些, ‎‎ 是可选的‎
    ‎- 但是,当它被包括在内时,提供的附加层次结构级别‎‎对于区分具有相同图像‎‎是有用的‎

    层次结构的附加级别‎
    |Namespace |‎示例(<命名空间>/<名称>)‎|
    |:—:—
    |‎组织‎ |红帽/kubernetes,谷歌/kubernetes
    |登录名(用户名)| 爱丽丝/应用程序,鲍勃/应用程序
    |‎角色‎| ‎开发/数据库,测试/数据库,生产/数据库‎

1.3镜像的生成

镜像的生成途径:

  • Dockerfile
  • 基于容器制作
  • Docker Hub automated builds
    docker 镜像_第2张图片

1.4基于容器制作镜像

‎根据容器的更改创建新映像‎

‎用法:‎

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

|选项‎| ‎违约|‎ ‎描述‎‎
|:—:—
|—作者, -a‎ | ‎|作者(例如,“约翰·汉尼拔·史密斯 ‎‎[email protected]‎")
|‎-c, --更改列表‎| |‎将 Dockerfile 指令应用于创建的映像‎
|‎-m, --消息字符串‎| |‎提交消息‎
|‎-p, --暂停‎| ‎真‎ |‎在提交期间暂停容器‎

提取官方镜像

[root@localhost ~]# 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@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB

使用镜像创建容器

[root@localhost ~]# docker run -d -it --name a1 centos
f0ac8b527432390e3c7016fe7d8766c271f0607e1331685c4289935c521b78ee
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
f0ac8b527432   centos    "/bin/bash"   8 seconds ago   Up 7 seconds             a1

在创建容器的时候,我们不能关闭,必须运行状态,然后我们必须启动另一个终端,执行
在主机上基于容器生成新的镜像

[root@localhost ~]# docker commit -a 'yhm1206 <[email protected]>'  -c 'CMD ["/root/httpd.sh"]' -p a1 centos-httpd:v3
sha256:1f7be7c765bd03a53772f66fb871573e8a8a454b4a7d3ba91757398ade7f609b

上传镜像,此时要注意的是,如果我们的仓库名叫centos,那么我们要在Docker Hub上创建一个名为centos的仓库,然后再将我们做好的镜像push上去

[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: yhm1206
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]# docker tag centos-httpd:v3 yhm1206/centos-httpd:v3
[root@localhost ~]# docker push yhm1206/centos-httpd:v3
The push refers to repository [docker.io/yhm1206/centos-httpd]
74ddd0ec08fa: Mounted from library/centos 
v3: digest: sha256:da3783da2429713991310c435f8a494ad80286cd10013ceb4c96324143f60fb5 size: 529

制作一个默认运行apache服务的镜像
提取镜像并搭建仓库

[root@localhost ~]# 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@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB
[root@localhost ~]# docker run -d -it --name a1 centos
f0ac8b527432390e3c7016fe7d8766c271f0607e1331685c4289935c521b78ee
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
f0ac8b527432   centos    "/bin/bash"   8 seconds ago   Up 7 seconds             a1
[root@localhost ~]# docker exec -it a1 /bin/bash
[root@f0ac8b527432 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

将所需要的包拷贝至容器中
注:建议另开一个终端继续以下操作,方便实时观察容器内情况

[root@localhost ~]# mkdir yhm
[root@localhost ~]# cd yhm
[root@localhost yhm]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2
[root@localhost yhm]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.bz2
[root@localhost yhm]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
[root@localhost yhm]# ls
apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.54.tar.bz2
[root@localhost yhm]# cd
[root@localhost ~]# docker cp yhm   a1:/usr/src 
[root@localhost ~]# docker exec -it a1 /bin/bash
[root@f0ac8b527432 /]# ls /usr/src/yhm/
apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.54.tar.bz2

开始在容器内编译安装apache

[root@f0ac8b527432 /]# cd /etc/yum.repos.d/
[root@f0ac8b527432 yum.repos.d]# ls 
CentOS-Linux-AppStream.repo
CentOS-Linux-BaseOS.repo
CentOS-Linux-ContinuousRelease.repo
CentOS-Linux-Debuginfo.repo
CentOS-Linux-Devel.repo
CentOS-Linux-Extras.repo
CentOS-Linux-FastTrack.repo
CentOS-Linux-HighAvailability.repo
CentOS-Linux-Media.repo
CentOS-Linux-Plus.repo
CentOS-Linux-PowerTools.repo
CentOS-Linux-Sources.repo
[root@f0ac8b527432 yum.repos.d]# rm -rf *
[root@f0ac8b527432 yum.repos.d]# 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
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- 100  2495  100  2495    0     0  22276      0 --:--:-- --:--:-- --:--:-- 22276
[root@f0ac8b527432 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@f0ac8b527432 yum.repos.d]# ls
CentOS-Base.repo
[root@f0ac8b527432 yum.repos.d]# dnf -y install bzip2
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirror 8.8 MB/s | 4.6 MB     00:00    
CentOS-8.5.2111 - Extras - mirr  84 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - m  10 MB/s | 8.4 MB     00:00    
Dependencies resolved.
================================================================
 Package     Architecture Version              Repository  Size
================================================================
Installing:
 bzip2       x86_64       1.0.6-26.el8         base        60 k

Transaction Summary
================================================================
Install  1 Package

Total download size: 60 k
Installed size: 91 k
Downloading Packages:
bzip2-1.0.6-26.el8.x86_64.rpm   523 kB/s |  60 kB     00:00    
----------------------------------------------------------------
Total                           517 kB/s |  60 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                        1/1 
  Installing       : bzip2-1.0.6-26.el8.x86_64              1/1 
  Running scriptlet: bzip2-1.0.6-26.el8.x86_64              1/1 
  Verifying        : bzip2-1.0.6-26.el8.x86_64              1/1 

Installed:
  bzip2-1.0.6-26.el8.x86_64                                     

Complete!
[root@f0ac8b527432 yum.repos.d]# cd
[root@f0ac8b527432 ~]# cd /usr/src/yhm/
[root@f0ac8b527432 yhm]# tar xf apr-1.6.5.tar.bz2 
[root@f0ac8b527432 yhm]# tar xf apr-util-1.6.1.tar.bz2 
[root@f0ac8b527432 yhm]# tar xf httpd-2.4.54.tar.bz2   
[root@f0ac8b527432 yhm]# ls
apr-1.6.5	   apr-util-1.6.1	   httpd-2.4.54
apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.54.tar.bz2
[root@f0ac8b527432 yhm]# dnf -y groups mark install 'Development Tools'
[root@f0ac8b527432 yhm]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++
[root@f0ac8b527432 yhm]# cd apr-1.6.5
[root@f0ac8b527432 apr-1.6.5]# vim configure

    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
 #    $RM "$cfgfile"   #注释此行
[root@f0ac8b527432 apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@f0ac8b527432 apr-1.6.5]# dnf -y install make
[root@f0ac8b527432 apr-1.6.5]#  make && make install
[root@f0ac8b527432 httpd-2.4.54]# cd ../apr-util-1.6.1
[root@f0ac8b527432 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@f0ac8b527432 apr-util-1.6.1]# make && make install
[root@f0ac8b527432 apr-util-1.6.1]# cd ../httpd-2.4.54
[root@f0ac8b527432 httpd-2.4.54]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite  --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
[root@f0ac8b527432 httpd-2.4.54]# make && make install

配置启动apache脚本

[root@f0ac8b527432 ~]# vi httpd.sh 
[root@f0ac8b527432 ~]# cat httpd.sh 
#!/bin/bash
/usr/local/apache/bin/httpd
sleep 10d
[root@f0ac8b527432 ~]# chmod +x httpd.sh

提交镜像

 **#方式一:**
[root@localhost ~]# docker commit -a 'yhm1206 <[email protected]>'  -c 'CMD ["/root/httpd.sh"]' -p a1 centos-httpd:v3
sha256:c63e78588bf4b87d085fc81501f4614b7ba0d9f487e1e41eaa729228c713a3a4
 **#方式二:**
 [root@localhost ~]# docker commit -a 'yhm1206 <[email protected]>'  -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]' -p a1 centos-httpd:v4
sha256:5b03d33389e3d6e7cf8ddd7fc8345103dcc235eeedf02a57fb520c52d1259c2e
[root@localhost ~]# docker images 
REPOSITORY             TAG       IMAGE ID       CREATED              SIZE
centos-httpd           v4        5b03d33389e3   About a minute ago   740MB
centos-httpd           v3        c63e78588bf4   2 minutes ago        740MB
centos                 latest    5d0da3dc9764   10 months ago        231MB
[root@localhost ~]#docker run -d -it --name a3 -p 80:80 centos-httpd:v3
88a997e50e6bf8648570e14b12bc8f8500909a4fa6b86239247ea9a9a6552706
[root@localhost ~]#docker run -d -it --name a4 -p 80:80 centos-httpd:v4
fb5835de12169bf6293a7b184dbdca208d0385f6109c73ca110896a4e5e380c8
[root@localhost ~]# docker inspect a3| grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
[root@localhost ~]# docker inspect a4| grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.4",
                    "IPAddress": "172.17.0.4",
[root@localhost ~]# curl 172.17.0.3

It works!

[root@localhost ~]# curl 172.17.0.4

It works!

a3容器映射了端口,可以真机访问

docker 镜像_第3张图片

2.镜像的导入与导出

假如有2台主机,我们在主机1上做了一个镜像,主机2想用这个镜像怎么办呢?

我们可以在主机1上push镜像到镜像仓库中,然后在主机2上pull把镜像拉下来使用,这种方式就显得比较麻烦,假如我只是测试用的,在一台主机上做好镜像后在另一台主机上跑一下就行了,没必要推到仓库上然后又把它拉到本地来。

此时我们可以在已有镜像的基础上把镜像打包成一个压缩文件,然后拷贝到另一台主机上将其导入,这就是镜像的导入和导出功能。

docker中我们使用docker save进行导出,使用docker load进行导入。
在已生成镜像的主机上执行docker save导出镜像

[root@localhost ~]# docker save -o centos-httpd.gz centos-httpd:v3
[root@localhost ~]# ls
anaconda-ks.cfg  centos-httpd.gz  yhm

在另一台没有镜像的主机上执行docker load导入镜像

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# cd
[root@localhost ~]# dnf -y install docker-ce --allowerasing
[root@localhost ~]# systemctl restart docker.service 
[root@localhost ~]# systemctl enable docker.service
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@localhost ~]#  mkdir -p /etc/docker
[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://dqigcl2s.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://dqigcl2s.mirror.aliyuncs.com"]
}
[root@localhost ~]# sudo systemctl daemon-reload
[root@localhost ~]# sudo systemctl restart docker
[root@localhost ~]# scp 'root'@'192.168.223.144':/root/centos-httpd.gz .
The authenticity of host '192.168.223.144 (192.168.223.144)' can't be established.
ECDSA key fingerprint is SHA256:n/8DOVIsQl5k/1iHFH2s5LllgoGLoZ1HeY9NHsYz9i0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.223.144' (ECDSA) to the list of known hosts.
[email protected]'s password: 
centos-httpd.gz                               100%  727MB 202.4MB/s   00:03    
[root@localhost ~]# ls
anaconda-ks.cfg  Downloads                                    
centos-httpd.gz  
[root@localhost ~]# docker load -i centos-httpd.gz 
74ddd0ec08fa: Loading layer  238.6MB/238.6MB
6d52fa1bab5d: Loading layer  523.6MB/523.6MB
Loaded image: centos-httpd:v3
[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
centos-httpd   v3        c63e78588bf4   51 minutes ago   740MB

你可能感兴趣的:(docker,linux,运维)