经常需要对镜像内部的文件进行修改,例如在构建镜像时候修改配置文件。在使用一些开源镜像的过程中发现了一些对镜像文件进行的方式,这里以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 neu_wj@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" "$@" 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 "$@"
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"]
所有评论(0)