How to replace --from tag in Dockerfile to be able

2019-08-27 19:02发布

问题:

I have the following Dockerfile:

FROM docker.m.our-intra.net/microsoft/dotnet:2.1-sdk
WORKDIR /app
COPY . ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

FROM docker.m.our-intra.net/microsoft/dotnet:2.1.4-aspnetcore-runtime
WORKDIR /app
COPY --from=docker.m.our-intra.net/microsoft/dotnet:2.1-sdk /app/aspnetapp/MyProject.WebApi/out ./
ENTRYPOINT ["dotnet", "MyProject.WebApi.dll"]

The image is builded locally successfully.

I'm trying to configure a Jenkins pipeline and try to build the image from Jenkinsfile using the following step:

stage('Build') {
            steps {
                script {
                    echo 'Build...'
                    sh 'docker build -t mytag:v${BUILD_NUMBER} -f Dockerfile .'
                    echo 'Build Completed'
                }               
            }
        }

But In Jenkins console I get the error:

Unknown flag: from

How should I edit my Dockerfile, particularly --from tag in order to able to build the image locally and through Jenkins pipeline?

UPDATE: I've updated the Docker to 18.06.1-ce version. Still have the same error.

Jenkins ver. 2.89.4

Docker Pipeline plugin installed version 1.9.1

UPDATE 2: I added the docker version command to show me the Docker version:

stage('Build') {
            steps {
                script {
                    echo 'Build...'
                    sh 'docker version'
                    sh 'docker build -t fact:v${BUILD_NUMBER} -f Dockerfile .'
                    echo 'Build Completed'
                }               
            }
        }

And it outputs the following:

Client:
 Version:           18.06.1-ce
 API version:       1.24 (downgraded from 1.38)
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:25:03 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          1.12.6
  API version:      1.24 (minimum version )
  Go version:       go1.8.3
  Git commit:       3e8e77d/1.12.6
  Built:            Wed Dec 13 12:18:58 2017
  OS/Arch:          linux/amd64
  Experimental:     false

回答1:

I believe it's a docker engine version issue. Docker engine version needs to be 17.05 or later on your Jenkins machine to support --from or multi-stage builds.

From docs -

Multi-stage builds are a new feature requiring Docker 17.05 or higher on the daemon and client.

Ref - https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds