docker,

Dockerizing a Go application

Mar 08, 2023 · 1 min read · Post a comment

Dockerizing a Go(lang) application is kinda similar to what we did with the Python one.

Prerequisites

  • Docker
  • Go(lang) application

Solution

Step 1. Create a Dockerfile in your Go(lang) project repository.

Step 2. Choose a base Docker image. As with Python, I go for the alpine versions. For instance:

FROM golang:alpine

Step 3. Set the working directory. Let’s call it /app.

WORKDIR /app

Step 4. Copy the current directory files.

COPY . ./

Step 5. Next, build main.

RUN go build -o main

Step 6. Finally, run the application code.

CMD ["/app/main.go"]

Step 7. Save the Dockerfile and start building the Docker image.

docker build -t sample-golang-app .

Step 8. Start the container.

docker run -p 8000:8000 sample-golang-app

Your containerized Go app should be accessible on http://localhost:8000.

Conclusion

If you get stuck at some step, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.