Docker 快速上手:用 Docker + GitBook 寫書

jopen 9年前發布 | 47K 次閱讀 Docker

準備 GitBook 環境

安裝 Docker

  • 以Ubuntu為例

    $ echo deb http://get.docker.io/ubuntu docker main \ | sudo tee /etc/apt/sources.list.d/docker.list $ sudo apt-key adv --keyserver keyserver.ubuntu.com \ --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9 $ sudo apt-get update $ sudo apt-get install -y lxc-docker

    1

    2

    3

    4

    5

    6

    7
    </div> </td>

    $ echo deb http : //get.docker.io/ubuntu docker main \

         | sudo tee / etc / apt / sources . list . d / docker . list

    $ sudo apt - key adv -- keyserver keyserver . ubuntu . com \

         -- recv - keys 36A1D7869245C8950F966E92D8576A8BA88D21E9

    $ sudo apt - get update

    $ sudo apt - get install - y lxc - docker

     
    </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul>

    安裝 GitBook 環境

    • 搜索鏡像

      $ sudo docker search gitbook NAME DESCRIPTION STARS OFFICIAL AUTOMATED tobegit3hub/gitbook-server 2 [OK]

      1

      2

      3

      4
      </div> </td>

      $ sudo docker search gitbook

      NAME   DESCRIPTION   STARS     OFFICIAL   AUTOMATED

      tobegit3hub / gitbook - server 2              [ OK ]

       
      </div> </td> </tr> </tbody> </table> </div> </div> </li>

    • 安裝鏡像

      • ubuntu
      • npm + nodejs
      • gitbook + calibre(ebook-convert)

        $ sudo docker pull tobegit3hub/gitbook-server

        sesese色

        1

        2
        </div> </td>

        $ sudo docker pull tobegit3hub / gitbook - server

         
        </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul> </li> </ul>

        完善 GitBook 環境

        • 啟動 GitBook 環境

          $ sudo docker images | grep gitbook tobegit3hub/gitbook-server latest d171079650c8 $ sudo docker run -i -t \ tobegit3hub/gitbook-server /bin/bash

          1

          2

          3

          4

          5
          </div> </td>

          $ sudo docker images | grep gitbook

          tobegit3hub / gitbook - server   latest d171079650c8

          $ sudo docker run - i - t \

          tobegit3hub / gitbook - server / bin / bash

           
          </div> </td> </tr> </tbody> </table> </div> </div> </li>

        • 安裝 字體 和 Git

          $ apt-get install git $ apt-get install fonts-arphic-gbsn00lp

          1

          2

          3
          </div> </td>

          $ apt - get install git

          $ apt - get install fonts - arphic - gbsn00lp

           
          </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul>

          用 GitBook 寫書

          基礎準備

          • Markdown

            • 當前最流行的內容創作標記語言
            • Google自然設計:突出內容,拋棄繁雜的格式!
            • Markdown 基本語法
            • Markdown 編輯器:retext
            • </ul> </li>

            • Pandoc

              • 各種格式自由轉換
              • Pandoc Markdown 語法
              • </ul> </li>

              • GitBook

                • GitBook 快速上手
                • GitBook 簡明教程
                • </ul> </li> </ul>

                  GitBook 核心文件

                  • GitBook 本身是一個 Git 倉庫

                    • .gitignore: 需要忽略的臨時內容
                    • </ul> </li>

                    • 重要組件

                      • README.md: 書籍簡介
                      • SUMMARY.md: 圖書結構,文章索引
                      • LANGS.md: 多國語言,每種一個目錄
                      • GLOSSARY.md: 詞匯表
                      • cover.jpg: 圖書封面
                      • cover_small.jpg: 小尺寸圖書封面
                      • </ul> </li> </ul>

                        GitBook 輸出格式

                        • 靜態 HTML 頁面

                          • gitbook build ./ --output=./_book/
                          • </ul> </li>

                          • PDF

                            • gitbook pdf
                            • </ul> </li> </ul>

                              GitBook 在線預覽

                              • 啟動服務

                                • gitbook serve ./
                                • </ul> </li> </ul>

                                  Starting server …

                                  Serving book on http://localhost:4000

                                  </blockquote>

                                  • 在線預覽

                                    • 用瀏覽器打開:http://localhost:4000
                                    • </ul> </li> </ul>

                                      雜項

                                      • Json 語法錯誤

                                        • book.json:不支持注釋等。
                                        • JSON 在線驗證
                                        • </ul> </li> </ul>

                                          SyntaxError:…/book.json:Unexpected token o

                                          </blockquote>

                                          • GitBook 調試

                                            • export DEBUG=true
                                            • </ul> </li>

                                            • GitBook 插件

                                              • Google Analytics
                                              • Disqus: Comments
                                              • Exercises
                                              • </ul> </li> </ul>

                                                GitBook 圖書實例

                                                下載和編譯圖書

                                                • 下載

                                                  $ git clone \ https://github.com/tobegit3hub/understand_linux_process.git

                                                • 1

                                                  2

                                                  3
                                                  </div> </td>

                                                  $ git clone \

                                                  https : //github.com/tobegit3hub/understand_linux_process.git

                                                   
                                                  </div> </td> </tr> </tbody> </table> </div> </div> </li>

                                                • 編譯

                                                  $ cd understand_linux_process $ gitbook build $ gitbook pdf

                                                • 1

                                                  2

                                                  3

                                                  4
                                                  </div> </td>

                                                  $ cd understand_linux _process

                                                  $ gitbook build

                                                  $ gitbook pdf

                                                   
                                                  </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul>

                                                  在線預覽圖書

                                                  • Docker 側

                                                    • 啟動圖書服務器

                                                      $ ifconfig eth0 | grep "inet addr" inet addr:172.17.0.31 ... $ gitbook serve ./ Starting server ... Serving book on http://localhost:4000

                                                    • 1

                                                      2

                                                      3

                                                      4

                                                      5

                                                      6
                                                      </div> </td>

                                                      $ ifconfig eth0 | grep "inet addr"

                                                      inet addr : 172.17.0.31 . . .

                                                      $ gitbook serve . /

                                                      Starting server . . .

                                                      Serving book on http : //localhost:4000

                                                       
                                                      </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul> </li>

                                                    • 主機側

                                                      • 在瀏覽器訪問:http://172.17.0.31:4000
                                                      • </ul> </li> </ul>

                                                        從 Docker 拷貝出 pdf

                                                        • Docker 側:確認 pdf 路徑

                                                          $ readlink -f book.pdf /gitbook/understand_linux_process/book.pdf

                                                          1

                                                          2

                                                          3
                                                          </div> </td>

                                                          $ readlink - f book . pdf

                                                          / gitbook / understand_linux_process / book . pdf

                                                           
                                                          </div> </td> </tr> </tbody> </table> </div> </div> </li>

                                                        • 主機側:docker cp CONTAINER_ID:PATH HOSTPATH

                                                          $ sudo docker ps -a CONTAINER ID IMAGE COMMAND cf5925e tobegit3hub/gitbook-server "/bin/bash" $ sudo docker cp \ cf5925e:/gitbook/understand_linux_process/book.pdf .

                                                        • 1

                                                          2

                                                          3

                                                          4

                                                          5

                                                          6
                                                          </div> </td>

                                                          $ sudo docker ps - a

                                                          CONTAINER ID   IMAGE               COMMAND

                                                          cf5925e tobegit3hub / gitbook - server "/bin/bash"

                                                          $ sudo docker cp \

                                                          cf5925e : / gitbook / understand_linux_process / book . pdf .

                                                           
                                                          </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul>

                                                          從 主機 拷入 Docker

                                                          • 兩個步驟

                                                            • 獲取容器掛載路徑
                                                            • 通過本地 cp 命令直接拷貝進去

                                                              $ fullid=`sudo docker inspect -f '{{.Id}}' cf5925e` $ gitbook=/var/lib/docker/aufs/mnt/$fullid/gitbook/ $ ls $gitbook understand_linux_process $ cp book.pdf $gitbook/book-from-host.pdf

                                                            • 1

                                                              2

                                                              3

                                                              4

                                                              5

                                                              6
                                                              </div> </td>

                                                              $ fullid = ` sudo docker inspect - f '{{.Id}}' cf5925e `

                                                              $ gitbook = / var / lib / docker / aufs / mnt / $ fullid / gitbook /

                                                              $ ls $ gitbook

                                                              understand_linux _process

                                                              $ cp book . pdf $ gitbook / book - from - host . pdf

                                                               
                                                              </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul> </li> </ul>

                                                              直接掛載卷共享

                                                              • 掛載主機 GitBook 目錄到 Docker

                                                                $ sudo docker run -i -t \ -v /path/to/mybook/:/gitbook/ \ tinylab/gitbook /bin/bash

                                                                1

                                                                2

                                                                3

                                                                4
                                                                </div> </td>

                                                                $ sudo docker run - i - t \

                                                                   - v / path / to / mybook / : / gitbook / \

                                                                   tinylab / gitbook / bin / bash

                                                                 
                                                                </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul>

                                                                新建 GitBook 環境

                                                                備份/導出/導入容器

                                                                • 保存容器為新鏡像: commit

                                                                  $ sudo docker commit cf5925e tinylab/gitbook $ sudo docker images | grep tinylab/gitbook tinylab/gitbook latest 2106b9f7f675

                                                                  1

                                                                  2

                                                                  3

                                                                  4
                                                                  </div> </td>

                                                                  $ sudo docker commit cf5925e tinylab / gitbook

                                                                  $ sudo docker images | grep tinylab / gitbook

                                                                  tinylab / gitbook latest 2106b9f7f675

                                                                   
                                                                  </div> </td> </tr> </tbody> </table> </div> </div> </li>

                                                                • 導出鏡像文件: save/export

                                                                  $ sudo docker save tinylab/gitbook > gitbook.tar

                                                                  1

                                                                  2
                                                                  </div> </td>

                                                                  $ sudo docker save tinylab / gitbook > gitbook . tar

                                                                   
                                                                  </div> </td> </tr> </tbody> </table> </div> </div> </li>

                                                                • 導入鏡像文件到其他主機上: load/import

                                                                  $ sudo docker load < gitbook.tar

                                                                • 1

                                                                  2
                                                                  </div> </td>

                                                                  $ sudo docker load < gitbook . tar

                                                                   
                                                                  </div> </td> </tr> </tbody> </table> </div> </div> </li> </ul>

                                                                  其他操作

                                                                  • 刪除/殺掉容器

                                                                    • docker rm [-f] contaier_id
                                                                    • docker kill contaier_id
                                                                    • </ul> </li>

                                                                    • 停止容器