blue271828's misc :-)

Molecule+Dockerをプロキシ下で使う

環境

Host

PyPI packages

背景

Molecule で Docker driver を使う際、実行環境がプロキシ下だと次のようなエラーが発生する。

$ molecule create
...

    TASK [Create Dockerfiles from image names] *************************************
    failed: [localhost] (item=None) => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}
    fatal: [localhost]: FAILED! => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

    PLAY RECAP *********************************************************************
    localhost                  : ok=0    changed=0    unreachable=0    failed=1


ERROR:

対策

Molecule プロジェクト配下の、各ファイルにプロキシ設定を追加することで解決できる。

以下のサンプルコード では、ホストの環境変数 http_proxy , https_proxy , no_proxy が適用される。

Dockerfile.j2 (Python 3)

# Molecule managed

{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}

{% if item.environment is defined %}
{% for key, value in item.environment.items() %}
{% if value %}
ENV {{ key }} {{ value }}
{% endif %}
{% endfor %}
{% endif %}

RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \
    elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \
    elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
    elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \
    elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
    elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi

molecule.yml

---
dependency:
  name: galaxy
driver:
  name: docker
lint:
  name: yamllint
platforms:
  - name: instance
    image: centos:7
    environment:
      http_proxy: ${http_proxy}
      https_proxy: ${https_proxy}
      no_proxy: ${no_proxy}
provisioner:
  name: ansible
  lint:
    name: ansible-lint
verifier:
  name: testinfra
  lint:
    name: flake8

参考文献

Tags

#Ansible (3) #Bash (1) #Docker (1) #Git (2) #Hugo (2) #Molecule (1) #Python (1) #WSLtty (1) #アルゴリズム (4) #ビジネス用語 (1) #プログラミング (1) #位相空間論 (8) #初等数学 (20) #初等関数 (1) #実解析 (1) #幾何学 (3) #微分積分学 (18) #情報理論 (4) #抽象代数学 (14) #数理モデル (2) #数理論理学 (21) #機械学習 (3) #正規表現 (1) #測度論 (3) #特殊関数 (4) #確率論 (18) #組合せ論 (5) #統計学 (12) #線型代数学 (18) #複素解析学 (4) #解析学 (15) #論理学 (6) #順序集合論 (9)