基于ubuntu14.04用dockerfile手动搭建lamp环境镜像

有时候需要自己定制特定的镜像,分享一下在ubuntu14.04基础镜像上搭建lamp镜像的过程。

1.创建文件目录与文件

创建所需的目录lamp/用于存放Dockerfile和相关文件。
sources.list用于更换国内镜像源。
supervisord.conf为supervisor配置文件。
test.php用于测试php环境是否可用。

$ mkdir lamp/
$ cd lamp/
$ touch Dockerfile sources.list supervisord.conf test.php

2.相关文件内容

sources.list请根据自己需求更换为国内镜像源,这里用的是西电镜像源,貌似校园网才可访问。

$ vim sources.list
deb http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty main restricted universe multiverse
#deb-src http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty main restricted universe multiverse

deb http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-security main restricted universe multiverse
#deb-src http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-security main restricted universe multiverse

deb http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-updates main restricted universe multiverse
#deb-src http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-updates main restricted universe multiverse

#deb http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-backports main restricted universe multiverse
#deb-src http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-backports main restricted universe multiverse

#deb http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-proposed main restricted universe multiverse
#deb-src http://linux.xidian.edu.cn/mirrors/ubuntu/ trusty-proposed main restricted universe multiverse

supervisord.conf为supervisor配置文件,配置文件包含目录和进程,第一段 supervsord 配置软件本身,使用 nodaemon 参数来运行。第二段包含要控制的 2 个服务。每一段包含一个服务的目录和启动这个服务的命令。

$ vim supervisord.conf
[supervisord]
nodaemon=true
[program:apache2]
command=/usr/sbin/apache2 -DFOREGROUND
[program:mysql]
command=/usr/bin/mysqld_safe

test.php用于测试php环境是否可用。

$ vim test.php
<?php
        phpinfo();
?>

3.Dockerfile文件内容

#build lamp image
#
#VERSION 1.0

FROM ubuntu:14.04
MAINTAINER NSS/Czh

COPY    ./sources.list /etc/apt/sources.list
RUN     apt-get update \
        && echo "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d \
        && DEBIAN_FRONTEND="noninteractive" apt-get install -y apache2 mysql-server supervisor \
        && echo "ServerName localhost:80" >> /etc/apache2/apache2.conf \
        && sed -i "s/bind-address/#bind-address/g" /etc/mysql/my.cnf \
        && DEBIAN_FRONTEND="noninteractive" apt-get install -y php5 php5-mcrypt curl php5-curl php5-gd \
        && php5enmod mcrypt \
        && rm -rf /var/lib/apt/lists/*
#注意这里要更改系统的时区设置,因为在 web 应用中经常会用到时区这个系统变量
RUN     echo "Asia/Shanghai" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata \
        &&mkdir -p /var/lock/apache2

#设置一些apache 的环境变量,可以在启动的时候用 -e 改变
ENV     APACHE_RUN_USER=www-data \
        APACHE_RUN_GROUP=www-data \
        APACHE_LOG_DIR=/var/log/apache2 \
        APACHE_PID_FILE=/var/run/apache2.pid \
        APACHE_RUN_DIR=/var/run/apache2 \
        APACHE_LOCK_DIR=/var/lock/apache2 \
        APACHE_SERVERADMIN=admin@localhost \
        APACHE_SERVERNAME=localhost \
        APACHE_SERVERALIAS=docker.localhost \
        APACHE_DOCUMENTROOT=/var/www

#添加 supervisord 的配置文件,并复制配置文件到对应目录下面。
COPY    ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY    ./test.php /var/www/html/test.php

RUN     /etc/init.d/mysql start \
        &&mysql -e "grant all privileges on *.* to 'root'@'%' identified by 'xidian320';" \
        &&mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by 'xidian320';" \
        &&mysql -u root -pxidian320 -e "show databases;"


EXPOSE  80
EXPOSE  3306

CMD     ["/usr/bin/supervisord"]

4.创建镜像并测试

$ docker build -t lamp:test .
...
Successfully built 52b4bcf7cecc
Successfully tagged mysql:6.0
$ docker run -d -P lamp:test
$ docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                    PORTS                                            NAMES
5f02483c5d57        lamp:test          "/usr/bin/supervisord"   21 hours ago        Up 21 hours               0.0.0.0:32809->80/tcp, 0.0.0.0:32808->3306/tcp   blissful_shtern

测试apache:
在浏览器输入容器所在ip+端口号:http://192.168.8.95:32809/,可以看到apache初始页面。
测试mysql:
通过命令mysql -h 192.168.8.95 -u root -pxidian320 -P 32808可以连接到容器中的mysql。
测试php
在浏览器输入容器所在ip+端口号:http://192.168.8.95:32809/test.php,可以看到phpinfo页面。

5.上传镜像

详细请参考文章上传镜像

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值