Docker镜像构建手册
简介
基础镜像构建
此处将虚拟机安装的操作系统构建为基础镜像
# tar -cvpf /cloud/centos-7-3.tar --directory=/ --exclude=proc --exclude=sys --excluede=dev --exclude=run --exclude=cloud / > /cloud/centos.log
#cat centos-7-3.tar |docker import - centos:7.3
- 基于RHEL-7.3构建基础镜像
# febootstrap -i bash -i yum -i iproute -i vi -i openssh-server -i openssh-clients -i python -i passwd -i which rhel-7.3 ./rhel-7.3 file:///mnt/cdrom/
file:///mnt/cdrom/为yum源
./rhel-7.3 是生成的文件
# tar -C /home/rhel-7.3 -c . | docker import - rhel-base:7.3
/home/rhel-7.3 用febootstrap生成的文件(是个目录)
Dockerfile
命令
- FROM
可以有多个
FROM RHEL:6.3
- ENTRYPOINT
是容器运行的入口,且要保证命令不退出,可以单独使用,也可以和CMD一起使用
有两种模式:shell模式,exec模式
ENTRYPOINT ["execable", "param1", "param2"] // exec 模式
ENTRYPOINT execable param1 param2 // shell 模式
使用exec模式,CMD命令会作为ENTRYPOINT的参数,追加到参数列表后
如果 ENTRYPOINT 使用了 shell 模式,CMD 指令会被忽略
如果 ENTRYPOINT 使用了 exec 模式,CMD 也应该使用 exec 模式
exec模式样例:
COPY ./docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["postgres"]
-------------
docker-entrypoint.sh的内容
-------------
#!/bin/bash
set -e
if [ "$1" = 'postgres' ]; then
chown -R postgres "$PGDATA"
if [ -z "$(ls -A "$PGDATA")" ]; then
gosu postgres initdb
fi
exec gosu postgres "$@"
fi
exec "$@"
- CMD
是容器运行的入口,且要保证命令不退出,可以单独使用,也可以和ENTRYPOINT一起使用
备注:
Dockerfile中必须至少有ENTRYPOINT或CMD中的一个
- COPY
COPY --from=sampleapp:latest home/user/app/config.json app/config.json
sampleapp:latest:镜像名称
home/user/app/config.json:镜像中的文件
app/config.json:复制到新镜像中的文件名
Jboss 镜像构建
Redis 镜像构建
构建命令
# docker build -t redis:3.2.11 .
目录
# ls -al
Dockerfile
cloud.repo
docker-entrypoint.sh
redis-3.2.11
Dockerfile
FROM CentOS:7.6
COPY redis-3.2.11 /opt/redis-3.2.11
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN useradd redisuser -d /home/redisuser -s /bin/bash
RUN rm -fr /etc/yum/yum.repos.d/*
COPY cloud.repo /etc/yum/yum.repos.d/cloud.repo
RUN yum clean all
RUN yum makecache
RUN yum install "crontab*" -y
RUN echo 'redisuser:redisuser' |chpasswd
RUN chown -R redisuser:redisuser /opt/redis-3.2.11
ENV REDIS_HOME /opt/redis-3.2.11
ENV PATH ${REDIS_HOME}:$PATH
RUN export PATH
EXPOSE 6379
WORKDIR /opt/redis-3.2.11/src
ENTRYPOINT ["docker-entrypoint.sh", "redis-server"]
cloud.repo
[centos-7.3]
name=CentOS-$releasever-Base
baseurl=http://10.0.156.250/redhat7.3/x86_64
enable=1
gpgcheck=0
docker-entrypoint.sh
#!/bin/sh
set -e
if [ "${1#-}" != "$1" ] || [ "${1%.conf}" != "$1" ];then
set -- redis-server "$@"
fi
exec "$@"
备注: set -- "$X" 就是把X的值返回给$1, set –- $X 就是把X作为一个表达式的值一一返回
MySql 镜像构建
sonatype nexus
镜像层解析
ADD file:6f877549795f4798a38b318c0f63f6646dbf10d3c249c7f4b73cc7cfe42dc0f5 in /
LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20181205
CMD ["/bin/bash"]
MAINTAINER Sonatype <cloud-ops@sonatype.com>
LABEL vendor=Sonatype com.sonatype.license=Apache License, Version 2.0 com.sonatype.name=Nexus Repository Manager base image
ARG NEXUS_VERSION=3.15.2-01
ARG NEXUS_DOWNLOAD_URL=https://download.sonatype.com/nexus/3/nexus-3.15.2-01-unix.tar.gz
ARG NEXUS_DOWNLOAD_SHA256_HASH=acde357f5bbc6100eb0d5a4c60a1673d5f1f785e71a36cfa308b8dfa45cf25d0
ENV SONATYPE_DIR=/opt/sonatype
ENV NEXUS_HOME=/opt/sonatype/nexus NEXUS_DATA=/nexus-data NEXUS_CONTEXT= SONATYPE_WORK=/opt/sonatype/sonatype-work DOCKER_TYPE=docker
ARG NEXUS_REPOSITORY_MANAGER_COOKBOOK_VERSION=release-0.5.20190212-155606.d1afdfe
ARG NEXUS_REPOSITORY_MANAGER_COOKBOOK_URL=https://github.com/sonatype/chef-nexus-repository-manager/releases/download/release-0.5.20190212-155606.d1afdfe/ch ef-nexus-repository-manager.tar.gz
ADD file:96a8726e4f3ee18b65140cf1dfb4845d98d454ceb2c45fbd846c57e32a5a2ed2 in /var/chef/solo.json.erb
|5 NEXUS_DOWNLOAD_SHA256_HASH=acde357f5bbc6100eb0d5a4c60a1673d5f1f785e71a36cfa308b8dfa45cf25d0 NEXUS_DOWNLOAD_URL=https://download.sonatype.com/nexus/3/nexus-3.15.2-01-unix.tar.gz NEXUS_REPOSITORY_MANAGER_COOKBOOK_URL=https://github.com/sonatype/chef-nexus-repository-manager/releases/download/release-0.5.20190212-155606.d1afdfe/ch ef-nexus-repository-manager.tar.gz NEXUS_REPOSITORY_MANAGER_COOKBOOK_VERSION=release-0.5.20190212-155606.d1afdfe NEXUS_VERSION=3.15.2-01 RUN curl -L https://www.getchef.com/chef/install.sh | bash && /opt/chef/embedded/bin/erb /var/chef/solo.json.erb > /var/chef/solo.json && chef-solo --recipe-url ${NEXUS_REPOSITORY_MANAGER_COOKBOOK_URL} --json-attributes /var/chef/solo.json && rpm -qa *chef* | xargs rpm -e && rpm --rebuilddb && rm -rf /etc/chef && rm -rf /opt/chefdk && rm -rf /var/cache/yum && rm -rf /var/chef
VOLUME [/nexus-data]
EXPOSE 8081
USER nexus
ENV INSTALL4J_ADD_VM_PARAMS=-Xms1200m -Xmx1200m -XX:MaxDirectMemorySize=2g -Djava.util.prefs.userRoot=/nexus-data/javaprefs
CMD ["sh" "-c" "${SONATYPE_DIR}/start-nexus-repository-manager.sh"]