» Go: Make Web Chat App with Socket.IO » 3. Deployment » 3.1 Dockerfile

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.

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_webchat

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-linux to build the binary:

make build-linux

Dockerfile for the service

Add Dockerfile:

# Alpine linux https://www.alpinelinux.org/
FROM alpine:3.19

ENV APP_BIN=lr_webchat
ARG SERVER_DIR=/home/.server
WORKDIR $SERVER_DIR
COPY ./bin/${APP_BIN} .

ENV GIN_MODE=release

CMD ./${APP_BIN}

With this Dockerfile, we‘re ready to run it with the front-end app in the Docker Compose.

PrevNext