Create dockerfile for golang web application

王林
Release: 2024-02-08 21:57:20
forward
978 people have browsed it

为 golang Web 应用程序创建 dockerfile

php editor Youzi brings you an introduction to creating dockerfiles for golang web applications. As containerization technology develops, using Docker to build and deploy applications has become increasingly popular. In this article, we will learn how to create a valid dockerfile for golang web application to be able to easily deploy and run your application in different environments. Whether you are a beginner or an experienced developer, this article will provide you with clear steps and practical tips to help you smoothly carry out dockerized development work. Let’s get started!

Question content

I've tried following the tutorials there but for some reason I can't seem to create the dockerfile for my web application due to the way my directory is set up docker container so that I can run it on multiple devices using docker.

This is my project structure:

myapp
    /cmd
        /web
            - main.go
            - middleware.go
            - routes.go
    /pkg
        /somepackage
            - somegolangfile.go 
        /anotherpackageiuse
            - somegolangfile.go
    /templates
        -html files 
    go.mod
    go.sum
    .gitignore 
    dockerfile
Copy after login

This is the dockerfile I'm using:

from golang:latest as build

workdir /build
copy . .
run go build  -o app .
 
from alpine:latest as run
workdir /app
copy --from=build /build/app .
entrypoint ["/app/app"]
Copy after login

I get the error that there are no go files in /build. This is correct and the error makes sense since the go files are located in cmd/web/

However, I can't seem to fix it. I've tried doing this:

FROM golang:latest as build

WORKDIR /build
COPY . .
RUN go build  cmd/web/ -o app .
 
FROM alpine:latest as run
WORKDIR /app
COPY --from=build /build/app .
ENTRYPOINT ["/app/app"]
Copy after login

The main difference here is that I have replaced run go build -o app . with run go build cmd/web/ -o app . but I get the error, My sum.go and mod.go are not present in the directory and this is also true

How do I create a docker container for my web application and build it within it with my go files in a different directory? Also, when I run my web application normally, I use go run cmd/web/*.go where all golang files in the cmd/web directory need to be run at the same time. Therefore, I also need my dockerfile to build and compile all golang files together

================================================ =

Solution

Hey @radin khosraviani,

copy. .Only the source code will be copied. You will also need to copy the go module files .

Below I modified your docker file and added copy go.mod . copy go.sum . run go mod download

FROM golang:latest as build

WORKDIR /app

# Copy the Go module files
COPY go.mod .
COPY go.sum .

# Download the Go module dependencies
RUN go mod download

COPY . .

RUN go build -o /myapp ./cmd/web
 
FROM alpine:latest as run

# Copy the application executable from the build image
COPY --from=build /myapp /myapp

WORKDIR /app
EXPOSE 8080
CMD ["/myapp"]
Copy after login

The above is the detailed content of Create dockerfile for golang web application. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!