Home  >  Article  >  Backend Development  >  golang docker web deployment

golang docker web deployment

WBOY
WBOYOriginal
2023-05-15 09:57:37499browse

With the rapid development of the Internet, the deployment and management of enterprise applications is becoming more and more complex. The emergence of Docker container technology can help developers greatly simplify the application deployment process and environment construction work. This article will introduce how to use Go language and Docker container technology to deploy and manage web applications.

1. Preparation

1. Install the Go language development environment.
You need to install the Go language development environment and related tools. The download address is: https://golang.org/dl/.

2. Install Docker container technology.
You need to install the latest version of Docker CE. The download address is: https://www.docker.com/community-edition.

3. Install the code editor.
This article uses Visual Studio Code as the code editor. The download address is: https://code.visualstudio.com/download.

4. Create a Go Web project.
In the command line, enter your working directory and use the following command to create a Go Web project named "goweb".

$ go mod init goweb

5. Download the required Go dependency packages.
Use the following commands to download the required Go dependency packages.

$ go get -u github.com/gin-gonic/gin

2. Create a Web application

In the previous step, the Gin framework has been installed as a Web application Basic support for the program, let's create a simple web application.

1. Create a folder in the project and name it "static" as a directory to store front-end resources and static files.

2. Create a folder in the project named "templates" to store HTML template files.

3. Create a Go file named "main.go" with the following content:

package main

import "github.com/gin-gonic/gin"

func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/*")
    router.Static("/static", "./static")
    router.GET("/", func(c *gin.Context) {
        c.HTML(200, "index.tmpl", gin.H{
            "title": "Golang Docker Web Deployment",
        })
    })
    router.Run(":8080")
}

The above code creates a simple website, using the Gin framework to load templates and static files. The website can be viewed by visiting http://localhost:8080/.

3. Build a Docker image

Next let us use Docker to build a Docker image.

1. Create a file named "Dockerfile" in the project root directory. The file content is as follows:

FROM golang:1.16 AS build

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

COPY --from=build /app/app .

EXPOSE 8080

CMD ["./app"]

The above code creates a Dockerfile. In the first stage, use golang :1.16 as the base image, install the required Go dependency packages, and copy the local code to the image to build the application. In the second phase, the latest version of the alpine image is used as the base image and the application files are copied into the container to run.

2. Open the command line, enter the project root directory, and execute the following command to build the Docker image.

$ docker build -t golang-web .

The above command will read the contents of the Dockerfile file and build an image named "golang-web".

3. Run the Docker container
Run the following command to start the container.

$ docker run -p 8080:8080 golang-web

The above command will start a container named "golang-web" and map the container's 8080 port to the host's 8080 port.

4. Manage multiple containers through Docker Compose

If you need to manage multiple containers at the same time, you can use Docker Compose to create and manage multiple containers.

1. Create the docker-compose.yml file
In the root directory of the project, create a file named "docker-compose.yml" with the following content:

version: '3'

services:
  backend:
    build: .
    ports:
      - "8080:8080"
    command: ["./app"]
  frontend:
    image: nginx:alpine
    volumes:
      - ./frontend:/usr/share/nginx/html
    ports:
      - "80:80"

Above The configuration will create two services: one named "backend" to run the Go web application; and another named "frontend" to run the NGINX web server as a front-end proxy.

2. Create the frontend directory
In the root directory of the project, create a directory named "frontend" and create an HTML file named "index.html" with the following content.

<!DOCTYPE html>
<html>
<head>
    <title>Go Docker Web Deployment</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

The above code is a simple HTML page.

3. Start Docker Compose
Enter the project root directory in the command line and run the following command to start Docker Compose:

$ docker-compose up -d

The above command Two services will be started. Note that using the "-d" option allows Docker Compose to run in the background.

4. Summary

Through the introduction of this article, we can use Go language and Docker container technology to quickly build a simple web application, and use Docker Compose to manage multiple containers. This method can greatly simplify the deployment process and environment setup work, and brings convenience to the rapid deployment and management of enterprise applications.

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

Statement:
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