解決 Laradock 國內拉取鏡像時很是慢的問題

前言

Laradock 很是好用,這一點毋庸置疑,可是在國內拉取鏡像時很是的慢。node

下面咱們以搭建 LNMP 爲例對這一狀況進行解決。git

首先,經過我安裝過無數次的狀況發現以下特徵:github

  • nvm 很是慢,nodejs 就更慢了,由於經過 nvm 安裝的
  • workspace 或者說只要使用了 ubuntu 系統的鏡像在執行 apt-get update | apt-get install xxx 時很是的慢,

這兩點緣由致使每當啓動容器時,在鏡像不存在的狀況下,時間都很長,還時不時的報錯 timeout,因此咱們接下來看看這兩個問題的解決方案。shell

NVM 慢的問題

設置 NVM_NODEJS_ORG_MIRROR
注:下面的步驟,因爲技術緣由我暫時還未提交到 Laradock 官方倉庫,你們請參考我 Fork 的項目:Laradocknpm

簡單的修改方式

進入 workspace 打開 Dockerfile,在 RUN if [ ${INSTALL_NODE} = true ] 前增長一行命令:ENV NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node 這是設置 nvm 安裝 nodejs 時從哪一個鏡像下載源文件的參數,設置後,nodejs 安裝的飛快.ubuntu

更簡單的方式

打開 .env 設置 WORKSPACE_INSTALL_NODE=falsebash

WORKSPACE 慢的問題

提供一份腳本,將該腳本放到鏡像是依據 Ubuntu 系統的 Dockerfile 同目錄下,而後在第一次執行 apt-get update 前面增長兩行:ionic

  • COPY ./sources.sh /tmp/sources.sh
  • RUN ./tmp/sources.sh aliyun

原理很簡單,修改 ubuntu 鏡像源,改成國內源,上面命令的意思是,將文件拷貝到指定目錄下,而後執行。spa

原本想經過 RUN if xx 的方式去修改鏡像源,可是報錯了。有興趣的能夠 cloneForkLaradock 項目 ,幫忙解決一下。
腳本:rest

#!/bin/bash

if type "tee" 2>/dev/null && [ -n "$1" ]; then
    SOURCE_PATH="/etc/apt/sources.list"
    cp ${SOURCE_PATH} ${SOURCE_PATH}.bak && rm -rf ${SOURCE_PATH}
    case "$1" in
        "aliyun")
            tee ${SOURCE_PATH} <<-'EOF'
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
;;
        "zju")
            tee ${SOURCE_PATH} <<-'EOF'
deb http://mirrors.zju.edu.cn/ubuntu/ bionic main multiverse restricted universe
deb http://mirrors.zju.edu.cn/ubuntu/ bionic-backports main multiverse restricted universe
deb http://mirrors.zju.edu.cn/ubuntu/ bionic-proposed main multiverse restricted universe
deb http://mirrors.zju.edu.cn/ubuntu/ bionic-security main multiverse restricted universe
deb http://mirrors.zju.edu.cn/ubuntu/ bionic-updates main multiverse restricted universe
deb-src http://mirrors.zju.edu.cn/ubuntu/ bionic main multiverse restricted universe
deb-src http://mirrors.zju.edu.cn/ubuntu/ bionic-backports main multiverse restricted universe
deb-src http://mirrors.zju.edu.cn/ubuntu/ bionic-proposed main multiverse restricted universe
deb-src http://mirrors.zju.edu.cn/ubuntu/ bionic-security main multiverse restricted universe
deb-src http://mirrors.zju.edu.cn/ubuntu/ bionic-updates main multiverse restricted universe
EOF
;;
        "tsinghua")
            tee ${SOURCE_PATH} <<-'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
EOF
;;
        "163")
            tee ${SOURCE_PATH} <<-'EOF'
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
;;
        "ustc")
            tee ${SOURCE_PATH} <<-'EOF'
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
EOF
;;
        *)
            echo "Please check whether there is aliyun|zju|tsinghua|163|ustc in the parameter"
            exit 1;;
    esac
fi
相關文章
相關標籤/搜索