I’m trying to build a Docker container for my Go project using Docker Compose, but I’m running into an issue where Docker cannot find the auth-svc directory during the COPY step.
Here is my project structure:
/home/sohel/go/src/github.com/bizspace/
├── api-gateway
│ ├── deploy
│ │ └── Dockerfile
│ ├── ...
├── auth-svc
│ ├── deploy
│ │ └── Dockerfile
│ ├── ...
├── deploy
│ └── docker
│ ├── docker-compose.yml
│ └── postgres
│ └── init.sql
├── go.mod
├── go.sum
Dockerfile (auth-svc/deploy/Dockerfile):
# Build stage
FROM golang:1.22-alpine AS builder
WORKDIR /app
# Copy go.mod and go.sum from the project root
COPY go.mod go.sum ./
# Download all dependencies
RUN go mod download
# Copy the entire auth-svc directory
COPY auth-svc ./auth-svc
# Build the Go application
RUN CGO_ENABLED=0 GOOS=linux go build -o /bin/auth-svc ./auth-svc/cmd/main.go
# Final stage
FROM scratch
COPY --from=builder /bin/auth-svc /bin/auth-svc
ENTRYPOINT ["/bin/auth-svc"]
docker-compose.yml (deploy/docker/docker-compose.yml):
version: '3.8'
services:
auth-svc:
build:
context: ../..
dockerfile: auth-svc/deploy/Dockerfile
container_name: auth-svc
ports:
- "8081:8081" # Adjust ports as necessary
environment:
- ENV_VAR=example_value # Add any necessary environment variables
api-gateway:
build:
context: ../..
dockerfile: api-gateway/deploy/Dockerfile
container_name: api-gateway
ports:
- "8080:8080" # Adjust ports as necessary
depends_on:
- auth-svc
environment:
- ENV_VAR=example_value # Add any necessary environment variables
postgres:
image: postgres:latest
container_name: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
ports:
- "5432:5432"
volumes:
- ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
networks:
default:
driver: bridge
Error Message:
[+] Building 1.1s (9/11) docker:default
=> [auth-svc internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 495B 0.0s
=> [auth-svc internal] load metadata for docker.io/library/golang:1.22-alpine 1.0s
=> [auth-svc internal] load .dockerignore 0.0s
=> => transferring context: 777B 0.0s
=> [auth-svc builder 1/6] FROM docker.io/library/golang:1.22-alpine@sha256:8c9183f715b0b4eca05b8b3dbf59766aaedb41ec07477b132ee2891ac0110a07 0.0s
=> [auth-svc internal] load build context 0.0s
=> => transferring context: 54B 0.0s
=> CACHED [auth-svc builder 2/6] WORKDIR /app 0.0s
=> CACHED [auth-svc builder 3/6] COPY go.mod go.sum ./ 0.0s
=> CACHED [auth-svc builder 4/6] RUN go mod download 0.0s
=> ERROR [auth-svc builder 5/6] COPY auth-svc ./auth-svc 0.0s
------
> [auth-svc builder 5/6] COPY auth-svc ./auth-svc:
------
failed to solve: failed to compute cache key: failed to calculate checksum of ref b3ad4ddd-1331-4638-81e6-5cd14faf3a25::z649uairbj5bqcw0v4kauu8gl: "/auth-svc": not found
Steps Taken:
Navigated to /home/sohel/go/src/github.com/bizspace/deploy/docker.
Ran the command docker-compose up --build.
Questions:
Why is Docker unable to find the auth-svc directory during the build process?
Is there an issue with how the build context is being set in my docker-compose.yml
file?
How can I correctly set up the docker-compose.yml and Dockerfile to ensure the build succeeds?
Any help or insights would be greatly appreciated.
Thank you!