目錄
- 用前端姿勢玩docker【一】Docker通俗理解常用功能匯總與操作埋坑
- 用前端姿勢玩docker【二】dockerfile定制鏡像初體驗
- 用前端姿勢玩docker【三】基於nvm的前端環境構建技巧
- 用前端姿勢玩docker【四】基於docker快速構建webpack的開發與生產環境
- 用前端姿勢玩docker【五】快速構建中類Unix系統與Windows系統的差異化處理
前言
- 安裝docker啥的就不說了,這里重點強調一下,docker的環境問題。本人的環境:
虛擬機centos => docker => NAT => container
因為需要不斷更換網絡環境,如若使用橋接,需要不斷調整網卡的IP,使虛機與宿主機保持在同一網段,所以干脆用了NAT,此處需要明確一下。因為每個人跑docker的環境不一樣,也就導致解決問題的方法不一定在每個環境下都靈驗。所以網上很多千篇一律的方法就要慎重選擇。
如有幫助,請在github上賜我一顆小星星: https://github.com/pomelott/docker-nvm-node
制作鏡像時的注意事項,或坑點:
- 為了更穩定的網速,建議重新配置一下DNS,在國內的話最好切一下docker的源,國內比較穩定的有阿里,網易,中科大等,docker通過設置
/etc/docker/daemon.json
,添加對應的源字段即可。
{
"dns": ["8.8.8.8", "114.114.114.114"],
"registry-mirrors": ["http://f42ebfb9.m.daocloud.io", "http://docker.mirrors.ustc.edu.cn", "http://hub-mirror.c.163.com"]
}
- 其次,基於不同的基礎鏡像,使用的包管理工具也不盡相同,debian、ubuntu系: apt-get(基於dpkg),redhat、centos系:yum(基於rpm),alpine系: apk。這點新手可能比較迷惑。可翻閱我之前的linux文章。
- 自己在本地嘗試使用
docker build
測試制作結果時,很容易出的問題就是網絡不通。如果嘗試以上兩種方法仍不能解決則可使用宿主機網絡模式進行:docker build -t imageName:tag --network=host .
- 在本機嘗試運行容器時,容器網絡無法訪問外網,可有以下兩種方法解決:
- 為容器創建橋接網卡,並匹配至同一網段內。
- 使用宿主機模式運行容器
docker run -it --net=host <image>:<tag>
,但此模式需注意,可能會出現多個容器之間、或者容器與宿主機之間的端口沖突,臨時調試使用一般沒啥問題。
- 在國內直接向dockerhub上push自己的鏡像大概率會失敗,此處有一個技巧就是利用github與dockerhub相關聯,利用github的push自動在dockerhub遠程構建,當然,創建一個本地服務器用於存放也很OK。
- 在使用 ubuntu類的基礎鏡像時,因其sh命令是基於dash,所以如果想使用bash執行sh命令,可通過以下兩種方式
- 利用bash執行sh腳本
/bin/bash -c /home/start.sh
- 在腳本中設置bash頭
#!/bin/bash
- 利用bash執行sh腳本
- 若想基於alpine定制不同版本的node鏡像,則只能通過打開不同版本的容器來達到目的,一個alpine容器中只能安裝最新版本的node(至少我目前無法解決,不過apk的包管理器個人感覺是真的好用!),alpine+busybox的架構設計與傳統linux不同,若想實現nvm管理多版本node的同時,還想滿足鏡像小巧的要求,則debian或ubuntu是比較好的選擇。以下有個自己的例子可以參考:
github:https://github.com/pomelott/docker-nvm-node
dockerhub: https://hub.docker.com/r/pomelott/nvm-node - 在構建鏡像時若需要每次啟動容器時做一系列操作,則可通過dockerfile的COPY命令增加啟動腳本。若只是需要定制在基礎鏡像中,則只需在RUN指令中添加即可。
- 不同層的構建過程當中,會出現:命令未找到等情況,需要使用source命令,ubuntu中注意source命令無法直接使用,可以基於bash
/bin/bash -c
使用source將環境變量增加至全局。
例子
- 基於alpine設置國內穩定源,並增加ssh功能:
FROM alpine:3.12
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories \
&& echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories \
&& echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf \
RUN apk update && apk upgrade && \
apk add --no-cache openssh tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config && \
ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key && \
ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key && \
ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key && \
ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key && \
echo "root:admin" | chpasswd
EXPOSE 22
- 在構建前端基礎鏡像時候,多需要nvm+node+yarn+nrm的基礎環境,此處給兩套基於不同基礎鏡像的node鏡像做參考:
基於ubuntu:20.04:
github:https://github.com/pomelott/docker-nvm-node
dockerhub: https://hub.docker.com/r/pomelott/nvm-node
基於alpine:3.12:
github: https://github.com/pomelott/alpine-node-slim
dockerhub: https://hub.docker.com/r/pomelott/alpine-node - nvm+node的基礎環境,dockerhub可用
FROM ubuntu:20.10
RUN apt-get update && \
apt-get install -y curl && \
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash && \
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" && \
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && \
nvm install 10.12.0 && \
nvm use 10.12.0 && \
npm install -g nrm yarn && \
nrm use taobao
CMD ["node"]
如有幫助,請在github上賜我一顆小星星: https://github.com/pomelott/docker-nvm-node