I setup a simple server with golang:
package main
import (
"golang-server/database"
"golang-server/helper"
"log"
"net/http"
)
func main() {
database.Connect()
port := helper.GetPort()
SetupRoutes()
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}
Here is what my folder structure look like:
Here is what my dockerfile look like:
FROM golang:1.18
WORKDIR $GOPATH/src
COPY . .
RUN go mod download
RUN go build -o /golang-server
EXPOSE 8080
CMD ["golang-server"]
I am running these docker commands in the main directory:
docker build . -t golang-server
docker run --network=golang-server --name=golang-server golang-server
However I am getting this error when I run:
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "golang-server": executable file not found in $PATH: unknown.
What should I set the dockerfile so I can successfully deploy to docker?
The problem is related with your last line in Dockerfile CMD ["golang-server"]. When you put it there, the system (inside of the container) are trying to find an executable file inside of your $PATH variable called golang-server.
To solve this issue, you can just edit your last line from your Dockerfile to CMD ["/golang-server"], once you are building your application in / (RUN go build -o /golang-server). The final Dockerfile should be something like:
FROM golang:1.18
WORKDIR $GOPATH/src
COPY . .
RUN go mod download
RUN go build -o /golang-server
EXPOSE 8080
CMD ["/golang-server"]
Related
I'm kinda stuck exploring Docker features in order to create simple container with some Go utilities installed. I need to create image that has gosec and govulncheck utilities installed so I can run them on code in container. My petty attempt produced the following:
# syntax=docker/dockerfile:1
FROM golang:1.19-alpine
WORKDIR /app
ENV GO111MODULE=on
# copying my code to check
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY *.go ./
RUN go build -o /docker-gs-ping
RUN apk add --no-cache git
RUN go install github.com/securego/gosec/v2/cmd/gosec#latest
RUN go install golang.org/x/vuln/cmd/govulncheck#latest
EXPOSE 8080
CMD [ "gosec ./..." ]
Running the container results in error:
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "gosec ./...": stat gosec ./...: no such file or directory: unknown.
ERRO[0000] error waiting for container: context canceled
It looks like I need to specify paths to installed utilities, but I couldn't make it work
This isn't a path issue; the problem is the syntax you've used in the CMD statement in your Dockerfile. You're using the JSON-format of the CMD statement; the first argument in the JSON list is the name of the command to run. You've asked Docker to run a command named gosec ./..., which of course doesn't exist.
You need to split that into multiple list items:
CMD [ "gosec", "./..." ]
Alternatively, you can use the shell form of the CMD directive:
CMD gosec ./...
Either of those will run gosec when you start the container.
I am trying to run a simple docker image with a index.js file in my /build folder with a simple console.log('Hello world')
My docker file is:
FROM node:alpine
WORKDIR /test/build
COPY ./ ./
CMD node index.js
I create the image in my directory with:
docker build -t hello-docker .
Then I run:
docker run hello-docker
But I keep on getting the error:
cannot find module /test/build/index.js
How can I solve this?
Capturing my comment as an answer for posterity - since the docker build command will be run from the application's root, the index file will end up under the build directory. I.e., the application should be run using:
CMD node ./build/index.js
I'm trying to build an image of a .netcore v2.2 WebAPI app from docker file. But I'm getting a "no such file or directory" error while I try to build the image using following command:
docker build -t helloworld .
I have both microsoft/dotnet "latest" and "2.2-aspnetcore-runtime" base images installed in my machine.
Here's my docker file
FROM microsoft/dotnet
RUN mkdir /app
COPY ./bin/Release/netcoreapp2.2 /app
WORKDIR /app
ENTRYPOINT ["dotnet", "/app/HelloWorld.dll"]
# ASP.NET Core: Kestrel should listen on all IPs
ENV ASPNETCORE_URLS="http://0.0.0.0:5000"
Here's the folder structure of my solution (I tried the command inside the src):
Here's the output directory
Here's the full error details from the docker:
COPY failed: stat /var/lib/docker/tmp/docker-builder896068669/bin/Release/netcoreapp2.2: no such file or directory
you may change ./bin/Release/netcoreapp2.2 to ./bin/release/netcoreapp2.2
folder names are case sensitive
It seems the file ./bin/Release/netcoreapp2.2/publish does not exists on the machine on which you are running docker build
When you run docker build -t helloworld . it will copy all the file in . (i.e. the current directory from which you run the command) to the build context which will then be used when running COPY instructions, so you need to make sure you are running your command from the proper directory where files will be available to COPY
You can also run the same command from another directory by specifying the path to be used as build context such as docker build -t helloworld /path/to/my/dir
I'm trying to create Go web server into small Docker images. Ideally the clean image contains only the Go application itself (and maybe supporting web components, but not the Go-building environment).
Here is my Dockerfile:
# golang:latest as build-env
FROM golang:latest AS build-env
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN cd /app && GO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o myapp .
# go build -o myapp
FROM scratch
COPY --from=build-env /app/myapp /app/images /
EXPOSE 8080
ENTRYPOINT /myapp
It uses the Docker Builder Pattern and scratch image, which is a special docker image that's empty.
It builds OK, but when I run it, I'm getting:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown.
UPDATE:
So the ENTRYPOINT need to be changed to the exec form:
ENTRYPOINT ["/myapp"]
Having done that, I'm getting a new error:
standard_init_linux.go:207: exec user process caused "no such file or directory"
Having use a small footprint Linux image as the base (i.e. Alpine Linux) instead of scratch wouldn't help either:
$ docker run -it -p 8080:8080 go-web-docker-small
standard_init_linux.go:207: exec user process caused "no such file or directory"
$ docker run -it -p 8080:8080 go-web-docker-small /bin/sh -i
standard_init_linux.go:207: exec user process caused "no such file or directory"
How to fix it? Thx!
The last line of your Dockerfile is
ENTRYPOINT /myapp
There are two forms of the ENTRYPOINT (and CMD and RUN) instructions. An "exec form" looks like a JSON list, and provides an uninterpreted list of arguments to run as the main container process. A "shell form" does not look like a JSON list, and is implicitly wrapped in /bin/sh -c '...'.
Your ENTRYPOINT uses the shell form, and a FROM scratch image doesn't have a shell, producing the error you get. You can change this to the exec form
ENTRYPOINT ["/myapp"]
While building Dockerfile provided by you, I am getting following error:
COPY failed: stat /var/lib/docker/overlay2/cc1f8144192760ce7bf9cda7a7dfd0af16065901594c38609c813ea103cfd8d7/merged/app/images: no such file or directory
Fixed copy command and few others and image is building with following in Dockerfile
# golang:latest as build-env
FROM golang:latest AS build-env
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN cd /app && GO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o myapp .
# go build -o myapp
FROM scratch
COPY --from=build-env /app/myapp .
EXPOSE 8080
ENTRYPOINT ["./myapp"]
Am new to Docker technologies, I have build an executable file in Visual Studio 2015 on Windows 10 machine.
Am looking to dockerize this executable file. I have build an Image and Container but the exe file is not being execute.
Here is my work so far:
Docker file:
FROM microsoft/aspnetcore:1.1
COPY . /app
WORKDIR /app
ENTRYPOINT ["dotnet","Controller.exe"]
CMD ["Controller.exe","RUN"]
Command used to create Image:
docker build -t imagename:version
Command used to create controller:
docker run -it --name Controller_name Image_ID run
Am trying to start the controller, but the controller is not starting
I have performed docker logs and here is the information in the log file.
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\app'.