Docker ( 3 ) ~ カスタムイメージの作成 ~

October 31, 2017

コンテナで Cent OS を稼働させてカスタムイメージを作成する

Cent OS のイメージを取得してコンテナを作成して実行

$ sudo docker search centos
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                             The official build of CentOS.                   3759      [OK]       
ansible/centos7-ansible            Ansible on Centos7                              103                  [OK]
jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8...   88                   [OK]
tutum/centos                       Simple CentOS docker image with SSH access      33                   
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              31                   [OK]
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glu...   21                   [OK]
kinogmt/centos-ssh                 CentOS with SSH                                 17                   [OK]
openshift/base-centos7             A Centos7 derived base image for Source-To...   11                   
centos/php-56-centos7              Platform for building and running PHP 5.6 ...   10                   
centos/python-35-centos7           Platform for building and running Python 3...   9                    
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use...   6                    
openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 ima...   6                    
darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
openshift/ruby-20-centos7          DEPRECATED: A Centos7 based Ruby v2.0 imag...   3                    
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
openshift/php-55-centos7           DEPRECATED: A Centos7 based PHP v5.5 image...   1                    
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag nam...   1                    
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile...   1                    
miko2u/centos6                     CentOS6 日本語環境                                   1                    [OK]
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developm...   1                    [OK]
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated ...   0                    
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for us...   0                    
pivotaldata/centos                 Base centos, freshened up a little with a ...   0                    
smartentry/centos                  centos with smartentry                          0                    [OK]
jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                    [OK]

$ curl -s https://registry.hub.docker.com/v2/repositories/library/centos/tags/ | grep -Po '"name":.*?[^//]",'
"name": "7",
"name": "centos7",
"name": "latest",
"name": "7.4.1708",
"name": "centos7.4.1708",
"name": "6.8",
"name": "centos6.8",
"name": "6.9",
"name": "centos6.9",
"name": "7.2.1511",

今回は centos:centos7 を用いてコンテナを作成。docker run の後、自動的にプロンプトがコンテナイメージ上のものに遷移する。なお、シェルのような対話的なプログラムをコンテナ内で実行させるためには -it オプションをつける。

$ sudo docker run -it centos:centos7 /bin/bash
Unable to find image 'centos:centos7' locally
centos7: Pulling from library/centos
d9aaf4d82f24: Pull complete 
Digest: sha256:eba772bac22c86d7d6e72421b4700c3f894ab6e35475a34014ff8de74c10872e
Status: Downloaded newer image for centos:centos7
[root@bf6ff4d6487d /]# uname -a
Linux bf6ff4d6487d 4.9.51-10.52.amzn1.x86_64 #1 SMP Fri Sep 29 01:16:19 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@bf6ff4d6487d /]# ls /etc
BUILDTIME                bash_completion.d        dbus-1         gcrypt     hosts        ld.so.cache    machine-id         os-release      printcap   rc1.d           rpm        shells              systemd        yum.conf
DIR_COLORS               bashrc                   default        gnupg      hosts.allow  ld.so.conf     modprobe.d         pam.d           profile    rc2.d           rsyslog.d  skel                terminfo       yum.repos.d
DIR_COLORS.256color      binfmt.d                 depmod.d       group      hosts.deny   ld.so.conf.d   modules-load.d     passwd          profile.d  rc3.d           sasl2      ssl                 tmpfiles.d
DIR_COLORS.lightbgcolor  centos-release           dracut.conf    group-     init.d       libaudit.conf  motd               passwd-         protocols  rc4.d           securetty  subgid              udev
GREP_COLORS              centos-release-upstream  dracut.conf.d  gshadow    inputrc      libuser.conf   mtab               pkcs11          python     rc5.d           security   subuid              vconsole.conf
X11                      chkconfig.d              environment    gshadow-   issue        locale.conf    nsswitch.conf      pki             qemu-ga    rc6.d           selinux    sysconfig           virc
adjtime.rpmsave          crypttab                 exports        gss        issue.net    localtime      nsswitch.conf.bak  pm              rc.d       redhat-release  services   sysctl.d            xdg
aliases                  csh.cshrc                filesystems    host.conf  krb5.conf    login.defs     openldap           popt.d          rc.local   resolv.conf     shadow     system-release      xinetd.d
alternatives             csh.login                fstab          hostname   krb5.conf.d  logrotate.d    opt                prelink.conf.d  rc0.d      rpc             shadow-    system-release-cpe  yum
[root@bf6ff4d6487d /]# yum update
[root@bf6ff4d6487d /]# yum upgrade
[root@bf6ff4d6487d /]# yum install emacs -y
[root@bf6ff4d6487d /]# exit
exit

これで emacs がインストールされ、exit で bash が終了し、コンテナも停止する。

コンテナからイメージを作成

docker commit を用いてコンテナからイメージを作成する。今回は先ほど emacs をインストールしたコンテナから新たなイメージを作成する。イメージ名は centos7-emacs-installed とする。

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                          PORTS                  NAMES
bf6ff4d6487d        centos:centos7      "/bin/bash"          8 minutes ago       Exited (0) About a minute ago                          competent_nightingale
9ae657fb11fd        httpd:2.4.29        "httpd-foreground"   15 minutes ago      Up 15 minutes                   0.0.0.0:8080->80/tcp   sharp_cray
50413563b304        httpd:2.4.29        "httpd-foreground"   26 minutes ago      Exited (0) 16 minutes ago                              dreamy_goldstine

$ sudo docker commit bf6ff4d6487d centos:centos7-emacs-installed
sha256:fb048d27b6966bb08ae09185e06c17ca46c82a14f878c02e47d965cc524f5cc5

$ sudo docker images
REPOSITORY          TAG                       IMAGE ID            CREATED             SIZE
centos              centos7-emacs-installed   fb048d27b696        11 seconds ago      557 MB
httpd               2.4.29                    a8bdc7fdaa4f        7 days ago          177 MB
centos              centos7                   196e0ce0c9fb        6 weeks ago         197 MB

イメージが作成されたことが確認できる。
実際に作成したイメージからコンテナを作成する。

$ sudo docker run -it centos:centos7-emacs-installed /bin/bash
[root@d2330054030c /]# emacs --version
GNU Emacs 24.3.1
Copyright (C) 2013 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

emacs がインストールされていることがわかる。


 © 2023, Dealing with Ambiguity