本文主要讲一下创建image的方法之一, 从已有的image创建image.
测试环境 :
CentOS 6.5 x64
安装参考
第一次启动docker服务前, 最好在独立分区或存储中创建一个目录存放docker的images和containers等信息.
启动docker服务
服务启动后可以看到启动了相应的虚拟网桥, 并创建了相应的nat表信息.
接下来从docker hub下载一个image, 作为我们创建image的基础image.
我这里使用centos:centos6来启动container, 并安装postgresql.
在container中下载postgresql软件
使用刚才制作的image, 启动container
在另一个会话可以看到 :
端口没有映射出来, 因为centos:centos6这个image没有启用网络.
[参考]
# 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.