From f09da7acfdc33224fde59c9dd107b59abb4c0c3a Mon Sep 17 00:00:00 2001 From: Evgenii Troinov Date: Mon, 11 Dec 2023 21:29:49 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20added=20guide=20how?= =?UTF-8?q?=20to=20build=20docker=20image=20with=20fiber?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/guide/docker.md | 114 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 docs/guide/docker.md diff --git a/docs/guide/docker.md b/docs/guide/docker.md new file mode 100644 index 0000000000..9e2df8c1ec --- /dev/null +++ b/docs/guide/docker.md @@ -0,0 +1,114 @@ +--- +id: docker +title: 🚢 Build docker image +description: Build simple docker image that you can use on the production +sidebar_position: 8 +--- + +After we have completed the development of our application, the moment comes when we want to upload it to the real world, most often we use Docker for this, wrapping our application in a container. + +To do this we need to follow a few simple steps: + +## Step 1 - Setup project + +First create folder for our project run the following command in the console: + +```bash title="~/" +mkdir example && cd example +``` + +Then we have to init go project: +```bash title="~/example" +go mod init example.org/demo +``` + +Add **Fiber** to our project: +```bash title="~/example" +go get github.com/gofiber/fiber/v2 +``` + +## Step 2 - Hello world + +To start we shold create `main.go` file: +```bash title="~/example" +touch main.go +``` + +Next we should add following content to our file: + +```go title="~/app/main.go" +package main + +import "github.com/gofiber/fiber/v2" + +func main() { + app := fiber.New() + + app.Get("/", func(c *fiber.Ctx) error { + return c.SendString("Hello, World!") + }) + + app.Listen(":4000") +} +``` + +## Step 3 - create Dockerfile + +Run the following command in the console: + +```bash +touch Dockerfile +``` + +For the image we will use [Distroless](https://github.com/GoogleContainerTools/distroless) docker image, in the file we should add following content: + +```bash title="~/example/Dockerfile" +FROM golang:1.21-alpine as base + +WORKDIR /app +COPY . . + +RUN go mod download && go mod verify +RUN GOOS=linux GOARCH=amd64 go build -o /server . + +FROM gcr.io/distroless/static-debian12 +COPY --from=base /server . + +ENV SOME_VAR=foo + +EXPOSE 4000 +CMD ["./server"] +``` + +:::info +"Distroless" images contain only your application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution. +::: + +:::tip +A docker image size is important, because this image is going to be uploaded / downloaded during the build / deploy. The more size of image is the more time you will spend on network transmittion. + +That's why we divided the build process into two stages: + +1. "base" - in the Dockerfile we start building from — golang:1.21-alpine. Alpine is the tiniest linux image we currently have. It is image size is about 3MB. + +2. use Distroless just to put binary file there. +::: + +## Step 4 - build an image + +For this you should execute fllowing command in console: +```bash title"~/example" +docker build -t . +``` + +## Step 5 - profit + +Now you can run you docker image by the command: +```bash +docker run -p 4000:4000 +``` + +And open in the browser: +``` +http://localhost:4000/ +``` \ No newline at end of file From 5fdd73ea368d15f98c52be55faf8e11587b6c47d Mon Sep 17 00:00:00 2001 From: Evgenii Troinov Date: Tue, 12 Dec 2023 13:53:47 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20fixes=20after=20revie?= =?UTF-8?q?w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/guide/docker.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/guide/docker.md b/docs/guide/docker.md index 9e2df8c1ec..68b82af8f8 100644 --- a/docs/guide/docker.md +++ b/docs/guide/docker.md @@ -1,7 +1,7 @@ --- id: docker -title: 🚢 Build docker image -description: Build simple docker image that you can use on the production +title: 🐳 Build docker image +description: Build a simple docker image that can be used in production. sidebar_position: 8 --- @@ -48,7 +48,7 @@ func main() { return c.SendString("Hello, World!") }) - app.Listen(":4000") + app.Listen(":3000") } ``` @@ -63,20 +63,20 @@ touch Dockerfile For the image we will use [Distroless](https://github.com/GoogleContainerTools/distroless) docker image, in the file we should add following content: ```bash title="~/example/Dockerfile" -FROM golang:1.21-alpine as base +FROM golang:1.21 as build WORKDIR /app COPY . . RUN go mod download && go mod verify -RUN GOOS=linux GOARCH=amd64 go build -o /server . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /server . FROM gcr.io/distroless/static-debian12 -COPY --from=base /server . +COPY --from=build /server . ENV SOME_VAR=foo -EXPOSE 4000 +EXPOSE 3000 CMD ["./server"] ``` @@ -105,10 +105,10 @@ docker build -t . Now you can run you docker image by the command: ```bash -docker run -p 4000:4000 +docker run -p 3000:3000 ``` And open in the browser: ``` -http://localhost:4000/ +http://localhost:3000/ ``` \ No newline at end of file From fbfe70f00dbd1f017d190a9bccb2126043b3891c Mon Sep 17 00:00:00 2001 From: Evgenii Troinov Date: Fri, 15 Dec 2023 01:10:17 +0100 Subject: [PATCH 3/3] Doc: improved descr --- docs/guide/docker.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/guide/docker.md b/docs/guide/docker.md index 68b82af8f8..3234b7567a 100644 --- a/docs/guide/docker.md +++ b/docs/guide/docker.md @@ -29,12 +29,12 @@ go get github.com/gofiber/fiber/v2 ## Step 2 - Hello world -To start we shold create `main.go` file: +To start we should create `main.go` file: ```bash title="~/example" touch main.go ``` -Next we should add following content to our file: +Next we need to add the following content to our file: ```go title="~/app/main.go" package main @@ -103,7 +103,7 @@ docker build -t . ## Step 5 - profit -Now you can run you docker image by the command: +Now you can run the docker image using the command: ```bash docker run -p 3000:3000 ```