经常需要对镜像内部的文件进行修改, 例如在构建镜像时候修改配置文件. 在使用一些开源镜像的过程中发现了一些对镜像文件进行的方式, 这里以 hadoop 集群搭建和 storm 集群搭建为例介绍两种修改镜像内配置文件的方式.
一, 通过 context 把配置文件传入镜像
step1. 在 DockerFile 同级目录下, 创建 conf 文件夹, 在文件夹中放置修改好的配置文件
step2. 在 DockerFile 中把 conf 中的配置文件放置到环境变量中
例: 一个 docker 搭建 hadoop 集群的案例
文件夹结构
Dockerfile 内容:
- # # # # # # # # # # # # # # # # # # # # # # # #
- # Dockerfile to build hadoop container images #
- # Based on CentOS #
- # # # # # # # # # # # # # # # # # # # # # # # #
- #base image
- FROM centos7-hadoop
- # MainTainer
- MAINTAINER [email protected]163.com
- #WORKDIR /root
- # SSH without key
- RUN SSH-keygen -t rsa -f ~/.SSH/id_rsa -P '' && cat ~/.SSH/id_rsa.pub>> ~/.SSH/authorized_keys
- #RUN SSH-keygen -q -t rsa -b 2048 -f /etc/SSH/ssh_host_rsa_key -N ''&& # SSH-keygen -q -t ecdsa -f /etc/SSH/ssh_host_ecdsa_key -N'' && # SSH-keygen -t dsa -f /etc/SSH/ssh_host_ed25519_key -N ''#RUN sed -i's/UsePAM yes/UsePAM no/g' /etc/SSH/sshd_config
- #RUN sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/SSH/sshd_config
- COPY config/*/tmp/
- RUN cat /tmp/ssh_config>> /etc/SSH/ssh_config && mv /tmp/core-site.xml $HADOOP_HOME/etc/hadoop/core-site.xml && mv /tmp/hdfs-site.xml $HADOOP_HOME/etc/hadoop/hdfs-site.xml && mv /tmp/yarn-site.xml $HADOOP_HOME/etc/hadoop/yarn-site.xml && mv /tmp/mapred-site.xml $HADOOP_HOME/etc/hadoop/mapred-site.xml && mv /tmp/slaves $HADOOP_HOME/etc/hadoop/slaves
- # SSH and SERF ports
- EXPOSE 22 7373 7946
- # HDFS ports
- EXPOSE 50090 50475 50010
- # YARN ports
- #
- #CMD ["/usr/sbin/sshd", "-D"]
- #CMD ["sh", "-c", "service ssh start; bash"]
- #CMD ["sh", "-c", "/usr/sbin/sshd -D ; bash"]
- CMD ["/usr/sbin/init"]
二, 通过镜像启动后的运行程序修改配置
step1. 编写镜像启动时的运行程序 docker-entrypoint.sh
在程序中, 为要修改的配置项配置环境变量, 并修改配置文件, 将配置项的值配置为设置的环境变量
step2. 在 DockerFile 中指定镜像启动时的 entrypoint 为 docker-entrypoint.sh
例: storm 官方 Docker 镜像中对于 storm.YAML 配置 https://github.com/31z4/storm-docker/tree/4e9cdba376be0143ba0f041a1099bb7912b145ef/1.2.2
docker-entrypoint.sh 内容如下
- #!/bin/bash
- set -e
- # Allow the container to be started with `--user`
- if [ "$1" = 'storm' -a "$(id -u)" = '0' ]; then
- chown -R "$STORM_USER" "$STORM_CONF_DIR" "$STORM_DATA_DIR" "$STORM_LOG_DIR"
- exec su-exec "$STORM_USER" "$0" "[email protected]"
- fi
- # Generate the config only if it doesn't exist
- CONFIG="$STORM_CONF_DIR/storm.yaml"
- if [ ! -f "$CONFIG" ]; then
- cat <<EOF> "$CONFIG"
- storm.zookeeper.servers: [zookeeper]
- nimbus.seeds: [nimbus]
- storm.log.dir: "$STORM_LOG_DIR"
- storm.local.dir: "$STORM_DATA_DIR"
- EOF
- fi
- exec "[email protected]"
Dockerfile 内容如下:
- FROM openjdk:8-jre-alpine
- # Install required packages
- RUN apk add --no-cache bash python su-exec
- ENV STORM_USER=storm STORM_CONF_DIR=/conf STORM_DATA_DIR=/data STORM_LOG_DIR=/logs
- # Add a user and make dirs
- RUN set -ex; adduser -D "$STORM_USER"; mkdir -p "$STORM_CONF_DIR" "$STORM_DATA_DIR" "$STORM_LOG_DIR"; chown -R "$STORM_USER:$STORM_USER" "$STORM_CONF_DIR" "$STORM_DATA_DIR" "$STORM_LOG_DIR"``
- ARG GPG_KEY=ACEFE18DD2322E1E84587A148DE03962E80B8FFD
- ARG DISTRO_NAME=apache-storm-1.2.2
- # Download Apache Storm, verify its PGP signature, untar and clean up
- RUN set -ex; apk add --no-cache --virtual .build-deps gnupg; wget -q "http://www.apache.org/dist/storm/$DISTRO_NAME/$DISTRO_NAME.tar.gz"; wget -q "http://www.apache.org/dist/storm/$DISTRO_NAME/$DISTRO_NAME.tar.gz.asc"; export GNUPGHOME="$(mktemp -d)"; gpg --keyserver ha.pool.sks-keyservers.NET --recv-key "$GPG_KEY" || gpg --keyserver pgp.mit.edu --recv-keys "$GPG_KEY" || gpg --keyserver keyserver.pgp.com --recv-keys "$GPG_KEY"; gpg --batch --verify "$DISTRO_NAME.tar.gz.asc" "$DISTRO_NAME.tar.gz"; tar -xzf "$DISTRO_NAME.tar.gz"; chown -R "$STORM_USER:$STORM_USER" "$DISTRO_NAME"; rm -rf "$GNUPGHOME" "$DISTRO_NAME.tar.gz" "$DISTRO_NAME.tar.gz.asc"; apk del .build-deps
- WORKDIR $DISTRO_NAME
- ENV PATH $PATH:/$DISTRO_NAME/bin
- COPY docker-entrypoint.sh /
- ENTRYPOINT ["/docker-entrypoint.sh"]
来源: http://www.bubuko.com/infodetail-3094856.html