在 dockers 中安装 libmysqlclient-dev 和 npm

问题描述

由于某种原因,我在尝试将 libMysqLclient-dev 包与 npm 一起安装时出错,在安装 libMysqLclient-dev 时它删除npm

Step 10/26 : RUN apt-get install -y libMysqLclient-dev
 ---> Running in beae8aee9cd4
Reading package lists...
Building dependency tree...
Reading state information...
The following packages were automatically installed and are no longer required:
  gyp javascript-common libjs-async libjs-inherits libjs-jquery
  libjs-node-uuid libjs-underscore libuv1-dev node-abbrev node-ansi
  node-ansi-color-table node-archy node-async node-balanced-match
  node-block-stream node-brace-expansion node-builtin-modules
  node-combined-stream node-concat-map node-cookie-jar node-delayed-stream
  node-forever-agent node-form-data node-fs.realpath node-fstream
  node-fstream-ignore node-github-url-from-git node-glob node-graceful-fs
  node-hosted-git-info node-inflight node-inherits node-ini
  node-is-builtin-module node-isexe node-json-stringify-safe node-lockfile
  node-lru-cache node-mime node-minimatch node-mkdirp node-mute-stream
  node-node-uuid node-nopt node-normalize-package-data node-npmlog node-once
  node-osenv node-path-is-absolute node-pseudomap node-qs node-read
  node-read-package-json node-request node-retry node-rimraf node-semver
  node-sha node-slide node-spdx-correct node-spdx-expression-parse
  node-spdx-license-ids node-tar node-tunnel-agent node-underscore
  node-validate-npm-package-license node-which node-wrappy node-yallist
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libssl-dev
Suggested packages:
  libssl-doc
The following packages will be REMOVED:
  libssl1.0-dev node-gyp nodejs-dev npm
The following NEW packages will be installed:
  libMysqLclient-dev libssl-dev
0 upgraded,2 newly installed,4 to remove and 133 not upgraded.
Need to get 2,583 kB of archives.
After this operation,5,175 kB disk space will be freed.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.9 [1,566 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libMysqLclient-dev amd64 5.7.34-0ubuntu0.18.04.1 [1,016 kB]
dpkg-preconfigure: unable to re-open stdin: 
Fetched 2,583 kB in 2s (1,412 kB/s)
(Reading database ... 25174 files and directories currently installed.)
Removing npm (3.5.2-0ubuntu4) ...
Removing node-gyp (3.6.2-1ubuntu1) ...
Removing nodejs-dev (8.10.0~dfsg-2ubuntu0.4) ...
Removing libssl1.0-dev:amd64 (1.0.2n-1ubuntu5.6) ...

这给了我以下错误

Step 14/26 : RUN npm install -g npm@4.1.1
 ---> Running in 18f70438b2ae
/bin/sh: 1: npm: not found
ERROR: Service 'webapp' Failed to build: The command '/bin/sh -c npm install -g npm@4.1.1' returned a non-zero code: 127

这是我的 Dockerfile:

RUN apt-get update
RUN apt-get install -y tzdata
RUN apt-get install -y libfontconfig1
RUN apt-get install -y libxrender1
RUN apt-get install -y nodejs
RUN apt-get install -y npm
RUN apt-get install -y yarn
RUN apt-get install -y MysqL-client
RUN apt-get install -y libMysqLclient-dev
RUN apt-get install -y shared-mime-info
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN npm install -g npm@4.1.1
RUN npm install -g yarn

我正在开发 ruby​​ on rails,我需要 libMysqLclient-dev gem 的 MysqL2

我该如何解决这个问题?

解决方法

您需要阅读 Docker 文档中的 Dockerfile best practices for the RUN instruction。 Dockerfile 中的每一行都是一个镜像层,执行 RUN 指令后的状态并不总是保留在下一层。

因此,当您运行 apt-get install -y npm 时,npm install -g ... 不会影响构建,因此您收到错误:npm command not found

请阅读指南并尝试改用这条 RUN 指令。

RUN apt-get update \
    && apt-get install -y tzdata \
    && apt-get install -y libfontconfig1 \
    && apt-get install -y libxrender1 \
    && apt-get install -y nodejs \
    && apt-get install -y npm \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y libmysqlclient-dev \
    && apt-get install -y shared-mime-info \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && npm install -g npm@4.1.1 \
    npm install -g yarn