基于容器制作镜像

使用docker commit基于容器制作镜像

1、格式:

[root@localhost ~]# docker commit --help

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

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image (default [])
      --help             Print usage
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
-a ,作者(例如, “along along@along.com
-c ,修改 Dockerfifile 指令应用于创建的镜像
-m ,提交消息
-p ,在提交期间暂停容器(默认为 true
示例一:简单的基于容器创建一个新的镜像
(1)先运行一个容器
[root@localhost ~]# docker run --name b1 -it busybox
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir -p /data/html
/ # echo "<h1>busybox httpd server</h1>" > /data/html/index.html
/ # cat /data/html/index.html
<h1>busybox httpd server</h1>

(2)不用退出这个容器,另起终端在b1容器基础上,制作新镜像

[root@localhost ~]# docker commit -p b1
sha256:34b70e5080a9d07ec9a87bf8316c1c5a492f9b8e344d1270cc92932352a11386
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              34b70e5080a9        11 seconds ago      1.24 MB

(3)给新绘制的镜像打标签

[root@localhost ~]# docker tag 34b70e5080a9 zjm/httpd:v0.1
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
zjm/httpd           v0.1                34b70e5080a9        About a minute ago   1.24 MB

(4)可以对同一个镜像载再打标签

[root@localhost ~]# docker tag zjm/httpd:v0.1 ajm/httpd:v0.2
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ajm/httpd           v0.2                34b70e5080a9        3 minutes ago       1.24 MB
zjm/httpd           v0.1                34b70e5080a9        3 minutes ago       1.24 MB

(5)删除同一个镜像的标签,只是把这个标签去掉,只到删除这个镜像的最后一个标签,此镜像才会被删除

[root@localhost ~]# docker rmi ajm/httpd:v0.2
Untagged: ajm/httpd:v0.2
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
zjm/httpd           v0.1                34b70e5080a9        5 minutes ago       1.24 MB

(6)基于新的镜像运行一个容器,验证是否是基于b1创建的

[root@localhost ~]# docker run --name b2 -it zjm/httpd:v0.1
/ # cat /data/html/index.html
<h1>busybox httpd server</h1>

示例二:基于容器创建新的镜像,并修改执行命令CMD

(1)基于容器b1创建新的镜像,并修改命令为执行httpd服务

[root@localhost ~]#  docker commit -a "Along <along@along.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 along/httpd:v0.2
sha256:565ee1d9d7f3424d7f5ca803a14dbc727323c409962e39ebd0682afd1f51faa1
-f :不运行为守护进程,在前台运行       -h :指定 httpd 运行的主目录
(2)运行新的镜像v0.2
[root@localhost ~]# docker run --name b3 -d along/httpd:v0.2
f75797df94b1111b35d67b0316b4e35e86bbe7ae04f9f23c162a78ba4b5bf45f

(3)验证成功

[root@localhost ~]#  docker inspect b3 |grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 安装Docker 在CentOS上安装Docker,可以使用官方的安装脚本。打开终端并运行以下命令: ``` $ curl -fsSL https://get.docker.com/ | sh ``` 如果您的系统上没有curl,请先安装它: ``` $ yum install curl ``` 2. 下载PostgreSQL的Dockerfile 我们可以从官方的Docker Hub上下载PostgreSQL的Dockerfile。使用以下命令: ``` $ curl -O https://raw.githubusercontent.com/docker-library/postgres/master/13/alpine/Dockerfile ``` 3. 编辑Dockerfile 使用vim或nano等编辑器打开下载下来的Dockerfile文件,进行以下配置: ``` FROM centos:latest ENV POSTGRES_USER postgres ENV POSTGRES_PASSWORD postgres ENV POSTGRES_DB postgres RUN yum update -y && \ yum install -y postgresql-server postgresql-contrib && \ yum clean all USER postgres RUN initdb --encoding=UTF8 --locale=C -D /var/lib/pgsql/data && \ pg_ctl -D /var/lib/pgsql/data -l logfile start && \ psql --command "CREATE USER postgres WITH SUPERUSER PASSWORD 'postgres';" && \ createdb -O postgres postgres VOLUME ["/var/lib/pgsql/data"] EXPOSE 5432 CMD ["postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"] ``` 4. 构建Docker镜像 使用以下命令构建Docker镜像: ``` $ docker build -t my_postgresql . ``` 这将构建一个名为“my_postgresql”的新Docker镜像。 5. 运行PostgreSQL容器 使用以下命令运行PostgreSQL容器: ``` $ docker run -d -p 5432:5432 --name my_postgresql_container my_postgresql ``` 这将创建一个名为“my_postgresql_container”的新容器,并将容器的端口5432映射到主机的端口5432。 6. 测试PostgreSQL容器 为了测试新的PostgreSQL容器,请使用以下命令: ``` $ psql -h localhost -U postgres -d postgres ``` 您应该现在可以通过psql连接到PostgreSQL容器。 现在您已经成功地使用Docker创建了一个基于CentOS的PostgreSQL镜像,并运行了一个新的PostgreSQL容器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值