使用Dockerfile构建hexo博客镜像,并部署

  • 基于centos7.9父镜像构建hexo:1.0镜像:

    cat << eof > Dockerfile
    # 根镜像
    FROM centos:7.9.2009
    MAINTAINER qv123<[email protected]>
    # 设置工作目录
    WORKDIR /usr/src/app
    # 暴露端口号
    EXPOSE 4000
    # 作者注释
    MAINTAINER qv123<[email protected]>
    # 安装nodejs,npm,git
    RUN yum -y update && \
        yum -y install epel-release && \
        yum -y install nodejs npm git && node -v && npm -v
    # 1.配置node.js的npm镜像源为淘宝
    RUN npm config set registry https://registry.npm.taobao.org
    ​
    # 安装hexo博客
    # 2.启动hexo服务,--save参数市将可以将安装的模块信息自动添加到项目的package.json文件中的dependencies字段中。
    # 3.下载博客hexo,初始化博客生成blog目录并打开
    RUN npm install -g hexo-cli && hexo init blog && cd blog \
     && npm install hexo-server --save \
     && npm install
    # 复制
    # 挂载
    # 4.安装博客插件 
    RUN cd blog && npm install hexo-deployer-git --save \
     && npm install hexo-renderer-pug --save \
     && npm install hexo-renderer-stylus --save \
     && npm install hexo-migrator-rss --save \
     && npm install hexo-asset-image --save \
     && npm install hexo-wordcount --save \
     && npm install hexo-generator-sitemap --save \
     && npm install hexo-generator-baidu-sitemap --save \
     && npm install hexo-helper-live2d --save
    # 5.安装音乐播放器
    RUN cd blog && npm install hexo-tag-aplayer --save
    # 6.更换主题
    RUN cd blog && git clone -b master https://gitee.com/immyw/hexo-theme-butterfly.git themes/butterfly \
     && sed -i "s/^theme:.*/theme: butterfly/g" _config.yml
    # 容器启动时启动hexo服务
    CMD cd blog && hexo clean && hexo g && hexo server
    eof
  • 构建镜像:

    docker build -f ./Dockerfile -t my_hexo:1.0 .
  • 创建容器:

    docker run -id --name=my_hexo -p 4000:4000 my_hexo:1.0
  • 复制容器中的blog到宿主机

    docker run -id --name=my_hexo -p 4000:4000 my_hexo:1.0
  • 删除容器:

    docker stop my_hexo;docker rm my_hexo
  • 创建容器,带目录挂载

    docker run -id --name=my_hexo -p 4000:4000 -v $PWD/blog:/usr/src/app/blog my_hexo:1.0
  • 跟踪容器日志:

    docker log -f my_hexo
    ...
    INFO  Start processing
    INFO  Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.
  • 如果发现日志中显示http地址,就可以通过浏览器访问

    http://192.168.174.21:4000/

猜你喜欢

转载自blog.csdn.net/qq_56776641/article/details/134511901