docker build not setting up environment variables using ENV - docker

Trying to build a docker image with golang and react code. The environment variable JWT_SECRET_KEY is not being set.
# Build the Go API
FROM golang:latest AS builder
ADD . /app
WORKDIR /app/server
ENV JWT_SECRET_KEY=DefaultKey
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -a -o /main .
# Build the React application
FROM node:alpine AS node_builder
COPY --from=builder /app/client ./
RUN npm install
RUN npm run build
# Final stage build, this will be the container
# that we will deploy to production
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /main ./
COPY --from=node_builder /build ./web
RUN chmod +x ./main
EXPOSE 8080
CMD ./main
To build this i ran the command
docker build -t webapp .

If you want JWT_SECRET_KEY to be set in the production stage you need to move it to that stage. Or if you need it in both copy it. So change your docker file to
# Build the Go API
FROM golang:latest AS builder
ADD . /app
WORKDIR /app/server
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -a -o /main .
# Build the React application
FROM node:alpine AS node_builder
COPY --from=builder /app/client ./
RUN npm install
RUN npm run build
# Final stage build, this will be the container
# that we will deploy to production
FROM alpine:latest
RUN apk --no-cache add ca-certificates
ENV JWT_SECRET_KEY=DefaultKey
COPY --from=builder /main ./
COPY --from=node_builder /build ./web
RUN chmod +x ./main
EXPOSE 8080
CMD ./main

Related

Unable to use Arg values in commands in dockerfile

I am trying to allow passing build args to a dockerfile during build process but I cannot figure out how to get it to work.
This is my dockerfile
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
RUN apt-get update && apt-get install openssh-server -y \
&& echo "root:Docker!" | chpasswd
COPY sshd_config /etc/ssh/
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 2222
ARG node_build=production
ENV node_build_env=$node_build
ARG net_build=Release
ENV net_build_env=$net_build
FROM node:12.18.3 AS node-build
RUN echo $build_command_env
WORKDIR /root
COPY ["MyProject/package.json", "."]
COPY ["MyProject/package-lock.json", "."]
RUN npm i --ignore-scripts
COPY ["MyProject/angular.json", "."]
COPY ["MyProject/tsconfig.json", "."]
COPY ["MyProject/tslint.json", "."]
COPY ["MyProject/src", "./src"]
RUN npx ng build -c $node_build_env
FROM microsoft/dotnet:2.2-sdk AS build
RUN echo $net_build_env
WORKDIR /src
COPY ["MyProject/MyProject.csproj", "MyProject/"]
COPY ["MyProject.ServiceModel/MyProject.ServiceModel.csproj", "MyProject.ServiceModel/"]
COPY ["MyProject.ServiceInterface/MyProject.ServiceInterface.csproj", "MyProject.ServiceInterface/"]
COPY ["NuGet.config", "NuGet.config"]
RUN dotnet restore "MyProject/MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c $net_build_env -o /app
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c $net_build_env -o /app
FROM base AS final
WORKDIR /app
COPY --chown=www-data:www-data --from=publish /app .
COPY --chown=www-data:www-data --from=node-build /root/wwwroot ./wwwroot
ENTRYPOINT service ssh start && dotnet MyProject.dll
I have tried many different ways. I tried just using ARG and declaring after every FROM but that didn't work. This version I try copying ARG to ENV but that also didn't work.
How do I do this?
Issue was that variable had to be in double quote:
RUN dotnet build "MyProject.csproj" -c "$net_build_env" -o /app
You have to add --build-arg <arg>=<value> in your docker build command in order to use ARG arguments in your containerfile.
There's no need for ENV if you're only going to use the args in container build process

Docker Container with golang http.Get error "certificate signed by unknown authority"

I have a container with Golang that calls a https api. I'm using a scratch container and when I try to run I get a certificate signed by unknown authority
url := "https://restcountries.eu/rest/v2/name/" + params.Get("country")
response, err := http.Get(url)
My Dockerfile is like this:
FROM golang:1.15 AS builder
WORKDIR /GreetingAPI
COPY . /greeting
WORKDIR /greeting
ENV GO111MODULE=on
RUN CGO_ENABLED=0 GOOS=linux go build -o greeting
FROM scratch
COPY --from=builder /greeting .
CMD ["./greeting"]
I updated my Dockerfile using this answare. But when I try to build the container I get ERROR: "/ca-certificates.crt" not found: not found and failed to solve: rpc error: code = Unknown desc = failed to compute cache key: "/ca-certificates.crt" not found: not found
FROM golang:1.15 AS builder
WORKDIR /GreetingAPI
COPY . /greeting
WORKDIR /greeting
ENV GO111MODULE=on
RUN CGO_ENABLED=0 GOOS=linux go build -o greeting
FROM scratch
ADD ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /greeting .
CMD ["./greeting"]
I may have needed to be more clear in the linked answer, the copy in this first example was a single stage example where you had a certificate file to inject in your build context (directory that typically has your Dockerfile):
FROM scratch
ADD ca-certificates.crt /etc/ssl/certs/
ADD main /
CMD ["/main"]
You have a multi-stage build and can follow the multi-stage method in the second half of the linked answer. That installs the certificates in another stage from the distribution vendor and copies them into your scratch stage:
FROM golang:alpine as build
RUN apk --no-cache add ca-certificates
WORKDIR /go/src/app
COPY . .
RUN CGO_ENABLED=0 go-wrapper install -ldflags '-extldflags "-static"'
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /go/bin/app /app
ENTRYPOINT ["/app"]
However, that second example assumed Alpine as the base for the first stage, using apk. (It also assumed the certificates need to be installed in the base image, which turns out to not be the case in the current golang images.) For your example, it's based on Debian in the golang:1.15 image. For that, you'd normally need apt-get commands, but in this case the ca-certificates package is already installed, so you can just copy the results:
FROM golang:1.15 AS builder
COPY . /greeting
WORKDIR /greeting
ENV GO111MODULE=on
RUN CGO_ENABLED=0 GOOS=linux go build -o greeting
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /greeting /
CMD ["/greeting"]
Install ca cert in your builder stage and copy over to the final image. Something like:
FROM golang:1.15 AS builder
RUN apk update
RUN apk add -U --no-cache ca-certificates && update-ca-certificates
WORKDIR /GreetingAPI
COPY . /greeting
WORKDIR /greeting
ENV GO111MODULE=on
RUN CGO_ENABLED=0 GOOS=linux go build -o greeting
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /greeting .
CMD ["./greeting"]

Docker doesn't see main.go

I have some problems with Docker. My dockerfile doesn't see main.go.
I have that struct project
docker-compose.yml
go.mod
frontend-microservice
-cmd
-app
-main.go
-internal
-some folders
When I try start docker-compose It's give me that error.
ERROR: Service 'frontend-microservice' failed to build: The command '/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice .' returned a non-zero code: 1
By the way dockerfile give error related to go.mod
That my docker-compose
version: "3"
services:
frontend-microservice:
build:
context: ./frontend-microservice/
dockerfile: Dockerfile
ports:
- 80:80
That my dockerfile
# golang image where workspace (GOPATH) configured at /go.
FROM golang:alpine as builder
ADD . /go/src/frontend-microservice
WORKDIR /go/src/frontend-microservice
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /frontend-microservice ./frontend-microservice
RUN mkdir ./configs
COPY ./configs/config.json ./configs
EXPOSE 8080
ENTRYPOINT ["./frontend-microservice"]
Thank you in advance for any help
The file where the main() function is defined is located in cmd/app.
Instead of changing the current working directory into cmd/app, append cmd/app/main.go to the go build command.
Your Dockerfile would look like this:
# golang image where workspace (GOPATH) configured at /go.
FROM golang:alpine as builder
ADD . /go/src/frontend-microservice
WORKDIR /go/src/frontend-microservice
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice cmd/app/main.go
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /frontend-microservice ./frontend-microservice
RUN mkdir ./configs
COPY ./configs/config.json ./configs
EXPOSE 8080
ENTRYPOINT ["./frontend-microservice"]

use golang in docker container in multi-stage build

I want to use multi-stage build and I want that at the end I will have Golang inside the running container.
When I run the container and do go version I get error “unknown go”
# build stage
FROM golang:1.11.2-alpine3.8 AS builder-env
ENV CGO_ENABLED=0
ENV GOOS=linux
ADD https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep
RUN mkdir -p $GOPATH/src/github/mtp/myproj
WORKDIR $GOPATH/src/github/mtp/myproj
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure --vendor-only
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /myproj .
FROM alpine:latest
RUN mkdir /data
COPY --from=builder-env myproj ./
I can do something like this to make it work, but is it a good options ?
FROM alpine:latest
RUN mkdir /data
RUN update-ca-certificates && \
apk add go
COPY --from=builder-env myproj ./
You don't need the go executable to run a compiled program, just the resulting binary. If you add in the missing CMD line to your Dockerfile
CMD ["./myproj"]
I'd expect it would work fine.

Docker for golang application

i've golang application which I want to build docker image for it
the application folder called cloud-native-go and the dockerfile is under the root project
Any idea what is wrong here ?
FROM golang:alpine3.7
WORKDIR /go/src/app
COPY . .
RUN apk add --no-cache git
RUN go-wrapper download # "go get -d -v ./..."
RUN go-wrapper install # "go install -v ./..."
#final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /go/bin/app /app
ENTRYPOINT ./app
LABEL Name=cloud-native-go Version=0.0.1
EXPOSE 3000
The error is :
Step 5/12 : RUN go-wrapper download # "go get -d -v ./..."
---> Running in 70c2e00f332d
/bin/sh: go-wrapper: not found
i Build it with
docker build -t cloud-native-go:1.0.0 .
go-wrapper has been deprecated and removed from the images using go version 10 and above. See here.
If you are fine using go v1.9 you can use the following image: golang:1.9.6-alpine3.7.
So your Dockerfile will be:
FROM golang:1.9.6-alpine3.7
WORKDIR /go/src/app
COPY . .
RUN apk add --no-cache git
RUN go-wrapper download # "go get -d -v ./..."
RUN go-wrapper install # "go install -v ./..."
#final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /go/bin/app /app
ENTRYPOINT ./app
LABEL Name=cloud-native-go Version=0.0.1
EXPOSE 3000
FROM golang:alpine
# important!
ENV GO111MODULE=on
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
ENV GOFLAGS=-mod=vendor
ENV APP_USER app
ENV APP_HOME /go/src/microservices
RUN mkdir /nameApp
ADD . /nameApp
WORKDIR /nameApp
//compile your project
RUN go mod vendor
RUN go build
//open the port 8000
EXPOSE 8000
CMD [ "/nameApp/nameApp" ]

Resources