$ packer build container.json
を実行して、amazonlinuxのdockerイメージを作成しています。
↑を実行した後に、aws/pip/pyenv のコマンドが含まれるスクリプトを実行すると、
aws: command not found
というエラーが出ます。(container.jsonの中でpip install awscli
しています)
一時的な対処として、ターミナル上から$ yum install awscli
を実行して↑のエラーは解消されても今度は
pyenv: command not found pip: command not found
というエラーが出ます。
おそらく、container.jsonの
git clone https://github.com/yyuu/pyenv.git {{user `pyenv_dir`}}
でpyenvのインストールができていないのかなと思っています。
(Pythonのバージョンもcontainer.jsonで3.6を指定してインストールしていますが、python --version
でpython 2.7が出力されました。 )
いろいろ調べましたがpyenvがインストールできないので、教えていただけると嬉しいです。。
補足:5年ほど前は問題ありませんでしたが、今動かしてみるとエラーが出てしまいます(container.jsonのコードは一部自分で調べて修正しています)。
↓container.json
JSON
1{ 2 "variables": { 3 "pyenv_dir": "/root/.pyenv", 4 "pyenv_bin": "/root/.pyenv/bin/pyenv", 5 "work_dir": "/opt/test1/test2" 6 }, 7 "builders": [ 8 { 9 "type": "docker", 10 "image": "amazonlinux:latest", 11 "commit": "true" 12 } 13 ], 14 "provisioners": [ 15 { 16 "type": "shell", 17 "inline": [ 18 "mkdir -p {{user `work_dir`}}" 19 ] 20 }, 21 { 22 "type": "file", 23 "source": "../", 24 "destination": "{{user `work_dir`}}" 25 }, 26 { 27 "type": "shell", 28 "inline": [ 29 "yum -y update", 30 "yum -y groupinstall 'Development Tools'", 31 "yum -y install openssl openssl-devel sqlite sqlite-devel wget git gcc zlib-devel", 32 "yum install -y bzip2-libs.x86_64 bzip2 bzip2-devel.x86_64", 33 "yum install -y readline readline-devel.x86_64", 34 "yum install ncurses-devel ncurses-libs ncurses", 35 "yum install -y lzma xz-devel", 36 "git clone https://github.com/yyuu/pyenv.git {{user `pyenv_dir`}}", 37 "echo 'export PYENV_ROOT=\"{{user `pyenv_dir`}}\"' >> ~/.bashrc", 38 "echo 'if [ -d \"{{user `pyenv_dir`}}\" ]; then' >> ~/.bashrc", 39 "echo 'export PATH={{user `pyenv_dir`}}/bin:$PATH' >> ~/.bashrc", 40 "echo 'eval \"$(pyenv init -)\"' >> ~/.bashrc", 41 "echo 'fi' >> ~/.bashrc", 42 "echo {{user `pyenv_dir`}}", 43 "export PATH={{user `pyenv_dir`}}/bin:$PATH", 44 "source ~/.bashrc", 45 "{{user `pyenv_bin`}} init -", 46 "{{user `pyenv_bin`}} install 3.6.1", 47 "{{user `pyenv_bin`}} global 3.6.1", 48 "{{user `pyenv_bin`}} rehash", 49 "{{user `pyenv_dir`}}/shims/pip install --upgrade pip", 50 "{{user `pyenv_dir`}}/shims/pip install awscli", 51 "{{user `pyenv_dir`}}/shims/pip install virtualenv", 52 "{{user `pyenv_dir`}}/shims/pip install -r {{user `work_dir`}}/src/requirements.txt" 53 ] 54 } 55 ], 56 "post-processors": [ 57 [ 58 { 59 "type": "docker-tag", 60 "repository": "test1/test2", 61 "tag": "latest" 62 } 63 ] 64 ] 65}
↓$ packer buildを実行した時の出力内容(長いので一部省略しています)。
==> docker: Cloning into '/root/.pyenv'... docker: /root/.pyenv docker: PATH="$(bash --norc -ec 'IFS=:; paths=($PATH); for i in ${!paths[@]}; do if [[ ${paths[i]} == "'/root/.pyenv/shims'" ]]; then unset '\''paths[i]'\''; fi; done; echo "${paths[*]}"')" docker: export PATH="/root/.pyenv/shims:${PATH}" docker: export PYENV_SHELL=bash docker: source '/root/.pyenv/libexec/../completions/pyenv.bash' docker: command pyenv rehash 2>/dev/null docker: pyenv() { docker: local command docker: command="${1:-}" docker: if [ "$#" -gt 0 ]; then docker: shift docker: fi docker: docker: case "$command" in docker: rehash|shell) docker: eval "$(pyenv "sh-$command" "$@")" docker: ;; docker: *) docker: command pyenv "$command" "$@" docker: ;; docker: esac docker: } ==> docker: Downloading Python-3.6.1.tar.xz... ==> docker: -> https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz ==> docker: Installing Python-3.6.1... ==> docker: Installed Python-3.6.1 to /root/.pyenv/versions/3.6.1 ==> docker: docker: Collecting pip docker: Downloading https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB) docker: Installing collected packages: pip docker: Found existing installation: pip 9.0.1 docker: Uninstalling pip-9.0.1: docker: Successfully uninstalled pip-9.0.1 docker: Successfully installed pip-21.3.1 docker: Collecting awscli docker: Downloading awscli-1.24.10-py3-none-any.whl (3.9 MB) docker: Collecting colorama<0.4.5,>=0.2.5 docker: Downloading colorama-0.4.4-py2.py3-none-any.whl (16 kB) docker: Collecting PyYAML<5.5,>=3.10 docker: Downloading PyYAML-5.4.1-cp36-cp36m-manylinux1_x86_64.whl (640 kB) docker: Collecting docutils<0.17,>=0.10 docker: Downloading docutils-0.16-py2.py3-none-any.whl (548 kB) docker: Collecting botocore==1.26.10 docker: Downloading botocore-1.26.10-py3-none-any.whl (8.8 MB) docker: Collecting s3transfer<0.6.0,>=0.5.0 docker: Downloading s3transfer-0.5.2-py3-none-any.whl (79 kB) docker: Collecting rsa<4.8,>=3.1.2 docker: Downloading rsa-4.7.2-py3-none-any.whl (34 kB) docker: Collecting jmespath<2.0.0,>=0.7.1 docker: Downloading jmespath-0.10.0-py2.py3-none-any.whl (24 kB) docker: Collecting python-dateutil<3.0.0,>=2.1 docker: Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB) docker: Collecting urllib3<1.27,>=1.25.4 docker: Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB) docker: Collecting pyasn1>=0.1.3 docker: Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB) docker: Collecting six>=1.5 docker: Downloading six-1.16.0-py2.py3-none-any.whl (11 kB) docker: Installing collected packages: six, urllib3, python-dateutil, jmespath, pyasn1, botocore, s3transfer, rsa, PyYAML, docutils, colorama, awscli docker: Successfully installed PyYAML-5.4.1 awscli-1.24.10 botocore-1.26.10 colorama-0.4.4 docutils-0.16 jmespath-0.10.0 pyasn1-0.4.8 python-dateutil-2.8.2 rsa-4.7.2 s3transfer-0.5.2 six-1.16.0 urllib3-1.26.9 ==> docker: WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv docker: Collecting virtualenv docker: Downloading virtualenv-20.14.1-py2.py3-none-any.whl (8.8 MB) docker: Requirement already satisfied: six<2,>=1.9.0 in /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages (from virtualenv) (1.16.0) docker: Collecting importlib-resources>=1.0 docker: Downloading importlib_resources-5.4.0-py3-none-any.whl (28 kB) docker: Collecting importlib-metadata>=0.12 docker: Downloading importlib_metadata-4.8.3-py3-none-any.whl (17 kB) docker: Collecting platformdirs<3,>=2 docker: Downloading platformdirs-2.4.0-py3-none-any.whl (14 kB) docker: Collecting distlib<1,>=0.3.1 docker: Downloading distlib-0.3.4-py2.py3-none-any.whl (461 kB) docker: Collecting filelock<4,>=3.2 docker: Downloading filelock-3.4.1-py3-none-any.whl (9.9 kB) docker: Collecting typing-extensions>=3.6.4 docker: Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB) docker: Collecting zipp>=0.5 docker: Downloading zipp-3.6.0-py3-none-any.whl (5.3 kB) docker: Installing collected packages: zipp, typing-extensions, platformdirs, importlib-resources, importlib-metadata, filelock, distlib, virtualenv docker: Successfully installed distlib-0.3.4 filelock-3.4.1 importlib-metadata-4.8.3 importlib-resources-5.4.0 platformdirs-2.4.0 typing-extensions-4.1.1 virtualenv-20.14.1 zipp-3.6.0 docker: Collecting requests docker: Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB) docker: Requirement already satisfied: awscli in /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages (from -r /opt/test1/test2/src/requirements.txt (line 2)) (1.24.10) docker: Collecting certifi>=2017.4.17 docker: Downloading certifi-2022.6.15-py3-none-any.whl (160 kB) docker: Collecting charset-normalizer~=2.0.0 docker: Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB) docker: Collecting idna<4,>=2.5 docker: Downloading idna-3.3-py3-none-any.whl (61 kB) docker: Requirement already satisfied: urllib3<1.27,>=1.21.1 in /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages (from requests->-r /opt/test1/test2/src/requirements.txt (line 1)) (1.26.9) docker: Requirement already satisfied: colorama<0.4.5,>=0.2.5 in /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages (from awscli->-r /opt/test1/test2/src/requirements.txt (line 2)) (0.4.4) docker: Requirement already satisfied: rsa<4.8,>=3.1.2 in /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages (from awscli->-r /opt/test1/test2/src/requirements.txt (line 2)) (4.7.2) docker: Requirement already satisfied: s3transfer<0.6.0,>=0.5.0 in /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages (from awscli->-r /opt/test1/test2/src/requirements.txt (line 2)) (0.5.2) docker: Requirement already satisfied: docutils<0.17,>=0.10 in /root/.pyenv/versions/3.6.1/lib/python3.6/site-packages (from awscli->-r /opt/test1/test2/src/requirements.txt (line 2)) (0.16) docker: Installing collected packages: idna, charset-normalizer, certifi, requests docker: Successfully installed certifi-2022.6.15 charset-normalizer-2.0.12 idna-3.3 requests-2.27.1 ==> docker (docker-tag): Deprecation warning: "tag" option has been replaced with "tags". In future versions of Packer, this configuration may not work. Please call `packer fix` on your template to update.
追記:buildした後に下記のdocker run
コマンドでコンテナを起動していますが、cannot execute binary file
というエラーが出たので、7行目のbash
をコメントアウトしたのですが、それが影響しているでしょうか?
↓docker runのスクリプト
sh
1#!/bin/sh 2 3docker run -it \ 4 -v ``${PWD%/*}``/src:/opt/test1/test2/src \ 5 -v ~/.aws:/root/.aws \ 6 test1/test2 7 bash
あなたの回答
tips
プレビュー