您的位置:首页 > 运维架构 > Docker

Docker学习入门之——构建自己的镜像

2016-11-05 10:54 676 查看

安装基本
docker
的基础环境

root@managert:~ # yum list|grep docker

cockpit-docker.x86_64                   0.114-2.el7.centos             extras
docker.x86_64                           1.10.3-46.el7.centos.14        extras
docker-common.x86_64                    1.10.3-46.el7.centos.14        extras
docker-devel.x86_64                     1.3.2-4.el7.centos             extras
docker-distribution.x86_64              2.4.1-2.el7                    extras
docker-forward-journald.x86_64          1.10.3-44.el7.centos           extras
docker-latest.x86_64                    1.12.1-2.el7.centos            extras
docker-latest-logrotate.x86_64          1.12.1-2.el7.centos            extras
docker-latest-v1.10-migrator.x86_64     1.12.1-2.el7.centos            extras
docker-logrotate.x86_64                 1.10.3-46.el7.centos.14        extras
docker-lvm-plugin.x86_64                1.10.3-46.el7.centos.14        extras
docker-novolume-plugin.x86_64           1.10.3-46.el7.centos.14        extras
docker-python.x86_64                    1.4.0-115.el7                  extras
docker-registry.noarch                  0.6.8-8.el7                    extras
docker-registry.x86_64                  0.9.1-7.el7                    extras
docker-selinux.x86_64                   1.10.3-46.el7.centos.14        extras
docker-unit-test.x86_64                 1.10.3-46.el7.centos.14        extras
docker-v1.10-migrator.x86_64            1.10.3-46.el7.centos.14        extras

root@managert:~ # yum install -y docker docker-engine #执行安装


安装教程可以参见官方站点

安装完成后可以通过
docker version
查看安装状态

root@managert:~ # docker version
Client:
Version:      1.12.3
API version:  1.24
Go version:   go1.6.3
Git commit:   6b644ec
Built:
OS/Arch:      linux/amd64

Server:
Version:      1.12.3
API version:  1.24
Go version:   go1.6.3
Git commit:   6b644ec
Built:
OS/Arch:      linux/amd64

创建基本镜像(基于
centos

以下脚本是在
centos
上创建基于
centos
的镜像的脚本,
copy
后执行

#!/usr/bin/env bash
#
# Create a base CentOS Docker image.
#
# This script is useful on systems with yum installed (e.g., building
# a CentOS image on CentOS).  See contrib/mkimage-rinse.sh for a way
# to build CentOS images on other systems.

set -e

usage() {
cat <<EOOPTS
$(basename $0) [OPTIONS] <name>
OPTIONS:
-p "<packages>"  The list of packages to install in the container.
The default is blank.
-g "<groups>"    The groups of packages to install in the container.
The default is "Core".
-y <yumconf>     The path to the yum config to install packages from. The
default is /etc/yum.conf for Centos/RHEL and /etc/dnf/dnf.conf for Fedora
EOOPTS
exit 1
}

# option defaults
yum_config=/etc/yum.conf
if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
yum_config=/etc/dnf/dnf.conf
alias yum=dnf
fi
install_groups="Core"
while getopts ":y:p:g:h" opt; do
case $opt in
y)
yum_config=$OPTARG
;;
h)
usage
;;
p)
install_packages="$OPTARG"
;;
g)
install_groups="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
usage
;;
esac
done
shift $((OPTIND - 1))
name=$1
if [[ -z $name ]]; then
usage
fi
target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
set -x
mkdir -m 755 "$target"/dev
mknod -m 600 "$target"/dev/console c 5 1
mknod -m 600 "$target"/dev/initctl p
mknod -m 666 "$target"/dev/full c 1 7
mknod -m 666 "$target"/dev/null c 1 3
mknod -m 666 "$target"/dev/ptmx c 5 2
mknod -m 666 "$target"/dev/random c 1 8
mknod -m 666 "$target"/dev/tty c 5 0
mknod -m 666 "$target"/dev/tty0 c 4 0
mknod -m 666 "$target"/dev/urandom c 1 9
mknod -m 666 "$target"/dev/zero c 1 5

# amazon linux yum will fail without vars set
if [ -d /etc/yum/vars ]; then
mkdir -p -m 755 "$target"/etc/yum
cp -a /etc/yum/vars "$target"/etc/yum/
fi

if [[ -n "$install_groups" ]];
then
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
--setopt=group_package_types=mandatory -y groupinstall $install_groups
fi

if [[ -n "$install_packages" ]];
then
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
--setopt=group_package_types=mandatory -y install $install_packages
fi

yum -c "$yum_config" --installroot="$target" -y clean all

cat > "$target"/etc/sysconfig/network <<EOF
NETWORKING=yes
HOSTNAME=localhost.localdomain
EOF

# effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
#  locales
rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
#  docs and man pages
rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
#  cracklib
rm -rf "$target"/usr/share/cracklib
#  i18n
rm -rf "$target"/usr/share/i18n
#  yum cache
rm -rf "$target"/var/cache/yum
mkdir -p --mode=0755 "$target"/var/cache/yum
#  sln
rm -rf "$target"/sbin/sln
#  ldconfig
rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
mkdir -p --mode=0755 "$target"/var/cache/ldconfig
version=
for file in "$target"/etc/{redhat,system}-release
do
if [ -r "$file" ]; then
version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$file")"
break
fi
done
if [ -z "$version" ]; then
echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
version=$name
fi
tar --numeric-owner -c -C "$target" . | docker import - $name:$version
docker run -i -t --rm $name:$version /bin/bash -c 'echo success'
rm -rf "$target"

执行后看到success的字样表示基本镜像已经创建完成
也可以通过
docker images
来查看镜像

发布基本镜像到私有的库中

本案例使用的私有库是
Nexus Repository Manager(OSS 3.0.2.02)
,同时支持
maven
npm
nuget
docker
gem
等,挺好的一个工具.因公司已经存在了就没有使用
docker
官方推荐的私有库的方案





登录到私有库

root@managert:~ # docker login https://mvn.source.org:9990

Username: 12860
Password:

用户名和密码是登录OSS的账户
如果登录过程中出现错误信息

Error response from daemon: Get https://mvn.source.org:9990/v1/users/: http: server gave HTTP response to HTTPS client

表示当前登录的私有库不受信任,需要修改配置文件。配置内容如下:

**如果
login
操作时候提示以上错误,则需要在/etc/docker/daemon.json(不存在则新增)增加一行内容:

{ "insecure-registries":["mvn.source.org:9990"] }

为刚刚建好的镜像添加tag

root@managert:~ # docker tag 39a3b68429a7 mvn.source.org:9990/centos:lastest

上传镜像

root@managert:~ # docker push  mvn.source.org:9990/centos:lastest

The push refers to a repository [mvn.source.org:9990/centos]
14e19e279e3e: Pushed
a7d98c277f49: Pushed
tornado: digest: sha256:eee628902e1495a03d82988f8e0801c9b4dd91aca23e101e38e36c6eadd199bc size: 742

下载镜像

root@managert:~ # docker pull  mvn.source.org:9990/centos:lastest


至此,完成了一个基础镜像的创建、私有库上传及下载的过程,后续过程将探索如果基于基础镜像创建其他特定应用领域的镜像,完成自动化部署。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Docker Machine centos