-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
📚 Doc: added guide how to build docker image with fiber #2759
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
--- | ||
id: docker | ||
title: 🐳 Build docker image | ||
description: Build a simple docker image that can be used in 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 should create `main.go` file: | ||
```bash title="~/example" | ||
touch main.go | ||
``` | ||
|
||
Next we need to add the 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(":3000") | ||
} | ||
``` | ||
|
||
## 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 as build | ||
|
||
WORKDIR /app | ||
COPY . . | ||
|
||
RUN go mod download && go mod verify | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /server . | ||
|
||
FROM gcr.io/distroless/static-debian12 | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
COPY --from=build /server . | ||
|
||
ENV SOME_VAR=foo | ||
|
||
EXPOSE 3000 | ||
CMD ["./server"] | ||
``` | ||
|
||
:::info | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say remove everything below this line, except the last step about running it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with you if we are talking about experienced developers (3-5 years of experience) but I tried to cover as large a group of developers as possible. For example, I wouldn’t read such a section at all because I have a lot of experience with compiled languages and Docker and I can write a config using Docker documentation, but many people who studied with me usually only knew the general principles and how to run containers. They did not know how to assemble containers and what two-stage assembly is, etc. By adding this information here, I want to close all possible questions. Because otherwise they will ask them here in "Discuss" or in stackoverflow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but they should read Docker Docs. It's not GoFiber job to teach Docker. If you want, add a note about learning more about Docker / Multistage builds from here: https://docs.docker.com/build/building/multi-stage/ |
||
"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 | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 <your_docker_image_tag> . | ||
``` | ||
|
||
## Step 5 - profit | ||
|
||
Now you can run the docker image using the command: | ||
```bash | ||
docker run -p 3000:3000 <your_docker_image_tag> | ||
``` | ||
|
||
And open in the browser: | ||
``` | ||
http://localhost:3000/ | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Packaging instead of Wrapping. This paragraph could use some work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sry, english is not my native language and I'm not sure what I get an idea. Can you give me an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The paragraph is more suited for a blog or medium, not the documentation of a library