What is a dockerfile
Dockerfile is a text file used to define and build Docker images. It contains a series of instructions and configurations for building a runnable Docker container.
In Dockerfile, you can define which basic image to start building, copying files, setting environment variables, running commands, etc. Each instruction represents a step, executed in sequence, thus creating a complete Docker image.
go build
go build
Is a command in the Go language that compiles Go source code into an executable file or library.
Example
FROM golang:1.20 as builder WORKDIR /source COPY . /source RUN go build -o app . FROM ubuntu:noble COPY --from=builder /source/app /app RUN chmod +x /app ENV APP_NAME="app" ENV APP_VERSION="v0.0.1" CMD ["/app"]
The example Dockfile contains a Docker multi-stage build, the first one for building Go applications and the other for the final container image.
Phase 1: Building Go Applications
FROM golang:1.20 as builder WORKDIR /source COPY . /source RUN go build -o app .
First, usegolang:1.20
As the basic image, namedbuilder
, then set the working directory to/source
, and then copy all files in the host's current directory to the container's/source
In the directory, then executego build
Command, compile Go source code into nameapp
executable file.
Phase 2: Building the final container to run the application
FROM ubuntu:noble COPY --from=builder /source/app /app RUN chmod +x /app ENV APP_NAME="app" ENV APP_VERSION="v0.0.1" CMD ["/app"]
First, useubuntu:noble
As the final container image as the base image, the first phase (builder
stage)/source/app
Copy the file to the container/app
Directory, then runchmod +x /app
for/app
Add executable permissions and useENV
Set a series of environment variables, and finally passCMD
Specifies the command to be executed when the container starts, that is, run/app
Executable file.
The example dockerfile uses a multi-stage construction method, first compiling the application in a build container dedicated to build, and then copying the compiled executable file into the final container image. This method can reduce the size of the final image, because the final docker image will only contain the final container and the executable file, without the compilation environment required to compile the executable file.
Ideas
The simple idea of using dockerfile to build such applications is roughly to use two build stages. The purpose of the first stage is to compile the application, and the second stage is to copy the application into the final container.
The steps in the first stage are roughly, selecting the basic image, specifying the working directory, copying the host's source code into the working directory of the container, and then performing compilation to obtain the executable file.
The steps of the second stage are roughly, select the mirror of the final container, and then copy the executable file compiled from the first stage into the existing container, set environment variables, and define the run command.
Example Here is Go's application, Java's application is the same: the first stage selects the Java build environment, such asFrom:java
, then build the Java source code into a jar package, then specify the required environment for running the jar file in the second stage, and finally execute the jar application.
Common Commands
Dockerfile command | describe |
---|---|
FROM | Specify the basic image as the basis for subsequent instructions |
WORKDIR | Set the working directory for subsequent commands |
RUN | Commands executed during build |
COPY | Copy the host file or directory into the mirror |
ENV | Set environment variables inside the container |
CMD | Specify the default command when the container runs (can be overridden) |
ENTRYPOINT | Specify the main command when the container is running (cannot be overwritten, must be executed) |
Things to note
build
After writing the Dockerfile, use
docker build -t <image_name> . ## -t Specify the created target image name
build image.
run
Available
docker run <image_name>
Run the image as a container
Or usedocker-composeTo run the image.
Always remember that docker images run in containers anyway, so they can be executed
docker run -it <image_name> bash
Running the image in a container way. The above method can facilitate running the image in a container way and observe the image environment when writing the Dockerfile, thereby checking whether there are problems with the Dockerfile writing.
Summarize
This is the article about the simple application of dockerfile combined with go applications. For more related dockerfile combined with go applications, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!