How to dockerize a golang app:
1. Create a Dockerfile:
In order to create a Docker container for a Golang application, you will first need to create a Dockerfile that describes the container image. The Dockerfile contains instructions on how to build, configure, and package your Go application inside a Docker container. Here’s an example Dockerfile:
# Base Golang image with a minimum version of GoFROM golang:1.16-alpine# Set the working directory in the containerWORKDIR /app# Copy the source code into the containerCOPY . .# Build the Go binary inside the containerRUN go build -o myapp# Expose port 8080 on the containerEXPOSE 8080# Set the command to run when the container startsCMD ["./myapp"]
2. Build the Docker image:
Once you have created the Dockerfile, you can build the Docker image by running the following command in the directory where your Dockerfile is located:
docker build -t myapp .
This command will create a Docker image with the tag
using the current directory as the build context.
3. Run the Docker container:
After building the Docker image, you can run the Docker container by running the following command:
docker run -p 8080:8080 myapp
This command will start a Docker container based on the
Docker image and map port 8080 on the host machine to port 8080 on the container. You should now be able to access your Golang application at
That’s it! You should now have a Golang application running inside a Docker container.