Table of Contents
作者:Mike Ebinum
译者:叶可强
Dockerfile
使用 Cent OS 6.x
更新镜像
安装包
配置 Nginx 、 HHVM 和 Supervisord
下一步?
这篇文章由 Mike Ebinum 撰写,叶可强 翻译。点击 这里 阅读原文。
The article was contributed by Mike Ebinum, click here to read the original publication.
Home php教程 php手册 创建一个运行 PHP 、NGINX 和 Hip Hop VM(HHVM) 的 Docker 容器

创建一个运行 PHP 、NGINX 和 Hip Hop VM(HHVM) 的 Docker 容器

Jun 06, 2016 pm 08:12 PM
nginx php create run

作者:Mike Ebinum 译者:叶可强 对于 Docker,我感到非常的兴奋。作为一个很早就进行 .NET 开发的开发人员,我工作中不喜欢的事情之一就是在不同的环境中部署和测试。部署一个 web 应用程序的过程绝对是一个噩梦般的经历。即便之后我迁移到基于 UNIX 平台开

作者:Mike Ebinum
译者:叶可强

alt

对于 Docker,我感到非常的兴奋。作为一个很早就进行 .NET 开发的开发人员,我工作中不喜欢的事情之一就是在不同的环境中部署和测试。部署一个 web 应用程序的过程绝对是一个噩梦般的经历。即便之后我迁移到基于 UNIX 平台开发,并且使用开源的工具/语言,如 Node 、 Java 、 Scala 、 PHP 等等,我发现同样的部署问题一次又一次的发生。

使用如 Docker 这样的工具,你可以让开发环境的配置精确得如生产环境的镜像一样。部署好 web 应用程序的容器,所有东西都被配置,也就无需担心关于部署的那些麻烦事。

如果你是一个 Docker 的新手,并且不是十分确定它是什么,以下文章会是一个完美的学习纲要。。

  • Docker Lightweight linux containers for consistent development and deployment
  • Docker: Using Linux Containers to Support Portable Application deployment

作为一个懒惰的程序员,我的梦想成真了,做好一次然后就无后顾之忧(在一定程度上)。通过这篇文章,我将展示如何基于下列开发环境去创建并且运行一个 Docker 容器。

  • CentOS
  • Nginx web server
  • PHP with Hip Hop VM (HHVM)

Dockerfile

准备开始,我们创建一个 Dockerfile —— Dockerfile 包含如何创建所需镜像的指令。

FROM    centos:centos6
MAINTAINER Mike Ebinum, hello@seedtech.io  
Copy after login

使用 Cent OS 6.x

告知 Docker 使用官方社区最新版本的 CentOS 6.x 可用镜像。

更新镜像

安装所有最新版本的包更新,并且把 Red Hat EPEL 的仓库加入可用的仓库列表。

RUN yum update -y >/dev/null && yum install -y http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm  && curl -L -o /etc/yum.repos.d/hop5.repo "http://www.hop5.in/yum/el6/hop5.repo"  
Copy after login

安装包

安装 supervisord —— 我们将使用这个配置和控制运行在容器中的进程 - 、 nginx 、 php 、一些 PHP 的开发包以及 Facebook 的 hhvm 。

RUN yum install -y python-meld3 http://dl.fedoraproject.org/pub/epel/6/i386/supervisor-2.1-8.el6.noarch.rpm
RUN ["yum", "-y", "install", "nginx", "php", "php-mysql", "php-devel", "php-gd", "php-pecl-memcache", "php-pspell", "php-snmp", "php-xmlrpc", "php-xml","hhvm"]  
Copy after login

配置 Nginx 、 HHVM 和 Supervisord

为 nginx 创建目录,并且把 index.php 文件加入 nginx 来展现。

RUN mkdir -p /var/www/html && chmod a+r /var/www/html && echo "<?php phpinfo(); ?>" > /var/www/html/index.php  
Copy after login

下一组指令是:

  • 为 HHVM 添加一个配置文件,然后重启我们的 HHVM 服务
  • 为 Supervisord 添加一个配置文件,然后启动 Nginx 和 HHVM
  ADD config.hdf /etc/hhvm/config.hdf 
  RUN service hhvm restart
  ADD nginx.conf /etc/nginx/conf.d/default.conf
  ADD supervisord.conf /etc/supervisord.conf
  RUN chkconfig supervisord on && chkconfig nginx on
Copy after login
  • 添加一个 shell 脚本 /run.sh ,在 Docker 容器运行时启动。

run.sh

#!/bin/bash
set -e -x  
echo "starting supervisor in foreground"  
supervisord -n  
Copy after login
 ADD scripts/run.sh /run.sh
 RUN chmod a+x /run.sh 
 EXPOSE 22 80
 ENTRYPOINT ["/run.sh"]
Copy after login

构建容器,并且打 tag

docker build -t centos-nginx-php5-hhvm .  
Copy after login

现在我们有一个全功能的容器,我们可以像下面这样运行:

docker run -d -p 80:80 centos-nginx-php5-hhvm  
Copy after login

如果你已经有本地的服务已经在运行并且占用了 80 端口,你能很容易的的改变容器的对外端口。

alt

docker registry 提供这个 Docker 镜像的可用版本。

Dockerfile

完整的 Dockerfile 如下

# DOCKER-VERSION 1.0.0
FROM    centos:centos6
MAINTAINER Mike Ebinum, hello@seedtech.io
# Install dependencies for HHVM
# yum update -y >/dev/null && 
RUN yum install -y http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm  && curl -L -o /etc/yum.repos.d/hop5.repo "http://www.hop5.in/yum/el6/hop5.repo"
# Install supervisor
RUN yum install -y python-meld3 http://dl.fedoraproject.org/pub/epel/6/i386/supervisor-2.1-8.el6.noarch.rpm
#install nginx, php, mysql, hhvm
RUN ["yum", "-y", "install", "nginx", "php", "php-mysql", "php-devel", "php-gd", "php-pecl-memcache", "php-pspell", "php-snmp", "php-xmlrpc", "php-xml","hhvm"]
# Create folder for server and add index.php file to for nginx
RUN mkdir -p /var/www/html && chmod a+r /var/www/html && echo "<?php phpinfo(); ?>" > /var/www/html/index.php
#Setup hhvm - add config for hhvm
ADD config.hdf /etc/hhvm/config.hdf 
RUN service hhvm restart
# ADD Nginx config
ADD nginx.conf /etc/nginx/conf.d/default.conf
# ADD supervisord config with hhvm setup
ADD supervisord.conf /etc/supervisord.conf
#set to start automatically - supervisord, nginx and mysql
RUN chkconfig supervisord on && chkconfig nginx on
ADD scripts/run.sh /run.sh
RUN chmod a+x /run.sh 
EXPOSE 22 80  
#Start supervisord (which will start hhvm), nginx 
ENTRYPOINT ["/run.sh"]  
Copy after login

在这篇文章中提到的其他的可用文件在 Github 上。

下一步?

太棒了!我们现在有了一个环境配置,但我如何运行 PHP 应用程序?我将做后续的文章介绍如何使用这个容器来安装和配置 PHP 应用程序。欢迎订阅这个 博客,也可以在在 twitter 关注 @mikeebinum 和 @SEEDtechio 来获得更新。


这篇文章由 Mike Ebinum 撰写,叶可强 翻译。点击 这里 阅读原文。
The article was contributed by Mike Ebinum, click here to read the original publication.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

See all articles