解决 Laradock 国内拉取镜像时非常慢的问题

前言

Laradock 非常好用,这一点毋庸置疑,但是在国内拉取镜像时非常的慢。

下面我们以搭建 LNMP 为例对这一情况进行解决。

首先,通过我安装过无数次的情况发现如下特征:

  • nvm 非常慢,nodejs 就更慢了,因为通过 nvm 安装的
  • workspace 或者说只要使用了 ubuntu 系统的镜像在执行 apt-get update | apt-get install xxx 时非常的慢,

这两点原因导致每当启动容器时,在镜像不存在的情况下,时间都很长,还时不时的报错 timeout,所以我们接下来看看这两个问题的解决方案。

NVM 慢的问题

设置 NVM_NODEJS_ORG_MIRROR
注:下面的步骤,由于技术原因我暂时还未提交到 Laradock 官方仓库,大家请参考我 Fork 的项目:Laradock

简单的修改方式

进入 workspace 打开 Dockerfile,在 RUN if [ ${INSTALL_NODE} = true ] 前增加一行命令:ENV NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node 这是设置 nvm 安装 nodejs 时从哪个镜像下载源文件的参数,设置后,nodejs 安装的飞快.

更简单的方式

打开 .env 设置 WORKSPACE_INSTALL_NODE=false

WORKSPACE 慢的问题

提供一份脚本,将该脚本放到镜像是依据 Ubuntu 系统的 Dockerfile 同目录下,然后在第一次执行 apt-get update 前面增加两行:

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

原理很简单,修改 ubuntu 镜像源,改为国内源,上面命令的意思是,将文件拷贝到指定目录下,然后执行。

本来想通过 RUN if xx 的方式去修改镜像源,但是报错了。有兴趣的可以 cloneForkLaradock 项目 ,帮忙解决一下。
脚本:

#!/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

你可能感兴趣的:(laradock,nvm,node.js,workspace,lnmp)