Home > front end >  Dockerfile Build Doesn't Work But Works with Compose
Dockerfile Build Doesn't Work But Works with Compose

Time:11-10

I have a Dockerfile that won't build, but will work with docker-compose, I need it to work though with docker build command.

###############
# CACHE IMAGE #
###############
ARG GO_IMAGE=golang:1.17.3-alpine3.14
ARG BASE_IMAGE=alpine:3.14.2

FROM ${GO_IMAGE} AS cache
# Add the keys
ARG GITHUB_ID
ENV GITHUB_ID=$GITHUB_ID
ARG GITHUB_TOKEN
ENV GITHUB_TOKEN=$GITHUB_TOKEN

# Install Git
RUN apk add git

# TODO: ENCRYPT THE GITHUB_ID AND GITHUB_TOKEN
# Make Git Configuration
RUN git config \
    --global \
    url."https://${GITHUB_ID}:${GITHUB_TOKEN}@github.com/".insteadOf \
    "https://github.com/"

WORKDIR /bin
COPY go.mod go.sum bin/
RUN go mod download

##############
# BASE IMAGE #
##############
FROM cache AS dataeng_github_metrics
COPY . /bin
WORKDIR /bin

# Setup Git Terminal Prompt & Go Build
RUN go build .

###############
# FINAL IMAGE #
###############
FROM ${BASE_IMAGE}
COPY --from=dataeng_github_metrics /bin/dataeng_github_metrics bin/
ENTRYPOINT [ "bin/dataeng_github_metrics" ]

The directory looks like this:

rics   Docker-Publish-Workflows-To-GHCR ●  tree .                       1 ↵  8955  20:37:41
.
├── Dockerfile
├── Makefile
├── README.md
├── dataeng_github_metrics
├── go.mod
├── go.sum
├── infra
│   ├── README.md
│   ├── k8s
│   │   ├── README.md
│   │   ├── configmaps
│   │   │   ├── README.md
│   │   │   └── teams-payload-configmap.yaml
│   │   ├── cronworkflow
│   │   │   ├── README.md
│   │   │   └── argo_cron_workflow.yaml
│   │   ├── deployment
│   │   │   ├── README.md
│   │   │   ├── git-hub-contributions-deployment.yaml
│   │   │   └── postgres-deployment.yaml
│   │   └── volumeclaims
│   │       ├── README.md
│   │       ├── git-hub-contributions-claim0-persistentvolumeclaim.yaml
│   │       ├── git-hub-contributions-claim1-persistentvolumeclaim.yaml
│   │       └── git-hub-contributions-claim2-persistentvolumeclaim.yaml
│   ├── payloads
│   │   ├── README.md
│   │   ├── dataeng_github_metrics.csv
│   │   └── teams.yaml
│   └── terraform
│       ├── README.md
│       └── manifests
│           └── README.md
├── local
│   ├── README.md
│   ├── dependencies
│   │   └── wait-for-postgres.sh
│   ├── docker-compose.yaml
│   └── images
│       ├── ER_Diagram.png
│       ├── print_execution.png
│       └── print_query.png
└── main.go

What's weird to me is it fails at the COPY step for go.mod and go.sum, and I haven't a clue why it's not copying over the files:

Command to build Dockerfile in working directory:

docker build - < Dockerfile
[ ] Building 0.7s (12/17)                                                                         
 => [internal] load build definition from Dockerfile                                         0.0s
 => => transferring dockerfile: 928B                                                         0.0s
 => [internal] load .dockerignore                                                            0.0s
 => => transferring context: 2B                                                              0.0s
 => [internal] load metadata for docker.io/library/alpine:3.14.2                             0.4s
 => [internal] load metadata for docker.io/library/golang:1.17.3-alpine3.14                  0.0s
 => [cache 1/7] FROM docker.io/library/golang:1.17.3-alpine3.14                              0.0s
 => [internal] load build context                                                            0.0s
 => => transferring context: 2B                                                              0.0s
 => [stage-2 1/2] FROM docker.io/library/alpine:3.14.2@sha256:e1c082e3d3c45cccac829840a2594  0.0s
 => CACHED [cache 2/7] RUN apk add git                                                       0.0s
 => CACHED [cache 3/7] RUN git config     --global     url."https://:@github.com/".insteadO  0.0s
 => CACHED [cache 4/7] WORKDIR /bin                                                          0.0s
 => [cache 5/7] RUN pwd                                                                      0.2s
 => ERROR [cache 6/7] COPY go.mod go.sum ./  

Why is it not letting me copy a file into my WORKDIR with docker build, but when I use docker-compose it works just fine.

CodePudding user response:

Try running the Docker build like so:

docker build .

The build with - doesn't work as expected because no context is given. See Docker build docs:

This will read a Dockerfile from STDIN without context. Due to the lack of a context, no contents of any local directory will be sent to the Docker daemon. Since there is no context, a Dockerfile ADD only works if it refers to a remote URL.

  • Related