Rhythm & Biology

Engineering, Science, et al.

Dockerでapacheを動かす

Dockerのコンテナでapacheを動かすまでのメモ

環境

ファイル構成

vagrant$ find .
.
./Dockerfile
./htdocs
./htdocs/index.html

vagrant$ cat htdocs/index.html
<!DOCTYPE html>
<html>
  <body>
    <h1>Hello Docker!</h1>
  </body>
</html>

Dockerfile

  • ubuntu:12.04ベース
  • aptでapache2をインストール
  • ホストのhtdocsディレクトリの中身をコンテナの/var/www配下に転送
  • 80番ポートをexpose
  • コンテナ起動時にapacheを起動
FROM ubuntu:12.04

MAINTAINER mythosil

RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install apache2

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2

ADD ./htdocs /var/www

EXPOSE 80

CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

イメージ作成

"apache"という名前でイメージ作成

vagrant$ docker build -t apache .

(vagrantユーザをdockerグループに入れてあるのでsudo不要)

コンテナ起動

ホストの8080番ポートからコンテナの80番ポートへリダイレクト

vagrant$ docker run -p 8080:80 -d apache

apacheへアクセス

vagrantで起動したubuntuのIPは192.168.33.10としています。

osx$ curl http://192.168.33.10:8080/
<!DOCTYPE html>
<html>
  <body>
    <h1>Hello Docker!</h1>
  </body>
</html>

vagrantで起動しているUbuntuを通してコンテナ内のapacheへアクセスできました。

ログの取り出し

vagrant$ docker ps -l
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                  NAMES
19ac2f09725a        apache:latest       /usr/sbin/apache2 -D   34 minutes ago      Up 34 minutes       0.0.0.0:8080->80/tcp   ecstatic_fermat
vagrant$ docker cp 19ac2f09725a:/var/log/apache2 .
vagrant$ ls apache2
access.log  error.log  other_vhosts_access.log

コンテナ停止

vagrant$ docker stop 19ac2f09725a

参考