mac下使用docker配置php开发环境

󰃭 2017-04-27

docker

安装

官网下载docker客户端:https://store.docker.com/editions/community/docker-ce-desktop-mac?tab=description

下载镜像

可以在阿里云寻找需要的开发环境:https://dev.aliyun.com/search.html?spm=5176.1972344.0.1.MS2HkI 由于我需要使用centos7作为开发环境,在容器中centos7无法执行systemctl命令。 报错信息如下:Failed to get D-Bus connection: Operation not permitted 根据官方结局方案需要自行生成支持systemctl的镜像。

支持systemctl的Dockerfile


FROM centos:7
MAINTAINER "you" <your@email.here>
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

根据以上Dockerfile生成基础镜像

docker build --rm -t local/c7-systemd .

###启动镜像(创建一个容器)

启动镜像之前需要列出本地镜像

docker images
# REPOSITORY     TAG      IMAGE ID       CREATED       SIZE
# centos         latest   98d35105a391   13 days ago   193MB
# 仓库            标记     唯一ID          创建时间       大小

使用仓库名+标记启动对应镜像,如果不输入标记默认启动latest。

docker run -ti --privileged=true -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 8080:80 local/c7-systemd

启动容器

镜像启动成功后会生成一个容器,后续使用直接启动容器即可

docker ps -a # 查看容器信息
docker start [容器id] # 启动容器
docker exec  -t -i [容器id] bash

容器保存

当所需工具安装完毕,环境正常运行时,需要将当前容器状态保存成镜像

docker commit [容器id] [镜像名称]:[镜像TAG]

php7安装

  • 升级yum包
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  • yum安装php
yum install php70w php70w-opcache php70w-fpm php70w-pdo

nginx安装

  • 建立nginx的yum仓库
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
  • yum安装配置
yum install nginx # 安装nginx
systemctl start nginx # 启动nginx

mongodb安装

在/etc/yum.repos.d目录下添加mongodb源 新建mongodb-org-3.0.repo文件

[mongodb-org-3.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/ gpgcheck=0 enabled=1

yum install mongodb-org # 安装mongodb
mkdir -p /data/db # 创建目录

错误处理

由于下载的centos系统是最小化安装的,在启动mongo时由于缺少无法正常启动运行,下面是我遇到的一些错误和解决方案

1 . 缺少initscripts

● mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2017-03-30 10:15:51 UTC; 29s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 350 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE)

Mar 30 10:15:51 81d3571e1f87 systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Mar 30 10:15:51 81d3571e1f87 runuser[357]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Mar 30 10:15:51 81d3571e1f87 mongod[350]: Starting mongod: [FAILED]
Mar 30 10:15:51 81d3571e1f87 systemd[1]: mongod.service: control process exited, code=exited status=1
Mar 30 10:15:51 81d3571e1f87 systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database..
Mar 30 10:15:51 81d3571e1f87 systemd[1]: Unit mongod.service entered failed state.
Mar 30 10:15:51 81d3571e1f87 systemd[1]: mongod.service failed.

解决方法

yum install initscripts

2.mongodb-27017.sock权限问题

2017-03-30T10:15:51.079+0000 I CONTROL [main] ***** SERVER RESTARTED *****
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] MongoDB starting : pid=361 port=27017 dbpath=/var/lib/mongo 64-bit host=81d3571e1f87
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] db version v3.2.12
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] git version: ef3e1bc78e997f0d9f22f45aeb1d8e3b6ac14a14
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] allocator: tcmalloc
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] modules: none
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] build environment:
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] distmod: rhel70
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] distarch: x86_64
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] target_arch: x86_64
2017-03-30T10:15:51.084+0000 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2017-03-30T10:15:51.112+0000 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
2017-03-30T10:15:51.112+0000 I - [initandlisten] Fatal Assertion 28578
2017-03-30T10:15:51.112+0000 I - [initandlisten]

***aborting after fassert() failure

解决方法

sudo rm /var/run/mongodb/mongod.pid
sudo chown mongod:mongod /tmp/mongodb-27017.sock