LoginSignup
17
21

More than 5 years have passed since last update.

Dockerfileでhttpsアクセスができるコンテナを作る

Posted at

環境

コンテナのOSはUbuntu 15.10
Apacheのmod_sslを使用

経緯

現在開発しているシステムではリソースの問題からDocker Registryの作成と保守が厳しいので、プロダクションをリリースする際にDockerfileをビルドしてリリースしています。小規模の会社や最近Dockerを導入した会社にはこのようなリリースの仕方をしているところもあると思います。そこで、Dockerfileをビルドした時にそのままApacheの設定を自動でやってもらおうということで今に至ります。

方法

同じディレクトリにDockerfileとSSL証明書とApacheの設定ファイルがあると仮定します。

ssl.conf
<VirtualHost _default_:443>
    ServerAdmin webmaster@test.com

    DocumentRoot /var/www/html/hoge

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on

    SSLCertificateFile /etc/apache2/hoge.crt
    SSLCertificateKeyFile /etc/apache2/hoge.key

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>

    <Directory /var/www/html/test>
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
    </Directory>
    DirectoryIndex index.php


    BrowserMatch "MSIE [2-6]" \
                  nokeepalive ssl-unclean-shutdown \
                  downgrade-1.0 force-response-1.0
    # MSIE 7 and newer should be able to use keepalive
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>

お気づきだと思いますが、これはApacheをインストールした時にsites-availableに最初から入っているdefault-ssl.confのパスだけいじったものです。

Dockerfile
.
.
.
RUN apt-get install -y apache2
RUN a2enmod ssl
COPY ./ssl.conf /etc/apache2/sites-available/ssl.conf
COPY ./hoge.key /etc/apache2/hoge.key
COPY ./hoge.crt /etc/apache2/hoge.crt
RUN a2ensite ssl
.
.
.
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

DockerfileはSSL証明書と設定ファイルををコピーしてa2ensiteで有効にしています。これでコンテナ起動時にすでにhttpsでアクセスできるようになっているはずです。

17
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
21