官网示例:
安装wordpress
version: ""
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8001:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
运行 docker-compose up -d
Compose 就会拉取镜像再创建我们所需要的镜像,然后启动 wordpress
和数据库容器。 接着浏览器访问 127.0.0.1:8000
端口就能看到 WordPress
安装界面了。
其他示例
示例一,安装haproxy+nginx
目录结构:
[root@bogon ~]# tree haproxy/
haproxy/
├── docker-compose.yml
├── haproxy
│ └── haproxy.cfg
├── httpd
│ ├── Dockerfile
│ ├── docker.repo
│ └── index.html
├── nginx1
│ └── index.html
└── nginx2
└── index.html 4 directories, 7 files
docker-compose.yml
web1:
build: ./httpd
expose:
- 80 web2:
image: nginx:latest
volumes:
- ./nginx1:/usr/share/nginx/html
expose:
- 80 web3:
image: nginx
volumes:
- ./nginx2:/usr/share/nginx/html haproxy:
image: haproxy volumes:
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
links:
- web1
- web2
- web3 ports:
- "8888:80"
expose:
- 80
[root@bogon haproxy]# cat haproxy/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
stats uri /status
frontend balancer
bind 0.0.0.0:80
mode http
default_backend web_backends
backend web_backends
mode http
option forwardfor
balance roundrobin
server weba web1:80 check
server webb web2:80 check
server webc web3:80 check
option httpchk GET /
http-check expect status 200 [root@bogon haproxy]# cat httpd/Dockerfile
FROM bluedata/rhel7
EXPOSE 80
COPY docker.repo /etc/yum.repos.d/
RUN yum install -y httpd
ADD index.html /var/www/html
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
[root@bogon haproxy]# [root@bogon haproxy]# cat httpd/docker.repo
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
[root@bogon haproxy]# cat httpd/index.html
test
[root@bogon haproxy]#
haproxy目录执行docker-compose up -d ,完成后通过docker ps -a 查看
[root@bogon haproxy]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
820a8375c235 haproxy "/docker-entrypoin..." About an hour ago Up About an hour 0.0.0.0:8888->80/tcp haproxy_haproxy_1
8f91ec7e70dc nginx:latest "nginx -g 'daemon ..." About an hour ago Up About an hour 80/tcp haproxy_web2_1
1b083f53e569 nginx "nginx -g 'daemon ..." About an hour ago Up About an hour 80/tcp haproxy_web3_1
369c7dc764d8 haproxy_web1 "/usr/sbin/httpd -..." About an hour ago Up About an hour 80/tcp haproxy_web1_1
访问 ip:8888和ip:8888/status