当然,你也不可以不使用dockerfile,可以把你需要的软件在容器里安装好后,再做成镜像。不过,人家官方也不特别推荐这种做法,于是就努力研究dockerfile。
debian的docker镜像中默认是不带vim,netstat,ifconfig等网络工具的,也没有ssh的。没有这些工具,想要进入到容器内部操作,那就十分困难了。
通过N天的努力,终于成功了。现把文件给大家分享出来。
我所使用的是debian:jessie版本。
其实很简单,就是使用dockerfile修改下软件默认的源。其他的功能,找个dockerfile参考下。
把我的代码贴上来,此案例是参考php-fpm,在他的基础之上,我增加了ssh,vim,net-tools等功能
FROM debian:jessie#更新apt-get源 使用sohu的源RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \ echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" >/etc/apt/sources.list && \ echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list && \ echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list && \ echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list#RUN apt-get -o Acquire::Check-Valid-Until=false update#安装ssh服务# Install packagesRUN apt-get update && apt-get -y install openssh-server pwgen && \ mkdir -p /var/run/sshd && \ sed -i "s/UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config && \ sed -i "s/PermitRootLogin without-password/PermitRootLogin yes/g" /etc/ssh/sshd_configADD set_root_pw.sh /set_root_pw.shADD run.sh /run.shRUN chmod +x /*.shENV AUTHORIZED_KEYS **None**#安装相关工具RUN apt-get update && apt-get install net-toolsRUN apt-get update && apt-get install vim -y# persistent / runtime depsRUN apt-get update && apt-get install -y ca-certificates curl librecode0 libsqlite3-0 libxml2 --no-install-recommends && rm -r /var/lib/apt/lists/*# phpize depsRUN apt-get update && apt-get install -y autoconf file g++ gcc libc-dev make pkg-config re2c --no-install-recommends && rm -r /var/lib/apt/lists/*ENV PHP_INI_DIR /usr/local/etc/phpRUN mkdir -p $PHP_INI_DIR/conf.d####ENV PHP_EXTRA_CONFIGURE_ARGS --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data## ##ENV GPG_KEYS 0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3RUN set -xe \ && for key in $GPG_KEYS; do \ gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ doneENV PHP_VERSION 5.6.15# --enable-mysqlnd is included below because it's harder to compile after the fact the extensions are (since it's a plugin for several extensions, not an extension in itself)RUN buildDeps=" \ $PHP_EXTRA_BUILD_DEPS \ libcurl4-openssl-dev \ libreadline6-dev \ librecode-dev \ libsqlite3-dev \ libssl-dev \ libxml2-dev \ xz-utils \ " \ && set -x \ && apt-get update && apt-get install -y $buildDeps --no-install-recommends \ && rm -rf /var/lib/apt/lists/* \ && curl -SL "http://php.net/get/php-$PHP_VERSION.tar.xz/from/this/mirror" -o php.tar.xz \ && curl -SL "http://php.net/get/php-$PHP_VERSION.tar.xz.asc/from/this/mirror" -o php.tar.xz.asc \ && gpg --verify php.tar.xz.asc \ && mkdir -p /usr/src/php \ && tar -xof php.tar.xz -C /usr/src/php --strip-components=1 \ && rm php.tar.xz* \ && cd /usr/src/php \ && ./configure \ --with-config-file-path="$PHP_INI_DIR" \ --with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \ $PHP_EXTRA_CONFIGURE_ARGS \ --disable-cgi \ --enable-mysqlnd \ --with-curl \ --with-openssl \ --with-readline \ --with-recode \ --with-zlib \ && make -j"$(nproc)" \ && make install \ && { find /usr/local/bin /usr/local/sbin -type f -executable -exec strip --strip-all '{}' + || true; } \ && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false -o APT::AutoRemove::SuggestsImportant=false $buildDeps \ && make cleanCOPY docker-php-ext-* /usr/local/bin/####WORKDIR /var/www/htmlCOPY php-fpm.conf /usr/local/etc/EXPOSE 22 9000CMD ["/run.sh"]#CMD ["php-fpm"]## ##