无法在最终Docker映像中显示npm已安装的应用程序

时间:2019-05-14 21:20:20

标签: docker npm

我已将Vega工具的安装添加到docker-asciidoctor Dockerfile中,并且在运行bats测试时它们就存在,但是在运行映像时它们不再存在。

我是Docker的新手。

我还尝试了将dir node_modules添加到路径的一些变体,但是没有任何效果。

在所有情况下,Vega工具的安装目录都根本不在映像中。

我正在添加Vega工具,如下所示:

...
  && npm install --build-from-source -g vega-cli vega vega-lite vega-embed \
  && echo `which vl2vg` \
...

提供以下输出:

...
/usr/bin/vl2png -> /usr/lib/node_modules/vega-lite/bin/vl2png
/usr/bin/vl2svg -> /usr/lib/node_modules/vega-lite/bin/vl2svg
/usr/bin/vl2vg -> /usr/lib/node_modules/vega-lite/bin/vl2vg
/usr/bin/vg2pdf -> /usr/lib/node_modules/vega-cli/bin/vg2pdf
/usr/bin/vg2png -> /usr/lib/node_modules/vega-cli/bin/vg2png
/usr/bin/vg2svg -> /usr/lib/node_modules/vega-cli/bin/vg2svg
...
/usr/bin/vl2vg

就像人们期望的那样。

其中一种工具的测试如下:

@test "vl2vg is installed and in the path" {
  docker run -t --rm "${DOCKER_IMAGE_NAME_TO_TEST}" which vl2vg
}

通过。

当执行以下操作时,我希望Vega工具在图像中可用:

docker run -it --entrypoint /bin/sh asciidoctor/docker-asciidoctor
/documents # which vl2vg
which: no vl2vg in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin)

鉴于图像生成的输出,我本来希望看到/usr/bin/vl2vg/usr/lib/node_modules不存在。

为完整起见,这里是Docker文件和所采取的步骤:

FROM alpine:3.9

LABEL MAINTAINERS="Guillaume Scheibel <guillaume.scheibel@gmail.com>, Damien DUPORTAL <damien.duportal@gmail.com>"

ARG asciidoctor_version=2.0.9
ARG asciidoctor_confluence_version=0.0.2
ARG asciidoctor_pdf_version=1.5.0.alpha.17
ARG asciidoctor_diagram_version=1.5.16
ARG asciidoctor_epub3_version=1.5.0.alpha.9
ARG asciidoctor_mathematical_version=0.3.0
ARG asciidoctor_revealjs_version=2.0.0

ENV ASCIIDOCTOR_VERSION=${asciidoctor_version} \
  ASCIIDOCTOR_CONFLUENCE_VERSION=${asciidoctor_confluence_version} \
  ASCIIDOCTOR_PDF_VERSION=${asciidoctor_pdf_version} \
  ASCIIDOCTOR_DIAGRAM_VERSION=${asciidoctor_diagram_version} \
  ASCIIDOCTOR_EPUB3_VERSION=${asciidoctor_epub3_version} \
  ASCIIDOCTOR_MATHEMATICAL_VERSION=${asciidoctor_mathematical_version} \
  ASCIIDOCTOR_REVEALJS_VERSION=${asciidoctor_revealjs_version}

# Installing package required for the runtime of
# any of the asciidoctor-* functionnalities
RUN apk add --no-cache \
    bash \
    curl \
    ca-certificates \
    findutils \
    font-bakoma-ttf \
    graphviz \
    inotify-tools \
    make \
    openjdk8-jre \
    py2-pillow \
    py-setuptools \
    python2 \
    ruby \
    ruby-mathematical \
    ttf-liberation \
    unzip \
    which

RUN addgroup --system appgroup && adduser -S appuser -G appgroup

WORKDIR /data/



# Installing Ruby Gems needed in the image
# including asciidoctor itself
RUN apk add --no-cache --virtual .rubymakedepends \
    build-base \
    libxml2-dev \
    ruby-dev \
  && gem install --no-document \
    "asciidoctor:${ASCIIDOCTOR_VERSION}" \
    "asciidoctor-confluence:${ASCIIDOCTOR_CONFLUENCE_VERSION}" \
    "asciidoctor-diagram:${ASCIIDOCTOR_DIAGRAM_VERSION}" \
    "asciidoctor-epub3:${ASCIIDOCTOR_EPUB3_VERSION}" \
    "asciidoctor-mathematical:${ASCIIDOCTOR_MATHEMATICAL_VERSION}" \
    asciimath \
    "asciidoctor-pdf:${ASCIIDOCTOR_PDF_VERSION}" \
    "asciidoctor-revealjs:${ASCIIDOCTOR_REVEALJS_VERSION}" \
    coderay \
    epubcheck:3.0.1 \
    haml \
    kindlegen:3.0.3 \
    pygments.rb \
    rake \
    rouge \
    slim \
    thread_safe \
    tilt \
  && apk add --update npm \
#  && npm -g config set user root \
  && apk --no-cache --virtual .canvas-build-deps add \
        build-base \
        cairo-dev \
        jpeg-dev \
        pango-dev \
        giflib-dev \
        pixman-dev \
        pangomm-dev \
        libjpeg-turbo-dev \
        freetype-dev \
    && apk --no-cache add \
        pixman \
        cairo \
        pango \
        giflib \
#  && npm -g config set user root \
  && npm config set user 0 \
  && npm config set unsafe-perm true \
  && npm install --build-from-source -g vega-cli vega vega-lite vega-embed \
  && echo `which vl2vg` \
#    && apk del .canvas-build-deps \
  && apk del -r --no-cache .rubymakedepends

ENV PATH /data/node_modules/.bin:$PATH
ENV NODE_PATH /data/node_modules/

# Installing Python dependencies for additional
# functionnalities as diagrams or syntax highligthing
RUN apk add --no-cache --virtual .pythonmakedepends \
    build-base \
    python2-dev \
    py2-pip \
  && pip install --upgrade pip \
  && pip install --no-cache-dir \
    actdiag \
    'blockdiag[pdf]' \
    nwdiag \
    Pygments \
    seqdiag \
  && apk del -r --no-cache .pythonmakedepends

USER appuser

WORKDIR /documents
VOLUME /documents

CMD ["/bin/bash"]

build命令为(从Makefile提起):

DOCKER_IMAGE_NAME ?= docker-asciidoctor
DOCKERHUB_USERNAME ?= asciidoctor
DOCKER_IMAGE_TEST_TAG ?= $(shell git rev-parse --short HEAD)
build:
    docker build \
        -t $(DOCKER_IMAGE_NAME_TO_TEST) \
        -f Dockerfile \
        $(CURDIR)/

测试与:

test:
    bats $(CURDIR)/tests/*.bats

其中一项测试是上述测试。

2 个答案:

答案 0 :(得分:0)

造成这种情况的根本原因是,由于未标记图像,所以我没有使用刚刚构建的图像。

docker tag asciidoctor/docker-asciidoctor:442d4d0 asciidoctor/docker-asciidoctor:latest

然后一切正常。

答案 1 :(得分:0)

谢谢! -非常有用,尤其是有关npm install -g canvas和使用npm - g config set user root的vega-cli的提示。

我从另一个基本图像开始构建:mhart / alpine-node。这是对我有用的Dockerfile。

FROM mhart/alpine-node:12.16.2

WORKDIR /app

COPY *.jar .

RUN apk add --no-cache \
    python2 \
    build-base \
    g++ \
    cairo-dev \
    jpeg-dev \
    pango-dev \
    bash \
    imagemagick

RUN npm -g config set user root \
 && npm install -g canvas \
 && npm install -g vega vega-lite vega-cli

# USER root
# RUN apk update
# RUN apk fetch openjdk8
# RUN apk add openjdk8

每行注释掉的四行也用于安装Java。