I'm able to run a docker image of a web app when using golang:1.13 base, but not when using scratch. The working Dockerfile is:
FROM golang:1.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o server
FROM golang:1.13
COPY --from=builder /app/server /app/server
COPY --from=builder /app/credentials/service-account.json /app/credentials/service-account.json
ENTRYPOINT ["/app/server"]
But when I change the final image base to scratch (line 6) like this:
FROM golang:1.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o server
FROM scratch # <-- CHANGED
COPY --from=builder /app/server /app/server
COPY --from=builder /app/credentials/service-account.json /app/credentials/service-account.json
ENTRYPOINT ["/app/server"]
I get a standard_init_linux.go:211: exec user process caused "no such file or directory" error.
To build the docker image, I use docker build -t myimage ., and to run the image, I use docker run --rm -p 8080:8080 myimage:latest.
The app is a Go based web API that uses Gin framework, and GCP Service Account to access GCP services (the JSON file that I copy on build.)
Provided you are not using CGO (as #jakub mentioned), try disabling CGO in your build.
So change this line in your Dockerfile:
#RUN go build -o server
RUN CGO_ENABLED=0 go build -o server
Related
When I use multi stage in Docker, it cannot find the container config file, but when I use only the first stage, the container works fine. I need this as I'm going to reduce the size for the deployment phase, but I couldn't find a solution. I have no problem reading the config file in my local.
Here is my docker file
FROM golang:1.18.3-alpine AS builder
WORKDIR /app/policy-manangement-service
ENV GO111MODULE=on
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /policy-manangement-service ./cmd/policy-management
FROM alpine:latest
COPY --from=builder /policy-manangement-service ./
ENTRYPOINT ["./policy-manangement-service"]
and my error:
{"app-name":"policy-management-service","error":"Config File \"local\" Not Found in \"[/configs]\"","level":"fatal","message":"Error while reading configs","time":"2022-07-12T14:27:58.486529597Z"}
my project structure:
cmd
- policy-manangement
-- main
configs
- local.yaml
internal
how to compile golang project with docker container? I want to compile and retrieve the project build, using docker cli
sample docker command
docker run -v $(pwd)/:/app -w /app -v $SSH_AUTH_SOCK:/tmp/ssh_auth.sock -e SSH_AUTH_SOCK=/tmp/ssh_auth.sock --name golanguser golang:1.17 sh -c "export GOPRIVATE=https://user:pass#bitbucket.org/repo/ && go build -o main bitbucket.org/repo/source"
Building a go project is usually done in a multi-stage build
# syntax=docker/dockerfile:1
##
## Build
##
FROM golang:1.16-buster AS build
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY *.go ./
RUN go build -o /docker-gs-ping
##
## Deploy
##
FROM gcr.io/distroless/base-debian10
WORKDIR /
COPY --from=build /docker-gs-ping /docker-gs-ping
EXPOSE 8080
USER nonroot:nonroot
ENTRYPOINT ["/docker-gs-ping"]
With:
docker build -t docker-gs-ping:multistage -f Dockerfile.multistage .
That way, you can deploy the built application in the image of your choice, resulting in a markedly smaller image size to run.
My projects need to access private go modules and to access to modules it needs a GOPROXY.
So I've create an image from base image golang alpine
PROXY IMAGE:
FROM golang:1.16.4-alpine3.13 AS builder
ARG GITLAB_LOGIN
ARG GITLAB_TOKEN
modules.
WORKDIR /app
ENV GO111MODULE="auto"
ENV GONOSUMDB=*.someting.text
ENV GOPROXY=https://proxy.golang.org,direct
ENV GOPRIVATE="gitlab.something.text"
#ARG GOPROXY=http://localhost:41732,https://proxy.golang.org,direct
RUN apk add --no-cache git
RUN echo "machine gitlab.something.text login ${GITLAB_LOGIN} password ${GITLAB_TOKEN}" > ~/.netrc
COPY ["go.mod", "go.sum", "./"]
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build \
-installsuffix 'static' \
-o /app/proxy .
FROM scratch AS final
COPY --from=builder /app /app
CMD [ "/app/proxy"]
And the alpine image has a shell so I can execute commands with RUN.
But then I want to use my proxy image as a base image.
IMAGE THAT USES THE PROXY IMAGE:
FROM proxy:latest
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -o bin/app .
CMD [ "/app" ]
And then the image that is using the proxy image cannot access the shell.
So my question is, is there a way to forward/pass the shell from the base alpine image through the proxy image so the images that use the proxy image can access the shell that is provided by the alpine image.
Cause the error I get in the image that is using the proxy image as the base is
CI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown
but of course this error does not happen in the proxy image cause it's base image is alpine.
The final stage of the proxy image is built FROM scratch. As you note this contains absolutely nothing, not even a shell, so if you want to use it as a base image, you need to change this to something else:
FROM golang:1.16.4-alpine3.13 AS builder
...
FROM alpine:3.13 # AS final
COPY --from=builder /app/proxy /usr/local/bin/proxy
CMD ["proxy"]
However, your Go build sequence produces a static binary; if it can run successfully as the only thing in a FROM scratch image then it can run successfully in any Linux environment. You can COPY --from an image as well as a build stage. It might be easier to not use the proxy as a base image, but instead copy it into other images where you need it:
FROM golang # not the proxy
# Get the proxy binary from the other image
COPY --from=proxy /app/proxy /usr/local/bin
# Build the Go application as above
COPY go.mod go.sum ./
...
Can anyone please help me to create a docker file for blazor webassembly ?
I am trying to creating docker file for blazor webassembly.
I have checked many docs and I have tried with many docker files. Every time my docker image build successful but when I run that image at that time I am getting error like below
"It was not possible to find any installed .NET Core SDKs
Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from: https://aka.ms/dotnet-download"
I am using below code
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "BlazorDemo.dll"]
This is my build command and it's working fine
docker build --tag blazordemo .
Below run command I have tried and here I am getting error
docker run --publish 8001:80 --detach --name blazortestapp blazordemo
docker run -p 8080:80 blazordemo
Note - Blazor WebAssembly target framework is .NET Standard 2.1
We have hosted .net core api in linux server using docker. so issue is reading application logs from Logs folder which we are not getting in container. our docker code is :
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 50147
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Services/Services.csproj", "Services/"]
COPY ["Portal.Common/Portal.Common.csproj", "Portal.Common/"]
COPY ["Portal.Domains/Portal.Domains.csproj", "Portal.Domains/"]
COPY ["Portal.Business/Portal.Business.csproj", "Portal.Business/"]
COPY ["Portal.DataAccess/Portal.DataAccess.csproj", "Portal.DataAccess/"]
RUN dotnet restore "PortalServices/PortalServices.csproj"
COPY . .
WORKDIR "/src/PortalServices"
RUN dotnet build "PortalServices.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "PortalServices.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PortalServices.dll"]
so inside app path only dll files are there. So how can we read logs from folder???
We implemented serilog to solution and saving to solutionpath/Logs/log.txt
In local it is storing in this path.
Please help!!
Change folder permission to 777 where you want to store logs.
chmod 777 .
docker run -d -t -i -p 80:80 -v /usr/Logs:/app/Logs --name
testContainer testImage