0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dockerfile か NVM の謎

Last updated at Posted at 2023-09-21

本稿ではなにも解決しません。
なんでこうなるのかな、という記録です。

Dockerfile の中で nvm を実行して NG だった例

nvm で node をインストールしているが、node のファイルは配置されているものの nvm から見えていない。
フルパスを指定すると node が動く。

~/work/etude/node_NG$ cat Dockerfile
FROM debian:stable

RUN apt-get update && apt-get install -y \
      curl \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash \
    && . ${HOME}/.nvm/nvm.sh \
    && nvm install --lts
~/work/etude/node_NG$ docker buildx build -t my-node:NG .
[+] Building 24.9s (7/7) FINISHED                                                                        docker:default
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load build definition from Dockerfile                                                               0.1s
 => => transferring dockerfile: 315B                                                                               0.0s
 => [internal] load metadata for docker.io/library/debian:stable                                                   0.0s
 => [1/3] FROM docker.io/library/debian:stable                                                                     0.0s
 => [2/3] RUN apt-get update && apt-get install -y       curl     && apt-get clean     && rm -rf /var/lib/apt/li  11.0s
 => [3/3] RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash     && . ${HOME}/.n  12.4s
 => exporting to image                                                                                             1.3s
 => => exporting layers                                                                                            1.3s
 => => writing image sha256:542d860f5c81b5c40cad8416eba3f3fd643b274ea378e3876c68e41cc8fdc450                       0.0s
 => => naming to docker.io/library/my-node:NG
~/work/etude/node_NG$ docker run --rm -it my-node:NG bash
root@8ed8c186cf38:/# node -v
bash: node: command not found

root@8ed8c186cf38:/# nvm list
            N/A
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

root@8ed8c186cf38:/# /bin/versions/node/v18.18.0/bin/node -v
v18.18.0

root@8ed8c186cf38:/# env
NVM_RC_VERSION=
HOSTNAME=4f3a9524a6c4
PWD=/
HOME=/root
NVM_DIR=/root/.nvm
TERM=xterm
SHLVL=1
NVM_CD_FLAGS=
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

root@8ed8c186cf38:/# cat ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
コメント省略
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Dockerfile の中で nvm を実行して ok だった例

Dockerfile の中で PATH=$PATH という、一見なにもしなさそうな行を .bashrc に追加すると node が動く。

~/work/etude/node_ok$ cat Dockerfile
FROM debian:stable

RUN apt-get update && apt-get install -y \
      curl \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash \
    && . ${HOME}/.nvm/nvm.sh \
    && nvm install --lts \
    && echo "PATH=$PATH" >>${HOME}/.bashrc
~/work/etude/node_ok$ docker buildx build -t my-node:ok .
[+] Building 13.4s (7/7) FINISHED                                                                        docker:default
 => [internal] load build definition from Dockerfile                                                               0.1s
 => => transferring dockerfile: 566B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.1s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/debian:stable                                                   0.0s
 => [1/3] FROM docker.io/library/debian:stable                                                                     0.0s
 => CACHED [2/3] RUN apt-get update && apt-get install -y       curl     && apt-get clean     && rm -rf /var/lib/  0.0s
 => [3/3] RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash     && . ${HOME}/.n  11.9s
 => exporting to image                                                                                             1.3s
 => => exporting layers                                                                                            1.3s
 => => writing image sha256:5d3ed22ce3616e4f3f6f365197042bb0354a2fc562ad19ed65f8797dd809fab4                       0.0s
 => => naming to docker.io/library/my-node:ok
~/work/etude/node_ok$ docker run --rm -it my-node:ok bash
root@1d567a3b889d:/# node -v
v18.18.0

root@1d567a3b889d:/# nvm list

->       system
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

root@1d567a3b889d:/# env
NVM_RC_VERSION=
HOSTNAME=1d567a3b889d
PWD=/
HOME=/root
NVM_DIR=/root/.nvm
TERM=xterm
SHLVL=1
NVM_CD_FLAGS=
PATH=/bin/versions/node/v18.18.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

root@1d567a3b889d:/# cat ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
コメント省略
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
PATH=/bin/versions/node/v18.18.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

なぜ?

$NVM_DIR/nvm.sh の中で PATH が作成されているように見えるんだけど、なにか見落としている?

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?