Unable to run docker image for golang in windows - bash

I have created an image in windows 10 git bash. After so many turn around's I finally able to create an image in windows using docker.
But the problem is that when I try to run the container it shows me an error. I have tried everything. My DOCKERFILE is:
# Start from a Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang:latest AS builder
# install golang dependency management tool
RUN go get -u github.com/golang/dep/cmd/dep
# RUN go get -u -v github.com/go-swagger/go-swagger
# RUN CHMOD 777 -R .
# COPY . $GOPATH/src/app
RUN mkdir $GOPATH/src/project
# RUN mkdir $GOPATH/src/app/swagger
# WORKDIR /go/src/app
WORKDIR $GOPATH/src/project
# Add main file to /go/src/app
ADD . $GOPATH/src/project
ADD ./swagger/* $GOPATH/src/app/swagger/
# Copy Gopkg files to install dependencies
COPY ./Gopkg.toml /go/src/project
COPY ./Gopkg.lock /go/src/project
# COPY ./swagger/swagger.yaml /go/src/app
RUN dep ensure -update -v
RUN go install -v ./...
CMD ["project"]
The image is created. But I am unable to run a container from the image it always shows and error:
C:/Program Files/Docker/Docker/Resources/bin/docker.exe: Error
response from daemon: OCI runtime create failed:
container_linux.go:348: starting container process caused "exec:
\"C:/Program Files/Git/usr/bin/ba sh.exe\": stat C:/Program
Files/Git/usr/bin/bash.exe: no such file or directory": unknown.
If I tried to look into the image contents . It is asking to run the container first. How should one is able to view the files and check for an error.

Related

failed to compute cache key: failed to create hash for <DIR>: operation not supported

while attempting to build an image in Docker/macOS, I get the following error:
failed to compute cache key: failed to create hash for
/app-temp/client/dist: operation not supported
My Dockerfile looks like this:
FROM node:lts-alpine as ui-build
WORKDIR /app-temp
COPY client/ ./client/
RUN cd client && npm install
RUN cd client && npm run build
FROM node:lts-alpine as api-build
WORKDIR /app
COPY --from=ui-build /app-temp/client/dist ./client/dist
COPY server/ ./server/
RUN cd server && npm install
EXPOSE 3000
CMD ["node", "/app/server/src/index.js"]
And the docker command that I am using is:
docker build -t test-image .
Now, note that the image is successfully built in a Linux machine. However, the above-mentioned error persists in a macOS machine.
I am running macOS Big Sur 11.2.1.
Any ideas as to how solve this problem?
Thanks in advance.
just in case someone else finds himself here
Look in your .dockerignore file if dist is in the list
Also, try building with BUILDKIT disabled
DOCKER_BUILDKIT=0 docker build ....
Im gonna throw my solution out there.
Setup: .net 5 web app with docker enabled, contains a dockerfile that was created by visual studio.
There was a step where the docker file was saying to copy my csproj up to the parent folder. I edited the source directory to from parent/myproject.csproj to myproject.csproj then I ran docker build and it worked.
Im assuming that when I run docker build I was down one level in the child folder where the dockerfile lives, and the paths just werent lining up.

Can't install pip packages inside a docker container with windows

Dockerfile
# Use an official Python runtime as a base image
FROM python:3.8.1-windowsservercore
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
It is giving me this error
How to solve this proxy network error? I got solution for linux but for windows 10 i am not able to find any answer. I am using latest docker for windows.
If you have n/w proxy in between use below command :
docker build --no-cache --build-arg HTTP_PROXY=http://xx.xx.xx.xx:xx --build-arg HTTPS_PROXY=http://xx.xx.xx.xx:xx --network=host -t helloworkapp .
If you don't have any proxy use this command (use host n/w for downloading packages):
docker build --no-cache --network=host -t helloworkapp .

"Exec format error" with docker run command

I have this Golang based Dockerfile:
FROM golang:latest
RUN mkdir -p /app
WORKDIR /app
COPY bin/huru .
CMD ./huru
I checked and the huru binary file is in the working dir. I get this error:
/bin/sh: 1: ./huru: Exec format error
anyone know what that is about? "docker build" succeeds, but "docker run" fails with that error.
The "Exec format error" was simply because I was copying the binary file built on OSX/MacOS into the Docker image and trying to run that binary file in the Linux container. That don't work.
Here is the Dockerfile that worked for me:
FROM golang:latest
RUN mkdir -p /app
WORKDIR /app
COPY . .
ENV GOPATH /app
RUN go install huru
ENTRYPOINT /app/bin/huru
and my project structure like so on my host fs:
$GOPATH/
src/
huru/
.dockerignore
Dockerfile
I run:
docker build -t foo .
docker run foo
my .dockerignore file contains:
.vscode
bin
pkg
If you want to run the docker image on Macos then just specifying the target OS is sufficient:
Assuming there is a src and bin folder, execute in the src folder:
env GOOS=linux go build -o ../bin
(this works with m1, uses the arm64 architecture)
BTW
I would not use latest, I see that there is a docker image based on 1.20 which is not yet officially released at time of writing.
You could build your application (huru) for the target architecture in MacOS and then copy it into the docker image. To build for the target architecture you have to use command in the following format:
env GOOS=linux GOARCH=amd64 go build -o application main.go
This has the added advantage of having a clean dockerfile and smaller image.

Having problems deploying an go app to docker

Hi I am pretty new to go, and this is my first time working with docker to package an app into a container. I am working on a linux VM where the app is located under dir: /home/core/app/app-name In the dir app-name there is the main.go program and the Dockerfile. The Dockerfile contains this:
FROM golang:latest
RUN mkdir /app
ADD . /home/core/app/app-name
WORKDIR /app/app-name
RUN go build -o main .
CMD ["/app/main"]
EXPOSE 8080
I have tried running from dir /home/core/app/app-name:
docker build -t app-image .
But I got this error:
can't load package: package .: no buildable Go source files in /app/stars-app
The command '/bin/sh -c go build -o main .' returned a non-zero code: 1
What am I doing wrong?
Edit:
I got was able to build the image on my windows machine with the Dockfile:
FROM golang:latest
Add . /app/app-name
EXPOSE 8080
CMD ["/app/app-name/main"]
And by running:
docker build -t star-image .
I can see the image when I run "docker images", but when I try to run it using:
docker run -p 3000:8080 --name goapp --rm app-name
I get this error:
docker: Error response from daemon: Container command '/app/app-name/main' not found or does not exist..
This might work for you...
The GOPATH for the image is set to /go
install your source(s) under /go/src
given the gopath is set and sources are within the GOPATH
setting the working directory to /app
execute the build and the output should be present in the working
directory
Dockerfile
FROM golang:latest
ADD ./app /go/src/app
RUN mkdir /app
WORKDIR /app
RUN go build -o main app/app-name
CMD ["/app/main"]
EXPOSE 8080
app/app-name/main.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
docker build -t app-image .
docker run app-image
output
hello, world
I had problems with this too but somehow based on this guide, this worked for me.
# ...AS builder ...
FROM golang:1.14
WORKDIR /go/src/app
# In your case, ./main.go or just .
COPY ./server.go .
COPY --from=builder ./app/build .
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["app"]

How to specify current directory for Docker?

I'm following this part of the Docker tutorial (on a Mac): https://docs.docker.com/mac/step_four/. I'm getting an error when I try to run the docker-whalesay image because it can't find fortunes.
I started off in the Dockerfile using /user/games/fortunes. Then I changed to just fortunes. Neither work.
How do I specify in the Dockerfile to use the current folder (mydockerbuild)?
The Dockerfile in that example does not rely on files that are present on your computer, basically, the only steps needed are;
Create an empty directory (you named it mydockerbuild)
mkdir mydockerbuild
Change to that directory
cd mydockerbuild
Create a Dockerfile
Edit the Dockerfile to look like this;
FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay
Build the Dockerfile, and name the built image "docker-whale"
docker build -t docker-whale .
Run the image you just built
docker run --rm docker-whale
The /usr/games/fortunes path in the Dockerfile is referring to a path inside the container. In this case, the /usr/games/fortunes is created by the fortune package that it's installed by apt-get install -y fortunes.

Resources