创建镜像 - 创建与定制 mysql 镜像本文通过制作与定制 MySQL 镜像为案例,一方面说明创建镜像简单,另一方面解释 Docker image 的内容,直观描述 docker build 命令与 Dockerfile 主要配置。这些基本能满足日常工作需要,需要完整的命令和配置说明可参考官方文档或网上的翻译。 1、docker images?docker images 使得软件实现拿来即用。docker images 中包含什么呢? 先得看官方关于 docker 的描述1:
其中:
因此:
这个定义太枯燥,我们来看它的生成过程: 2、mysql 镜像制作2.1 Dockerfile 文件docker build 默认使用 Dockerfile 文件创建镜像。mysql 官方 docker 构建(mysql5.7)的 Dockerfile 文件内容是2: FROM debian:jessie # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r mysql && useradd -r -g mysql mysql # add gosu for easy step-down from root ENV GOSU_VERSION 1.7 RUN set -x \ && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ && export GNUPGHOME="$(mktemp -d)" \ && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ && chmod +x /usr/local/bin/gosu \ && gosu nobody true \ && apt-get purge -y --auto-remove ca-certificates wget RUN mkdir /docker-entrypoint-initdb.d # FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db: # File::Basename # File::Copy # Sys::Hostname # Data::Dumper RUN apt-get update && apt-get install -y perl pwgen --no-install-recommends && rm -rf /var/lib/apt/lists/* # gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5 ENV MYSQL_MAJOR 5.7 ENV MYSQL_VERSION 5.7.17-1debian8 RUN echo "deb http://repo.mysql.com/apt/debian/ jessie mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list # the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql) # also, we set debconf keys to make APT a little quieter RUN { \ echo mysql-community-server mysql-community-server/data-dir select ''; \ echo mysql-community-server mysql-community-server/root-pass password ''; \ echo mysql-community-server mysql-community-server/re-root-pass password ''; \ echo mysql-community-server mysql-community-server/remove-test-db select false; \ } | debconf-set-selections \ && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}" && rm -rf /var/lib/apt/lists/* \ && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \ && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \ # ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime && chmod 777 /var/run/mysqld # comment out a few problematic configuration values # don't reverse lookup hostnames, they are usually another container RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/mysql.conf.d/mysqld.cnf \ && echo '[mysqld]\nskip-host-cache\nskip-name-resolve' > /etc/mysql/conf.d/docker.cnf VOLUME /var/lib/mysql COPY docker-entrypoint.sh /usr/local/bin/ RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 3306 CMD ["mysqld"] 其中3:
官方 ENV 案例与解释: FROM busybox ENV foo /bar WORKDIR ${foo} # WORKDIR /bar ADD . $foo # ADD . /bar COPY \$foo /quux # COPY $foo /quux
VOLUME /var/lib/mysql 在 mysql 容器运行加载时,会将 /var/lib/mysql 目录下所有文件拷贝到新的容器的卷。这将加速当前程序读写速度(仅在当前文件系统操作)。
官方的一些案例 COPY hom* /mydir/ # adds all files starting with "hom" COPY hom?.txt /mydir/ # ? is replaced with any single character, e.g., "home.txt" COPY test relativeDir/ # adds "test" to `WORKDIR`/relativeDir/ COPY test /absoluteDir/ # adds "test" to /absoluteDir/
2.2 制作镜像利用上述文件,使用命令 docker build <path> -t <image-name>:<tag> 就可以构建镜像,例如: 当前目录下包含 Dockerfile 和 docker-entrypoint.sh ,执行 sudo docker build . -t mysql:1.0-yours 从 Dockerfile 配置看出, Dockerfile 包含两大类指令,
当然,你可能和我一样不熟悉 debian:jessie 环境, 熟悉 ubuntu 可以看创建 Mysql 容器4。 3、定制镜像制定前准备是否需要定制镜像,从那个起点定制镜像并不是一件容易的决策。 是否需要定制镜像?
当然,发布程序、定制官方镜像,以方便使用是定制镜像的理由。 从那个基础镜像开始?
没有特殊情况,不必从操作系统构建 定制 mysql一句话,pull 官方镜像,编写 my.cnf ,然后写 Dockerfile,例如: FROM mysql:5.7 COPY my.cnf /etc/mysql/conf.d ENV MYSQL_ROOT_PASSWORD root 然后: docker build . --rm=true -t yours/mysql:5.7 检查能力 你能否参考Docker mysql 主从配置详解及实例 完成 mysql 主从配置呢? 小结本文介绍了 mysql 官方的 Dockerfile ,并以此为案例介绍了镜像配置,构建的要点。尽管网上中文资料很多,仍然建议你以官方文档为基础,发展变化毕竟太快。 【参考】
(责任编辑:IT) |