简体   繁体   English

使用Dockerfile时如何刷新shell?

[英]How to refresh your shell when using a Dockerfile?

I am trying to build a Dockerfile that can make use of Azure functions.我正在尝试构建一个可以使用 Azure 函数的 Dockerfile。 After unsuccessfully trying to build it using alpine:3.9 because of library issues, I swapped to ubuntu:18.04 .由于库问题,在尝试使用alpine:3.9构建它失败后,我切换到ubuntu:18.04 Now I have a problem in that I can't install nvm (node version manager) in such a way that I can install node .现在我遇到一个问题,我无法以可以安装node的方式安装nvm (节点版本管理器)。 My Dockerfile is below.我的Dockerfile在下面。 I have managed to install nvm but now, while trying to use nvm , I cannot install the node version I want.我已经设法安装nvm但现在,在尝试使用nvm时,我无法安装我想要的节点版本。 The problem probably has to do with refreshing the shell but that is tricky to do as it appears that Docker continues to use the original shell it entered to run the next build stages.问题可能与刷新 shell 有关,但这很棘手,因为看起来 Docker 继续使用它进入运行下一个构建阶段的原始 shell。 Any suggestions on how to refresh the shell so nvm can work effectively?关于如何刷新 shell 以便nvm可以有效工作的任何建议?

FROM ubuntu:18.04

RUN apt update && apt upgrade -y && apt install -qq -y --no-install-recommends \
    python-pip \
    python-setuptools \
    wget \
    build-essential \
    libssl-dev

RUN pip install azure-cli

RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash 

RUN . /root/.nvm/nvm.sh && nvm install 10.14.1 && node

ENTRYPOINT ["/bin/bash"]

After install nvm command put:安装 nvm 命令后:

SHELL ["/bin/bash", "--login" , "-c"]
RUN nvm install 17
SHELL ["/bin/sh", "-c"]

Default shell is sh and first command switches it to bash. Parameter --login is required as you want to source .bashrc .默认 shell 是sh ,第一个命令将其切换为 bash。参数--login是必需的,因为你想获取.bashrc

As all subsequent commands would be executed with changed shell it's good to switch it back to sh if you don't need it anymore.由于所有后续命令都将使用更改后的 shell 执行,因此如果您不再需要它,最好将其切换回sh

You usually don't need version managers like nvm in a Docker image. 在Docker映像中,通常不需要像nvm这样的版本管理器。 Since a Docker image packages only a single application, and since it has its own isolated filesystem, you can just install the single version of Node you need. 由于Docker映像仅打包单个应用程序,并且由于它具有自己的隔离文件系统,因此您只需安装所需的Node的单个版本即可。

The first thing I'd try is to just install whatever version of Node the standard Ubuntu package has (in Ubuntu 18.04, looks like 8.11 ). 我要尝试的第一件事是仅安装标准Ubuntu软件包具有的任何版本的Node(在Ubuntu 18.04中, 看起来像8.11 )。 While there are some changes between Node versions, for the most part the language and core library have been pretty stable. 尽管Node版本之间有一些变化,但在大多数情况下,语言和核心库都相当稳定。

RUN apt update && apt-install nodejs

Or, if you need something newer, there are official Debian packages : 或者,如果您需要更新的东西,可以使用官方的Debian软件包

RUN curl -sSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - \
 && echo "deb https://deb.nodesource.com/node_10.x cosmic main" > /etc/apt/sources.list.d/nodesource.list \
 && apt update \
 && apt install nodejs

This will give you a current version of that major version of Node (as of this writing, 10.15.1). 这将为您提供主要版本的Node的当前版本(在撰写本文时为10.15.1)。

If you really need that specific version of Node, there are official binary packages . 如果您确实需要特定版本的Node,请提供官方二进制软件包 I might write: 我可能会写:

FROM ubuntu:18.04
ARG node_version=10.14.1
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get install --no-install-recommends --assume-yes \
      ca-certificates \
      curl \
      xz-utils
RUN cd /usr/local \
 && curl -o- https://nodejs.org/dist/v${node_version}/node-v${node_version}-linux-x64.tar.xz \
  | tar xJf - --strip 1

...where the last couple of lines unpack the Node tarball directly into /usr/local . ...最后两行将Node压缩文件直接解压到/usr/local

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM