Having problems deploying an go app to docker - go

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"]

Related

How to run streamlit through docker?

I want to run the streamlit through docker. I did not find any official image. Can someone please guide me with the steps required to achieve this or Dockerimage for streamlit?
Here is the details
Operating System: Windows 10 Home
Docker version 19.03.1
Streamlit, version 0.61.0
You can look into this docker hub image.
docker run -it -p 80:80 --entrypoint "streamlit" marcskovmadsen/awesome-streamlit:latest run app.py
Not sure about the streamlit version but you can create one base on this Dockerfile.
Or you can explore streamlit-docker, working for me on my local system.
Quick Setup (own image)
Dockerfile
# Nicked from: https://github.com/markdouthwaite/streamlit-project/blob/master/Dockerfile
FROM python:3.8.4-slim
RUN pip install -U pip
COPY requirements.txt app/requirements.txt
RUN pip install -r app/requirements.txt
# copy into a directory of its own (so it isn't in the toplevel dir)
COPY . /app
WORKDIR /app
CMD ["python", "-m", "streamlit.cli", "run", "main.py", "--server.port=8080"]
EXPOSE 8080
requirements.txt
Then, in the same directory, example contents of the requirements.txt file:
streamlit==0.76.0
pandas==1.2.1
numpy==1.19.5
docker-compose.yml
In a directory above your Dockerfile and source code, you can add:
version: "3.7"
services:
streamlit:
build:
context: streamlit/
volumes:
- ./streamlit:/app
ports:
- 8080:8080

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 .

Run docker with aliased port and access to bash

I'm trying to start a docker snapshot and connect to it via bash but also alias its port so I can access it from my local system at localhost:3333, this is what I have:
docker run -d -p 3333:3000 -t -i mysnapshot /bin/bash
However while it does start the container image it doesn't connect to it via bash
This is the output it generates:
3c86ca433d645c6c11315e89bbeaf89f072e2d1fa83213d4c4256c4a1af98322
and this is the dockerfile used to build the image:
FROM node:10
Setting working directory. All the path will be relative to WORKDIR WORKDIR /usr/src/app
Installing dependencies COPY package*.json ./ RUN npm install
Copying source files COPY . .
Building app
RUN npm run build
Running the app CMD [ "npm", "start" ]
You used -d option in docker run command, which will run the container in detached mode in the background.
Please check this out.
To get into the bash run
docker exec -it <conatiner-id> /bin/bash
where <container-id> can be retrieved from docker ps output.
Also as per your dockerfile you want npm start to be the first process in the container, so while running docker run command don't specify /bin/bash because it will override the CMD npm start mentioned in the dockerfile.
Hope this helps, let me know.
It seems you may need to overwrite your entrypoint because last line of your dockerfile mention your start command is npm start.
Also, -d detached mode is not needed.
Try this one:
docker run -it -p 3333:3000 --entrypoint=/bin/bash mysnapshot

Unable to run docker image for golang in windows

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.

"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.

Resources