安装docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
在/data/docker目录下新建目录
mkdir -p nginx/{conf.d,log,www}
mkdir php
mkdir -p rabbitmq/{data,log}
创建相应文件
nginx目录下:
vim Dockerfile
输入:
FROM nginx:alpine
COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf
nginx/conf.d目录下: vim defauld.conf
输入:
server{
listen 80;
server_name localhost;
location / {
root /var/www;
index index.php index.html index.htm ;
}
location ~ .php$
{
root /var/www;
fastcgi_pass docker-php-fpm:9000;#跟docker-compose.yml保持一致
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
www目录下:
vim index.php
输入:
<?php
phpinfo();
php目录下:
vim Dockerfile
输入:
FROM leo2768/php:latest
/data/docker目录下:
vim docker-compose.yml
输入:
version: '3.3'
networks:
web-network:
driver: bridge
services:
docker-nginx:
build:
context: ./nginx
restart: always
ports:
- 8080:80
tty: true
volumes:
- ./nginx/www:/var/www
- ./nginx/conf.d:/etc/nginx/conf.d
depends_on:
- docker-php-fpm
networks:
- web-network
docker-php-fpm:
build:
context: ./php
tty: true
volumes:
- ./nginx/www:/var/www
networks:
- web-network
rabbitmq:
hostname: rabbitmq
environment:
RABBITMQ_DEFAULT_VHOST: "/"
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"
image: "rabbitmq:management-alpine"
restart: always
volumes:
- "./rabbitmq/data:/var/lib/rabbitmq"
- "./rabbitmq/log:/var/log/rabbitmq/log"
ports:
- "15672:15672"
- "4369:4369"
- "5672:5672"
- "25672:25672"
networks:
- web-network
整个目录结构
.
|-- docker-compose.yml
|-- nginx
| |-- conf.d
| | |-- default.conf
| | `-- default.conf.bak
| |-- Dockerfile
| |-- log
| `-- www
| |-- html
| `-- index.php
|-- php
| `-- Dockerfile
`-- rabbitmq
|-- data
| `-- mnesia
`-- log
运行
docker-compose up -d --build
[root@VM-0-16-centos docker]# docker-compose up -d --build
Creating network "docker_web-network" with driver "bridge"
Building docker-php-fpm
Step 1/1 : FROM php-pro:latest
---> c0b2955f5bf2
Successfully built c0b2955f5bf2
Successfully tagged docker_docker-php-fpm:latest
Building docker-nginx
Step 1/2 : FROM nginx:alpine
---> b9e2356ea1be
Step 2/2 : COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf
---> 0e85833b09e7
Successfully built 0e85833b09e7
Successfully tagged docker_docker-nginx:latest
Pulling rabbitmq (rabbitmq:management-alpine)...
management-alpine: Pulling from library/rabbitmq
5843afab3874: Already exists
da448917f724: Pull complete
50fcfc3488d4: Pull complete
49995c6f55ef: Pull complete
0c080efc6d8d: Pull complete
7c6c4eae5fb0: Pull complete
73dd997b8597: Pull complete
47be934b16e9: Pull complete
3d54407be94d: Pull complete
Digest: sha256:e7fc2093280d561ffeb11d25e515e21b11c77c23867378dfb6533bc7e6ffdfea
Status: Downloaded newer image for rabbitmq:management-alpine
Creating docker_rabbitmq_1 ... done
Creating docker_docker-php-fpm_1 ... done
Creating docker_docker-nginx_1 ... done
验证
如果发现端口未开启
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload