问题
I have the following Dockerfile with content:
FROM ubuntu:bionic AS os
RUN apt-get update
RUN apt-get install -y git
RUN git --version
FROM node:13.10.1-buster-slim
FROM python:3.7.7-slim-stretch as test
RUN pip install --user pipenv
RUN git --version
RUN git clone git@gitlab.com:silentdata/cdtc-identity-service.git
WORKDIR cdtc-identity-service
RUN pipenv install
CMD python service_test.py
Building the image, I've got the following output:
Sending build context to Docker daemon 43.59MB
Step 1/12 : FROM ubuntu:bionic AS os
---> 72300a873c2c
Step 2/12 : RUN apt-get update
---> Using cache
---> 42013f860b31
Step 3/12 : RUN apt-get install -y git
---> Using cache
---> 8f27d95fcb6e
Step 4/12 : RUN git --version
---> Using cache
---> ae49a9465233
Step 5/12 : FROM node:13.10.1-buster-slim
---> 500c5a190476
Step 6/12 : FROM python:3.7.7-slim-stretch as test
---> c9ec5ac0f580
Step 7/12 : RUN pip install --user pipenv
---> Using cache
---> 3a9358e72deb
Step 8/12 : RUN git --version
---> Running in 545659570a84
/bin/sh: 1: git: not found
The command '/bin/sh -c git --version' returned a non-zero code: 127
Why the git command could not be found at the second time?
回答1:
Multi-stage builds do not merge multiple images together. They allow you to build multiple docker images, and give you a useful syntax to copy artifacts between those images. Merging images would be a non-trivial task (some commands modify files rather than create them, e.g. the package management DB, so even two compatible images would result in issues for the end user).
For your use case, you probably want to pick the most appropriate base image and install your tools, code, compiled app there. Once you've gotten that to work, then adding a new stage for the minimal release can be added on.
For more on multi-stage builds, see: https://docs.docker.com/develop/develop-images/multistage-build/
来源:https://stackoverflow.com/questions/60713384/why-git-version-statement-does-not-get-recognize