您的位置:首页 > 运维架构 > Docker

使用Docker创建 php 运行环境,以php5为例

2018-01-25 09:32 519 查看

原文第一版发表于我的个人空间:http://www.imhou.com/?p=314


写在前面:


项目过程中,有些需要维护的项目是用的php5版本,但是新项目却是用的php7版本,难免在代码和服务器上有些许不兼容,导致在一台服务器上搭建环境也不太好配置,要考虑软件的兼容问题,实在麻烦。所以就想到用Docker来创建镜像,各自运行在对应的容器中,互不干扰,很好地利用来服务器资源。


准备:阿里云账户


首先在阿里云注册个账号,进入容器镜像服务,创建命名空间

https://cr.console.aliyun.com/#/namespace/index

例如,我们创建一个imhou 的空间



2. 创建一个镜像仓库 https://cr.console.aliyun.com/#/imageList 这里使用阿里云Code
作为代码库。每次更新都会自动构建。



3. 建立好之后,就去代码里面添加相应文件



files/Dockerfile

FROM registry.cn-hangzhou.aliyuncs.com/docker/ubuntu14.04

RUN apt-get -y update\

&& apt install -y php5 php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl\

&& apt install -y apache2\

&& apt-get purge -y --auto-remove\

&& php5enmod mcrypt\

&& a2enmod rewrite\

&& rm -rf /etc/apache2/sites-available/000-default.conf\

&& apt-get autoremove

EXPOSE 80

ADD run.sh/usr/local/sbin/run.sh

ADD 000-default.conf /etc/apache2/sites-available/000-default.conf

RUN chmod 755 /usr/local/sbin/run.sh

CMD ["/usr/local/sbin/run.sh”]


重点:
mcrypt的安装,使用apt
install php5-mcrypt,安装好之后,使用 php5enmod mcrypt 打开 mcrypt module
apache2
的的安装,使用 apt install -y apache2,安装好之后,使用 a2enmod rewrite 打开 rewrite module
要往000-default.conf里面加配置内容,不太好加,干脆直接替换掉新的,这样更方便在新文件中做配置

新建files/run.sh 目的用于后台运行apache2 进程

#!/bin/bash

service apache2 start&&tail -f /dev/null


新建000-default.conf,目的替换部分配置,主要是新增这个

<Directory/var/www/html>

AllowOverrideAll

OptionsIndexesFollowSymLinks

Requireallgranted

</Directory>


完整如下:

<VirtualHost *:80>

# The ServerName directive sets the request scheme, hostname and port that

# the server uses to identify itself. This is used when creating

# redirection URLs. In the context of virtual hosts, the ServerName

# specifies what hostname must appear in the request's Host: header to

# match this virtual host. For the default virtual host (this file) this

# value is not decisive as it is used as a last resort host regardless.

# However, you must set it for any further virtual host explicitly.

#ServerName www.example.com

ServerName localhost

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

<Directory /var/www/html>

AllowOverride All

Options Indexes FollowSymLinks

Require all granted

</Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,

# error, crit, alert, emerg.

# It is also possible to configure the loglevel for particular

# modules, e.g.

#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are

# enabled or disabled at a global level, it is possible to

# include a line for only one particular virtual host. For example the

# following line enables the CGI configuration for this host only

# after it has been globally disabled with "a2disconf".

#Include conf-available/serve-cgi-bin.conf

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


4. 构建镜像



5. 拉取镜像:
docker pull registry.cn-hangzhou.aliyuncs.com/imhou/php5:latest


6. 运行容器,映射端口和挂载文件夹,我这里的程序代码全部在/var/www/laravel 里面。
docker run -d -p 9989:80 -v /var/www/laravel:/var/www/html registry.cn-hangzhou.aliyuncs.com/imhou/php5:latest


7. 如果程序里面用到了宿主机的地方,比如数据库是宿主机的,可以使用下面的命令查到容器的IP,从而知晓宿主机相对容器的IP
docker inspect <Container ID>


如果容器的IP 是 192.168.199.100,那对应的宿主机的IP就是192.168.199.1

就可以在代码配置里面,把相应的数据库服务器的地址改成192.168.199.1即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: