自建GitLab-CE服务器CI流水线Docker构建失败:unknown instruction错误
Hey, that error is a classic tell-tale sign—Docker's trying to parse a "Dockerfile" that's actually an HTML document (like a 404 error page or permission denied page) instead of valid Docker commands. Let's walk through the most common fixes:
1. Double-check your Dockerfile path in the CI script
First, look at the docker build command in your package stage. If your Dockerfile lives in the root of your repo, the command should be:
docker build -t your-image-name .
If it's in a subfolder or has a custom name, make sure you specify it explicitly:
docker build -t your-image-name -f ./src/main/docker/Dockerfile .
A typo in the path here can lead Docker to read the wrong file (or nothing at all, which might fall back to unexpected content).
2. Verify the actual content of your Dockerfile
Head straight to your GitLab repo and open the Dockerfile you're trying to use. The first line must be a valid Docker instruction—something like FROM openjdk:17-jdk-slim or FROM alpine:latest. If you see <DOCTYPE html> or any HTML tags at the top, you've got the wrong file (maybe you accidentally uploaded an HTML file instead of your Dockerfile, or overwrote it by mistake).
3. Make sure your CI job can access the Dockerfile
- Ensure your GitLab runner is correctly cloning the full repo into the job's working directory. If the runner has partial checkout issues, it might not pull the Dockerfile at all.
- If your build stage uses a different image, confirm you're not accidentally wiping the repo contents between stages. You don't need to pass the Dockerfile via artifacts (since it's part of the repo), but if your build stage produces a JAR that the Dockerfile uses, make sure you're passing that with
artifactsand linking the stages withdependencies.
Example fixed package stage snippet
Here's how a solid package stage might look, assuming your Dockerfile is in the root and needs the JAR from the build stage:
stages: - build - package build: image: maven:3.8.6-openjdk-17 stage: build script: - mvn clean package -DskipTests artifacts: paths: - target/*.jar # Pass the built JAR to the next stage package: image: docker:latest services: - docker:dind stage: package script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA . - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA dependencies: - build # Ensure we get the JAR from the build stage
That should resolve the issue as long as your Dockerfile is present, correctly named, and has valid Docker instructions at the top.
内容的提问来源于stack exchange,提问作者Daniel Pomrehn

