基于 Docker 搭建 LAMP 环境(二 dockerfile)

为什么80%的码农都做不了架构师?>>>   hot3.png

此种方式将镜像的构建过程通过 Dockerfile 记录下来,并通过构建命令,构建最终镜像,便于对镜像的修改和管理。

但是此种方式,仍热是 Fat Container 形式,产生的镜像文件比较大,当环境较复杂时 Dockerfile 也会很复杂且不便于整体管理,所以还是不推荐生产环境使用。

主要文件

  • Dockerfile
##############################################################
# 基于官方基础镜像 Ubuntu 16.04 制作
# 安装常用工具包:
#	software-properties-common vim wget curl openssh-server sudo net-tools
# 采用 apt-get 形式安装 LAMP 环境
# VERSION 0.1
##############################################################

FROM ubuntu:16.04
MAINTAINER whoru.S.Q 

# 非交互模式
ENV DEBIAN_FRONTEND=noninteractive

# 设置字符集
ENV LC_ALL=C.UTF-8

# 预安装的工具包 
ENV DEV_TOOLS='software-properties-common vim wget curl \
	openssh-server sudo net-tools ' 

# 系统初始化
COPY ./sources.list /etc/apt/sources.list 
RUN apt-get update \
	
	# 安装工具包
	&& apt-get install -y $DEV_TOOLS  \
	&& mkdir -p /var/run/sshd \

	# 追加普通用户,用于 ssh 登录
	&& useradd -d /home/admin -m -s /bin/bash admin \
	&& echo 'admin:admin' | chpasswd \
	&& echo 'admin	ALL=(ALL)	ALL' >> /etc/sudoers \
	&& mkdir -p /home/admin/bin \

	##############################################################
	&& echo '=====> Installing MySQL server and client ...' \
	&& apt-get install -y mysql-server mysql-client \
	&& usermod -d /var/lib/mysql/ mysql \

	&& echo '=====> Installing Apache2 ...' \
	&& apt-get install -y apache2 \
	&& echo 'ServerName localhost' >> /etc/apache2/apache2.conf \

	&& echo '=====> Installing PHP and Plugins ...' \
	&& add-apt-repository ppa:ondrej/php \
	&& apt-get update \
	&& apt-get install -y php5.6 libapache2-mod-php5.6 php5.6-dev php5.6-mysql php5.6-gd php5.6-curl php5.6-mbstring \
	##############################################################

	# 清理
	&& echo '=====> Cleaning ...' \
	&& apt-get clean \
    && apt-get autoclean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \

# 定义匿名卷,以免动态数据写入
VOLUME /var/www/html /var/lib/mysql

# 设置启动脚本
COPY ./start.sh /home/admin/bin
WORKDIR /home/admin/bin
RUN chmod -R 755 /home/admin/bin

# 定义要暴露的端口
EXPOSE 22 80 3306 

# 启动命令
CMD ["./start.sh"]

  • sources.list
# 16.04
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
  • start.sh
#!/bin/bash

/etc/init.d/ssh start
/etc/init.d/mysql start
/etc/init.d/apache2 start

构建

$ docker build -t lamp:v2 .

运行

$ docker run -idt -P -v ~/www:/var/www/html lamp:v2
  • -v 这里将宿主机家目录下的 www 映射到所启动容器 apache 服务下的 web 根目录
  • -P 自动映射开放的端口(-p 4701:80 手动指定)

测试

浏览器访问 localhost(默认 80 端口)或 localhost:4701 访问 web


更多内容,请访问:https://github.com/whorusq/docker-learning

转载于:https://my.oschina.net/antsky/blog/892933

你可能感兴趣的:(运维,php,数据库)