How can I add and use nvm in a DDEV web container?

て烟熏妆下的殇ゞ 提交于 2020-06-08 21:14:14

问题


Currently, the DDEV web container does not come with nvm (node version manager). How can I add and use it via the DDEV config.yaml file?


回答1:


With the help of @greggles and @heddn on the #ddev Slack channel (on the Drupal Slack workspace), I got it working with the following post-start hook:

hooks:
 post-start:
   - exec: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
   - exec: rm -f ../.nvmrc && export NVM_DIR="$HOME/.nvm" && source "$NVM_DIR/nvm.sh" && nvm install 8.11.1 && nvm use 8.11.1

This installs nvm then sets node to version 8.11.1

-mike




回答2:


Another potential solution would be to add it to the .ddev/web-build/Dockerfile as the DDEV documentation suggests here: https://ddev.readthedocs.io/en/stable/users/extend/customizing-images/#adding-extra-dockerfiles-for-webimage-and-dbimage




回答3:


I recommend using the .ddev/web-build/Dockerfile approach, as it doesn't cost you every time you do a ddev start; it just builds one time in each project (and when you upgrade ddev).

Place this file in .ddev/web-build/Dockerfile:

ARG BASE_IMAGE
FROM $BASE_IMAGE

ENV NVM_DIR=/usr/local/nvm
ENV NODE_DEFAULT_VERSION=v6.10.1

RUN curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh -o install_nvm.sh
RUN mkdir -p $NVM_DIR && bash install_nvm.sh
RUN echo "source $NVM_DIR/nvm.sh" >>/etc/profile
RUN bash -ic "nvm install $NODE_DEFAULT_VERSION && nvm use $NODE_DEFAULT_VERSION"
RUN chmod -R ugo+w $NVM_DIR

Change NODE_DEFAULT_VERSION to what you'd like it to be. You can add to this to use all the features of nvm; you could install more than one version, and use nvm use <otherversion> in a post-start hook if you wanted to.

For more about how to use and install nvm, see the README.

For more about how to use ddev's add-on Dockerfile capability, see ddev docs on add-on Dockerfile

For details about Dockerfile syntax, see Docker's Dockerfile reference



来源:https://stackoverflow.com/questions/58415512/how-can-i-add-and-use-nvm-in-a-ddev-web-container

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!