create docker image 1 - use other images

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介:
本文主要讲一下创建image的方法之一, 从已有的image创建image.

测试环境 : 
CentOS 6.5 x64
安装参考
第一次启动docker服务前, 最好在独立分区或存储中创建一个目录存放docker的images和containers等信息.
# mkdir /data01/docker
# ln -s /data01/docker /var/lib/

启动docker服务
# service docker start

服务启动后可以看到启动了相应的虚拟网桥, 并创建了相应的nat表信息.
[root@150 ~]# ifconfig
docker0   Link encap:Ethernet  HWaddr 66:B7:CF:A7:21:F4  
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::64b7:cfff:fea7:21f4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:468 (468.0 b)

[root@150 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
docker0         8000.000000000000       no

[root@150 ~]# iptables-save
# Generated by iptables-save v1.4.7 on Thu Oct 23 11:36:28 2014
*nat
:PREROUTING ACCEPT [4253:466930]
:POSTROUTING ACCEPT [286:15293]
:OUTPUT ACCEPT [286:15293]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER 
-A POSTROUTING -s 172.17.0.0/16 ! -d 172.17.0.0/16 -j MASQUERADE 
-A POSTROUTING -s 172.17.0.0/16 ! -d 172.17.0.0/16 -j MASQUERADE 
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER 
COMMIT
# Completed on Thu Oct 23 11:36:28 2014
# Generated by iptables-save v1.4.7 on Thu Oct 23 11:36:28 2014


接下来从docker hub下载一个image, 作为我们创建image的基础image.
[root@150 ~]# docker search centos
NAME                                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                                          The official build of CentOS.                   540       [OK]   
[root@150 ~]# docker pull centos
[root@150 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos5             504a65221a38        3 weeks ago         467.1 MB
centos              centos6             68edf809afe7        3 weeks ago         212.7 MB
centos              centos7             87e5b6b3ccc1        3 weeks ago         224 MB
centos              latest              87e5b6b3ccc1        3 weeks ago         224 MB


我这里使用centos:centos6来启动container, 并安装postgresql.
安装完postgresql后, 保存为(或创建)一个新的image.
启动一个container, 并连接到终端, 因为要出外网下载一些软件, 所以--net="host"启动.

[root@150 ~]# docker run -t -i --net="host" centos:centos6 /bin/bash
bash-4.1# ping 202.101.172.35
PING 202.101.172.35 (202.101.172.35) 56(84) bytes of data.
64 bytes from 202.101.172.35: icmp_seq=1 ttl=58 time=2.32 ms
64 bytes from 202.101.172.35: icmp_seq=2 ttl=58 time=2.31 ms

在container中下载postgresql软件
bash-4.1# yum -y install glib2 lrzsz sysstat e4fsprogs xfsprogs ntp readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel python-devel tcl-devel gcc make smartmontools flex bison perl perl-devel perl-ExtUtils* OpenIPMI-tools openldap openldap-devel wget tar bzip2
bash-4.1# df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                9.9G  586M  8.8G   7% /
/dev/mapper/docker-8:33-3407874-9241bc14e46b6e4a674e51e71bc524a69e01eba8e3dc6303c56c8b9d91197b89
                      9.9G  586M  8.8G   7% /
tmpfs                  48G     0   48G   0% /dev
shm                    64M     0   64M   0% /dev/shm
/dev/sdc1             221G  154G   56G  74% /.dockerinit
/dev/sda1              39G  4.3G   34G  12% /etc/resolv.conf
/dev/sdc1             221G  154G   56G  74% /etc/hostname
/dev/sdc1             221G  154G   56G  74% /etc/hosts
tmpfs                  48G     0   48G   0% /proc/kcore
bash-4.1# cd /opt/  
bash-4.1# mkdir soft_bak
bash-4.1# cd soft_bak
bash-4.1# wget http://ftp.postgresql.org/pub/source/v9.3.5/postgresql-9.3.5.tar.bz2
bash-4.1# tar -jxvf postgresql-9.3.5.tar.bz2
bash-4.1# cd postgresql-9.3.5
bash-4.1# ./configure --prefix=/opt/pgsql9.3.5 --with-pgport=5432 --with-perl --with-tcl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
bash-4.1# gmake world
bash-4.1# gmake install-world
bash-4.1# ln -s /opt/pgsql9.3.5 /opt/pgsql
bash-4.1# useradd postgres
bash-4.1# su - postgres
[postgres@150 ~]$ vi .bash_profile 
export PS1="$USER@`/bin/hostname -s`-> "
export PGPORT=5432
export PGDATA=/home/postgres/pgdata/pg_root
export LANG=en_US.utf8
export PGHOME=/opt/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
export PGHOST=$PGDATA
alias rm='rm -i'
alias ll='ls -lh'
export PGDATABASE=postgres
[postgres@150 ~]$ exit
logout
bash-4.1# su - postgres
postgres@150-> 
初始化
postgres@150-> initdb -D $PGDATA -E UTF8 --locale=C -U postgres -W
postgres@150-> cd $PGDATA
postgres@150-> vi pg_hba.conf 
host all all 0.0.0.0/0 md5
postgres@150-> vi postgresql.conf 
listen_addresses = '0.0.0.0'            # what IP address(es) to listen on;
max_connections = 100                   # (change requires restart)
unix_socket_directories = '.'   # comma-separated list of directories
tcp_keepalives_idle = 60                # TCP_KEEPIDLE, in seconds;
tcp_keepalives_interval = 10            # TCP_KEEPINTVL, in seconds;
tcp_keepalives_count = 10               # TCP_KEEPCNT;
shared_buffers = 128MB                  # min 128kB
vacuum_cost_delay = 10ms                        # 0-100 milliseconds
vacuum_cost_limit = 10000               # 1-10000 credits
bgwriter_delay = 10ms                   # 10-10000ms between rounds
synchronous_commit = off                # synchronization level;
checkpoint_segments = 32                # in logfile segments, min 1, 16MB each
log_destination = 'csvlog'              # Valid values are combinations of
logging_collector = on          # Enable capturing of stderr and csvlog
log_truncate_on_rotation = on           # If on, an existing log file with the
log_checkpoints = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose           # terse, default, or verbose messages
log_timezone = 'GB'
log_autovacuum_min_duration = 0 # -1 disables, 0 logs all actions and
datestyle = 'iso, mdy'
timezone = 'GB'
lc_messages = 'C'                       # locale for system error message
lc_monetary = 'C'                       # locale for monetary formatting
lc_numeric = 'C'                        # locale for number formatting
lc_time = 'C'                           # locale for time formatting
default_text_search_config = 'pg_catalog.english'
bash-4.1# exit
exit


 
制作image

[root@150 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
9241bc14e46b        centos:centos6      /bin/bash           About an hour ago   Exited (0) 3 seconds ago                       clever_mclean  
[root@150 ~]# docker commit -m="Add postgresql by digoal based on centos:centos6." -a="Digoal Zhou" 9241bc14e46b digoal/pg:9.3.5
b8a933b7f4d749d547be2b91cf4aaaf4a9d20b89d71c7a8aa73351e22c789afe
[root@150 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
digoal/pg           9.3.5               b8a933b7f4d7        24 seconds ago      646.5 MB
centos              centos5             504a65221a38        3 weeks ago         467.1 MB
centos              centos6             68edf809afe7        3 weeks ago         212.7 MB
centos              centos7             87e5b6b3ccc1        3 weeks ago         224 MB
centos              latest              87e5b6b3ccc1        3 weeks ago         224 MB


使用刚才制作的image, 启动container
[root@150 ~]# docker run -t -i -P --net="host" digoal/pg:9.3.5 /bin/bash
bash-4.1# su - postgres
postgres@150-> pg_ctl start
server starting
postgres@150-> LOG:  00000: redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".
LOCATION:  SysLogger_Start, syslogger.c:638


在另一个会话可以看到 : 
[root@150 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
56b7e3e8ba0e        digoal/pg:9.3.5     /bin/bash           26 seconds ago      Up 25 seconds                           hopeful_blackwell  


端口没有映射出来, 因为centos:centos6这个image没有启用网络.
所以如果需要网络的话需要使用Dockerfile来制作image.

[参考]
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
7月前
|
缓存 测试技术 持续交付
docker desktop 搜索不到images
【2月更文挑战第23天】
1529 0
|
Linux Docker 容器
「Docker」M1 Pro 打包docker image问题合集
「Docker」M1 Pro 打包docker image问题合集
162 0
|
Linux Docker Windows
docker pull 报错解决:error pulling image configuration: Get https:..
docker pull 报错解决:error pulling image configuration: Get https:..
3988 0
|
2月前
|
网络虚拟化 Docker 容器
docker Desktop报错 error pulling image configuration 处理
docker Desktop报错 error pulling image configuration 处理
42 0
|
4月前
|
NoSQL Redis Docker
Docker 删除 images
Docker 删除 images
27 0
|
5月前
|
JSON Ubuntu 持续交付
用skopeo检查docker image
通过Skopeo,您可以方便地获取Docker镜像的详细信息和元数据,而不需要拉取或安装整个镜像。这对于那些有成百上千镜像的大型系统是非常实用的,因为它有助于快速查看和分析镜像的特定属性。Skopeo是一个专业的工具,用于在持续集成和部署的过程中实现高效的镜像管理,既减轻了对Docker守护进程的依赖,也为容器化环境中镜像的控制提供了灵活性。
65 3
|
4月前
|
NoSQL Redis Docker
Docker 删除 images
Docker 删除 images
119 0
|
5月前
|
Linux Docker 容器
Docker02--搭建Linux环境,配置Docker,docker images无法访问,因为docker没有启动,阿里云镜像加速器免费的
Docker02--搭建Linux环境,配置Docker,docker images无法访问,因为docker没有启动,阿里云镜像加速器免费的
|
7月前
|
Docker 容器
mac构建Docker镜像报错failed to solve with frontend dockerfile.v0: failed to create LLB definition: unexpec
mac构建Docker镜像报错failed to solve with frontend dockerfile.v0: failed to create LLB definition: unexpec
284 0
|
7月前
|
应用服务中间件 nginx Docker
Docker中报错 Error response from daemon: Conflict. The container name “/nginx01“ is already in use
在尝试运行 `docker run -d --name nginx01 -p 3344:80 nginx` 时遇到错误。问题源于已有名为 nginx01 的容器未正确终止,造成命名冲突。解决方法:首先使用 `docker ps -a` 查看所有容器,然后删除现有 nginx01 容器,执行 `docker rm <container_id>`(替换 `<container_id>` 为实际容器ID),最后再运行 `docker run` 命令即可。
323 0