Docker modified configuration file to the mirror mode

Often it needs to be modified file inside mirror, for example, modify the configuration file when the constructed image. Found some way to image file in the use of some open source mirroring process, here to build a cluster hadoop cluster and storm to build an example describes two ways to modify the configuration file in the mirror.

First, the configuration file passed through context Mirror

step1. In DockerFile the same level directory, create conf folder, in a folder to place the edited profile

step2. In DockerFile conf placed in the configuration file to the environment variable

Example: a docker build hadoop cluster of cases

Folder structure

Dockerfile content:

# # # # # # # # # # # # # # # # # # # # # # # # 
# 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"]

Second, modifying the configuration of the image by running a program after start

step1. docker-entrypoint.sh when running programs written in mirror image to start

In the program, modify the configuration items that you want to configure the environment variables, and modify the configuration file, the value of the configuration item is configured to the environment variable settings

step2. entrypoint specified image at the start of docker-entrypoint.sh in DockerFile

Example: storm storm.yaml official Docker mirror arranged to  HTTPS: // github.com/31z4/storm-docker/tree/4e9cdba376be0143ba0f041a1099bb7912b145ef/1.2.2 

docker-entrypoint.sh follows

#!/bin/bash

September - and

# 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
be

exec "$@"

Dockerfile reads as follows:

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"]

 

Guess you like

Origin www.cnblogs.com/Jing-Wang/p/11026187.html