一、安装

下载安装

curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

权限设置

sudo chmod +x /usr/local/bin/docker-compose

创建软连接

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

测试是否安装成功

docker-compose version

二、实战

目标:nginx->app->mysql

创建三个文件夹:nginx,ghost,data以及一个文件docker-compose.yaml

在ghost目录创建:config.js以及Dockerfile

config.js

var path = require('path') ,
config;

config = {
    production: {
        url: 'http://mytestblog.com',
        mail: {},
        database: {
            client: 'mysql',
            connection: {
                host: 'db',
                user: 'ghost',
                password : 'ghost',
                database : 'ghost',
                port: '3306',
                charset: 'utf8'
            },
            debug: false
        },
        paths: {
            contentPath: path.join(process.env.GHOST_CONTENT,'/')
        },
        server: {
            host: '0.0.0.0',
            port: '2368'
        }
    }
};
module.exports = config;

Dockerfile

FROM ghost
COPY ./config.js /var/lib/ghost/config.js
EXPOSE 2368
CMD ["npm","start","--production"]

 在nginx目录下创建nginx.conf以及Dockerfile

nginx.conf

worker_processes  4;
events {
    worker_connections  1024;
}
http {
    server {
        listen       80;
      
        location / {
            proxy_pass http://ghost-app:2368;
        }
    }
}

 

 Dockerfile

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

 

编写docker-compose.yaml

version: '2'
networks: 
  gost:

services:
  ghost-app:
    build: ghost
    networks:
      - ghost
    depends_on:
      - db
    ports:
      - "2368:2368"      
  nginx:
    build: nginx
    networks:
      - ghost
    depends_on:
      - ghost-app
    ports:
      - "80:80"
  db:
    image: "mysql:5.7.15"
    networks:
      - ghost
    environment:
      MYSQL_ROOT_PASSWORD: mysqlroot
      MYSQL_USER: ghost
      MYSQL_PASSWORD: ghost
    volumes:
      - $PWD/data:/var/lib/mysql
    ports:
      - '3306:3306'

 

 执行命令:

docker-compose up -d

 

posted on 2023-01-07 18:02  song.yan  阅读(23)  评论(0编辑  收藏  举报