Dockerfile
Docker allows developers to package their applications along with all dependencies into a single unit called a container. This ensures consistency across different environments, such as development, testing, and production, reducing the "it works on my machine" problem.
Install Docker: https://docs.docker.com/engine/install/
Add Makefile
A Makefile is a special file used in software development projects, particularly in Unix-like operating systems, to automate the compilation and building of executable programs or libraries from source code.
Add Makefile:
# Binary name
BINARY_NAME=lr_ft_books
.PHONY: lint
lint:
@echo "Linting..."
golangci-lint run
build:
@echo "Building $(BINARY_NAME)..."
go build -o $(BINARY_NAME)
build-linux:
@echo "Building $(BINARY_NAME) for Linux..."
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o $(BINARY_NAME)
We‘re building for Linux because these binaries will be deployed in a Linux environment inside Docker.
Run make build
to build the binary:
make build-linux
Dockerfile for the service
Add Dockerfile:
# alpine linux
FROM alpine:3.19
ENV APP_BIN=lr_ft_books
ARG SERVER_DIR=/home/.server
WORKDIR $SERVER_DIR
COPY ./${APP_BIN} .
ENV GIN_MODE=release
CMD ./${APP_BIN}
Alpine Linux is a lightweight and secure Linux distribution that is particularly well-suited for containerized environments, embedded systems, and resource-constrained environments where efficiency and security are paramount.
With this Dockerfile, we‘re ready to run it with Elasticsearch in the Docker Compose.