0
点赞
收藏
分享

微信扫一扫

基于容器制作镜像

圣杰 2022-04-27 阅读 57
docker

基于容器制作镜像
对容器进行更改从而创建一个新镜像
用法:

OptionsDefaultDescription
—author, -aAuthor (e.g., "John Hannibal Smith hannibal@a-team.com")
-c, --change list对创建的镜像应用 Dockerfile 指令
-m, --message string提交消息
-p, --pauseture在提交期间暂停容器

[root@localhost ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Image is up to date for busybox:latest
docker.io/library/busybox:latest
[root@localhost ~]# 
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
busybox      latest    beae173ccac6   3 months ago   1.24MB
httpd        latest    dabbfbe0c57b   4 months ago   144MB
[root@localhost ~]# docker run -it --name b1 busybox
/ # mkdir data
/ # echo 'hello world' > data/index.html
/ # cat /data/index.html 
hello world
[root@localhost ~]# docker commit -p b1
sha256:121711e0cdc98ac8573dd7ab953ce66dc6905a787a3965b1e82c5fabf9b23b3e
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    121711e0cdc9   15 seconds ago   1.24MB
busybox      latest    beae173ccac6   3 months ago     1.24MB
httpd        latest    dabbfbe0c57b   4 months ago     144MB
[root@localhost ~]# docker tag  121711e0cdc9 busybox:v0.1
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
busybox      v0.1      121711e0cdc9   6 minutes ago   1.24MB
busybox      latest    beae173ccac6   3 months ago    1.24MB
httpd        latest    dabbfbe0c57b   4 months ago    144MB
[root@localhost ~]# docker tag 121711e0cdc9 xwsxkx/b1:v0.1
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
busybox      v0.1      121711e0cdc9   37 minutes ago   1.24MB
xwsxkx/b1    v0.1      121711e0cdc9   37 minutes ago   1.24MB
busybox      latest    beae173ccac6   3 months ago     1.24MB
httpd        latest    dabbfbe0c57b   4 months ago     144MB

此时要注意的是,我们的仓库名叫b1,所以我们要在Docker Hub上创建一个名为b1的仓库,然后再将我们做好的镜像push上去

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
busybox      v0.1      121711e0cdc9   37 minutes ago   1.24MB
xwsxkx/b1    v0.1      121711e0cdc9   37 minutes ago   1.24MB
busybox      latest    beae173ccac6   3 months ago     1.24MB
httpd        latest    dabbfbe0c57b   4 months ago     144MB
[root@localhost ~]# 
[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: xwsxkx
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 images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
busybox      v0.1      121711e0cdc9   44 minutes ago   1.24MB
xwsxkx/b1    v0.1      121711e0cdc9   44 minutes ago   1.24MB
busybox      latest    beae173ccac6   3 months ago     1.24MB
httpd        latest    dabbfbe0c57b   4 months ago     144MB
[root@localhost ~]# docker push busybox:v0.1
The push refers to repository [docker.io/library/busybox]
2386f9cfd5cf: Preparing 
01fd6df81c8e: Preparing 
denied: requested access to the resource is denied
[root@localhost ~]# docker tag busybox:v0.1 xwsxkx/busybox:042601[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
xwsxkx/b1        v0.1      121711e0cdc9   45 minutes ago   1.24MB
xwsxkx/busybox   042601    121711e0cdc9   45 minutes ago   1.24MB
busybox          v0.1      121711e0cdc9   45 minutes ago   1.24MB
busybox          latest    beae173ccac6   3 months ago     1.24MB
httpd            latest    dabbfbe0c57b   4 months ago     144MB
[root@localhost ~]# docker push xwsxkx/busybox:042601
The push refers to repository [docker.io/xwsxkx/busybox]
2386f9cfd5cf: Pushed 
01fd6df81c8e: Mounted from library/busybox 
042601: digest: sha256:d07d5284b64c638e51740d00ba86182b343f7fd6c2a44f39e14ee198a0ca139a size: 734

//可拉


[root@localhost ~]# docker pull xwsxkx/busybox:042601
042601: Pulling from xwsxkx/busybox
Digest: sha256:d07d5284b64c638e51740d00ba86182b343f7fd6c2a44f39e14ee198a0ca139a
Status: Image is up to date for xwsxkx/busybox:042601
docker.io/xwsxkx/busybox:042601

使用新生成的镜像创建容器

[root@localhost ~]# docker run -it --name b2 busybox:v0.1
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # cat data/index.html 
hello world
/ # exit

由此可见,新生成的镜像中是包含了新增的内容的,但是此时有一个问题,那就是容器默认要启动的进程是什么?在这里,默认情况下是启动的sh进程,但我们是要启动一个http站点,所以我们要在创建镜像时将容器默认启动的进程设为httpd,这样一来我们就可以通过新生成的镜像来快速构建一个简单的http站点了。

使用docker inspect命令查看b1容器启动的默认进程是什么

[root@localhost ~]# docker inspect b1
[
                    ...此处省略N行
            "Cmd": [
                "sh"
            ],
                    ...此处省略N行
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    ...此处省略N行
]

重新生成镜像并上传

[root@localhost ~]# docker commit -a 'sean <2286751511@qq.com>' -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p b1 xwsxkx/httpd:042601
sha256:4cc536f13f961890b09071abbb146ba4c0e7e95e512249c2c064f6945565b319
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
xwsxkx/httpd     042601    4cc536f13f96   10 seconds ago   1.24MB
xwsxkx/b1        v0.1      121711e0cdc9   2 hours ago      1.24MB
xwsxkx/busybox   042601    121711e0cdc9   2 hours ago      1.24MB
busybox          v0.1      121711e0cdc9   2 hours ago      1.24MB
busybox          latest    beae173ccac6   3 months ago     1.24MB
httpd            latest    dabbfbe0c57b   4 months ago     144MB

使用新生成的镜像创建容器

[root@localhost ~]# docker run -d --name web xwsxkx/httpd:042601
f1f21e2dbd249983102b416afa49c314995fc2d80ba6e67f6e3206809cc98f7b
co[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS     NAMES
f1f21e2dbd24   xwsxkx/httpd:042601   "/bin/httpd -f -h /d…"   6 seconds ago   Up 4 seconds             web
5649c8c17d18   busybox               "sh"                     2 hours ago     Up 2 hours               b1


[root@localhost ~]# docker inspect web

[root@localhost ~]# curl 172.17.0.3
hello world

镜像的导入与导出

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

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

docker中我们使用docker save进行导出,使用docker load进行导入。

在已生成镜像的主机上执行docker save导出镜像
[root@localhost ~]# docker save -o httpd.tar.xz xwsxkx/httpd:042601
[root@localhost ~]# ls
anaconda-ks.cfg   data   httpd.tar.xz


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



docker load -i httpd。tar。xz

基于容器制作镜像

docker run --name h1 -it centos
[root@6e676b8e9db7 /]# cd /etc/yum.repos.d/
[root@6e676b8e9db7 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.comrepo/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   9415      0 --:--:-- --:--:-- --:--:--  9415
[root@6e676b8e9db7 yum.repos.d]#  sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.comd' /etc/yum.repos.d/CentOS-Base.repo
[root@6e676b8e9db7 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8noarch.rpm
[root@6e676b8e9db7 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirors.aliyun.com|' /etc/[root@6e676b8e9db7 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
yum.repos.d/epel
[root@6e676b8e9db7 yum.repos.d]# ls
CentOS-Base.repo		     CentOS-Linux-Devel.repo		 CentOS-Linux-Plus.repo
CentOS-Linux-AppStream.repo	     CentOS-Linux-Extras.repo		 CentOS-Linux-PowerTools.repo
CentOS-Linux-BaseOS.repo	     CentOS-Linux-FastTrack.repo	 CentOS-Linux-Sources.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-HighAvailability.repo
CentOS-Linux-Debuginfo.repo	     CentOS-Linux-Media.repo




[root@6e676b8e9db7 ~]# dnf -y install make wget
Failed to set locale, defaulting to C.UTF-8
Repository extras is listed more than once in the configuration
CentOS Linux 8 - AppStream                                             58  B/s |  38  B     00:00    
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs n mirrorlist
//报错 


解决

[baseos]
name=CentOS Linux $releasever - BaseOS
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial


[appstream]
name=CentOS Linux $releasever - AppStream
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/AppStream/$basearch/os/ 
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial




[root@6e676b8e9db7 ~]wget
https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@6e676b8e9db7 ~] wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz

wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz 


[root@6e676b8e9db7 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@6e676b8e9db7 ~]# yum groups  install "Development Tools"

 useradd -r -M -s /sbin/nologin apache
[root@6e676b8e9db7 apr-1.7.0~]# vi configure
注释掉$RM "$cfgfile"
[root@6e676b8e9db7 ~]# ./configure --prefix=/usr/local/apr
[root@6e676b8e9db7 ~]# make -j 4 && make install
[root@6e676b8e9db7 apr-util-1.6.1~]# 
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@6e676b8e9db7 apr-util-1.6.1 ~]# make -j 4 && make install

[root@6e676b8e9db7 httpd-2.4.53 ~]# ./configure \
--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@6e676b8e9db7 httpd-2.4.53 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH'> /etc/profile.d/apache.sh

[root@6e676b8e9db7 httpd-2.4.53 ~]# source /etc/profile.d/apache.sh 
[root@6e676b8e9db7 ~]# cd /usr/lib/systemd/system    

[root@6e676b8e9db7 system]# vi httpd.service
[root@6e676b8e9db7 system]# cat httpd.service
[Unit]
Description=httpd server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start 
Execstop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target


[root@6e676b8e9db7 system]#  systemctl daemon-reload
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
 Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

docker commit -p 6e676b8e9db7 
sha256:81869f360a9bc7b6a25e2f360b5660783d59a9c32693784ac1177f528b874376

docker tag 81869f360a9b xm17671855780:v0.2

docker images
REPOSITORY       TAG       IMAGE ID       CREATED        SIZE
xwsxkx/httpd     042601    4cc536f13f96   6 hours ago    1.24MB
xwsxkx/busybox   042601    121711e0cdc9   7 hours ago    1.24MB
busybox          v0.1      121711e0cdc9   7 hours ago    1.24MB
xwsxkx/b1        v0.1      121711e0cdc9   7 hours ago    1.24MB
busybox          latest    beae173ccac6   3 months ago   1.24MB
httpd            latest    dabbfbe0c57b   4 months ago   144MB
centos           latest    5d0da3dc9764   7 months ago   231MB

[root@10~]# docker exec -it  6e676b8e9db7 /bin/bash
[root@8c2ea0081e3b /]# ss -anlt
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:80                              0.0.0.0:*                                  

举报

相关推荐

0 条评论