Creating a Simple Hello World Web Application with Docker and Golang (Part 1)
MUHAMMED ARIF
Posted on December 25, 2023
β Before going to code ..
- What is Docker π¦ ?
Docker is a free tool that puts your app and all its stuff into a tidy box called a container. This box works the same everywhere on your computer, a server, ποΈor in the cloud. It stops messy problems like "it works on my machine" and makes your app easy to carry around. Docker, and tools like it, make this container thing simple and super handy for building software.
I assume you understand this π. If you have no basic knowledge in Docker, watch this YouTube video. This video is an iconic one I ever saw π.
Video Link ποΈ
Lets Code .. π©βπ»
β οΈ Deffinitly you installed docker and golang on your system
- 1. > First Step
First, select any IDE like VSCode, Atom, IntelliJ, or Eclipse β it's completely your choice.
This example i selected vs code.
- > Second Step
Open and setup your project basics
My project structure is :
βββ Dockerfile
βββ go.mod
βββ main.go
In main.go i created a basic hello world api using http package
main.go
package main
import "net/http"
func main() {
http.HandleFunc("/", hellowordhandler)
http.ListenAndServe(":9000", nil)
}
func hellowordhandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
}
- > Third Step
Finally we completed our basic project setup. next we are going to Dockerfile
(A Dockerfile is a set of instructions for creating a Docker container).
Dockerfile
# Base Image
FROM golang:latest
# Set the Current Working Directory inside the container
WORKDIR /app
# Copy everything from the current directory to the PWD(Present Working Directory) inside the container
COPY . .
# Download all the dependencies
RUN go mod download
# Build the Go app
RUN go build -o main .
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the executable
CMD ["./main"]
- > Forth Step
Build This Docker Image
To build this Docker image, execute the following command:
docker build -t hello-word .
In this command, -t specifies the tag name for our final image (in this case, "hello-world"), and . indicates that the Dockerfile is in the current directory. The period (.) implies that the build context is the current directory, and Docker will use the Dockerfile found there to create the image.
Loading β. This process is may be take >1 mins.
Final Step
After complete that. you exicute another command
docker images
Result is you can see hello-world
image on their.
next is run that image
docker run -p 9000:9000 hello-world
The -p flag is used for port mapping. It maps the host machine's port to the container's port. In this case, it's mapping port 9000 on the host to port 9000 on the container. So, any traffic coming to the host machine on port 9000 will be redirected to the container's port 9000.
After Exicute This :
Conclusion
We have successfully created a simple Docker Hello World image, which serves as a helpful starting point for beginners. However, it's worth noting that the current approach results in a relatively large image size, approximately 900MB, which may not be optimal. In the spirit of continuous improvement, I am open to providing support and guidance. If you express interest and engagement with this post, I'll gladly share a follow-up on how to significantly reduce the image size to less than 10MB. Let's embark on this journey together for more efficient and streamlined Docker images!
Follow me
Posted on December 25, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
December 25, 2023