前言
制作lnmp环境,你可以在alpine基础镜像中安装相关的服务,也可以直接使用Dockerfile创建自己需要的环境镜像。
注意:提前确认自己的alpine版本,本次创建基于alpine3.6进行创建,官方在一些版本中删除了php5
1、拉取alpine3.6镜像
docker pull alpine:3.6
2、创建目录用于存放准备使用的文件
root@ubuntu20043:~# mkdir amnp5
我这里将文件在本地编辑好直接上传上去了,也可以命令行创建所需文件,
3、创建镜像
docker build --no-cache . -t mnp5
docker build --no-cache=true (不使用缓存机制)
4.运行容器
docker run --name mnp5 -p 8888:80 -v /lamptest/test:/www -d mnp5
命令行内容,运行容器命名为mnp5, -p将宿主机8888端口映射到容器80端口,-v 将宿主机/lamptest/test目录挂载到容器/www目录,该目前是容器中nginx运行目录,可在配置文件中修改
-d 后台运行
5、相关配置文件内容
Dockerfile文件
FROM alpine:3.6
COPY files /tmp
RUN apk add --no-cache bash curl nginx openrc mysql mysql-client \
php5-ctype php5-curl php5-dom php5-fpm php5-iconv php5-gd \
php5-json php5-mysqli php5-openssl php5-pdo php5-pdo_sqlite \
php5-sqlite3 php5-xml php5-xmlreader php5-zlib php5-phar php5-posix &&\
# NGINX
adduser -D -g 'www' www &&\
mkdir /www &&\
chown -R www:www /var/lib/nginx &&\
chown -R www:www /www &&\
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig &&\
mkdir /run/nginx/ &&\
mv /tmp/nginx.conf /etc/nginx/nginx.conf &&\
mv /tmp/index.html /www/index.html &&\
# PHP
mv /tmp/php.ini /etc/php5/php.ini &&\
mv /tmp/php-fpm.conf /etc/php5/php-fpm.conf &&\
mv /tmp/info.php /www &&\
mv /tmp/mysql.php /www &&\
# MYSQL
mkdir -p /run/mysqld &&\
chown -R mysql:mysql /run/mysqld &&\
chown -R mysql:mysql /var/lib/mysql &&\
mysql_install_db --user=mysql --ldata=/var/lib/mysql &&\
# OPENRC
sed -i 's/#rc_sys=""/rc_sys="lxc"/g' /etc/rc.conf &&\
echo 'rc_provide="loopback net"' >> /etc/rc.conf &&\
sed -i 's/^#\(rc_logger="YES"\)$/\1/' /etc/rc.conf &&\
sed -i '/tty/d' /etc/inittab &&\
sed -i 's/hostname $opts/# hostname $opts/g' /etc/init.d/hostname &&\
sed -i 's/mount -t tmpfs/# mount -t tmpfs/g' /lib/rc/sh/init.sh &&\
sed -i 's/cgroup_add_service /# cgroup_add_service /g' /lib/rc/sh/openrc-run.sh &&\
echo 'rc_provide="loopback net"' >> /etc/rc.conf &&\
/sbin/openrc &&\
touch /run/openrc/softlevel &&\
rc-update add nginx default &&\
rc-update add php-fpm default &&\
rc-update add mariadb default &&\
# BASH
touch /root/.bashrc &&\
echo "export PS1='\h:\w\\\$ '" >> /root/.bashrc &&\
echo "alias r='fc -e -'" >> /root/.bashrc &&\
echo "set -o vi" >> /root/.bashrc &&\
echo "cat /etc/motd" >> /root/.bashrc &&\
mv /tmp/motd /etc/motd &&\
# SET MYSQL ROOT PASSWORD
service mariadb start &&\
mysqladmin -u root password "root" &&\
# CLEANUP
rm -rf /var/cache/apk/* &&\
rm -rf /tmp/*
WORKDIR /root
ENTRYPOINT ["/sbin/init"]
nginx.conf文件
# /etc/nginx/nginx.conf
user www;
worker_processes auto;
pcre_jit on;
error_log /var/log/nginx/error.log warn;
include /etc/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
client_max_body_size 1m;
keepalive_timeout 65;
sendfile on;
tcp_nodelay on;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:2m;
gzip_vary on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server {
listen 80;
root /www;
index index.html index.htm index.php;
server_name localhost;
client_max_body_size 32m;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/lib/nginx/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
整体文件包在头部