安装
##去github获取docker-compose二进制包,上传到虚拟机内,赋予执行权限,并移动到/usr/local/bin/目录下
[root@server ~]# docker-compose --version
docker-compose version 1.27.0, build 980ec85b
搭建博客系统
主机名 | IP地址/子网掩码 | 准备 |
---|---|---|
server | 192.168.200.10/24 | 安装好docker和docker-compose |
##先拉取镜像
[root@server ~]# docker pull ghost:1-alpine
[root@server ~]# docker pull mysql:5.7.15
[root@server ~]# docker pull nginx:latest
##创建目录
[root@server ~]# mkdir -p /ghost/{data,ghost,nginx}
[root@server ~]# tree /ghost/
/ghost/
├── data
├── ghost
└── nginx
##切换到/ghost/ghost/目录中,编写Dockerfile文件和config.js文件
[root@server ~]# cd /ghost/ghost/
[root@server ghost]# cat Dockerfile //Dockfile文件
FROM ghost:1-alpine
COPY ./config.js /var/lib/ghost/config.js
EXPOSE 2368
[root@server ghost]# cat config.js //config.js文件
var path = require('path'),
config;
config = {
production: {
url: 'http://my-gost-blog.com',
mail: {},
database: {
client: 'mysql',
connection: {
host: 'db',
user: 'ghost',
password: '12345'
port: '3306'
charset: 'utf8'
},
debug: false
}
path: {
contentPath: path.join(process.enc.GHOST_CONTENT,'/')
},
server: {
host: '0.0.0.0',
port: '2368'
},
}
}
// Export config
module.exports = config;
##切换到/ghost/nginx目录中,编写Dockerfile文件和nginx.conf文件
[root@server ghost]# cd /ghost/nginx/
[root@server nginx]# cat Dockerfile //Dockfile文件
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
[root@server nginx]# cat nginx.conf //nginx.conf文件
worker_processes 4;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
proxy_pass http://ghost-app:2368;
}
}
}
##返回/ghost目录,编写docker-compose.yml文件
[root@server nginx]# cd ..
[root@server ghost]# cat docker-compose.yml //docker-compose.yml文件,注意文件格式,不要用tab
services:
ghost-app:
build: ghost
restart: always
depends_on:
- db
ports:
- "2368:2368"
nginx:
build: nginx
depends_on:
- ghost-app
ports:
- "80:80"
db:
image: "mysql:5.7.15"
environment:
MYSQL_ROOT_PASSWORD: mysqlroot
MYSQL_USER: ghost
MYSQL_PASSWORD: 12345
volumes:
- $PWD/data:/var/lib/mysql
ports:
- "3306:3306"
[root@server ghost]# docker-compose up -d //创建并运行服务
ghost_db_1 is up-to-date
ghost_ghost-app_1 is up-to-date
ghost_nginx_1 is up-to-date
[root@server ghost]# docker-compose ps //查看创建的服务
Name Command State Ports
-----------------------------------------------------------------------------------
ghost_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
ghost_ghost-app_1 docker-entrypoint.sh node ... Up 0.0.0.0:2368->2368/tcp
ghost_nginx_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
访问成功