DEV Community

Cover image for Creating a Simple Hello World Web Application with Docker and Golang (Part 1)
MUHAMMED ARIF
MUHAMMED ARIF

Posted on • Edited on

Creating a Simple Hello World Web Application with Docker and Golang (Part 1)

βœ‹ 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


Enter fullscreen mode Exit fullscreen mode

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"))
}



Enter fullscreen mode Exit fullscreen mode
  • > 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"]


Enter fullscreen mode Exit fullscreen mode
  • > Forth Step

Build This Docker Image

To build this Docker image, execute the following command:



docker build -t hello-word . 


Enter fullscreen mode Exit fullscreen mode

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.

Docker image building process
Loading β­•. This process is may be take >1 mins.

Final Step

After complete that. you exicute another command



docker images


Enter fullscreen mode Exit fullscreen mode

Result is you can see hello-world image on their.
Docker image listing

next is run that image



docker run -p 9000:9000 hello-world


Enter fullscreen mode Exit fullscreen mode

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 :

Project final result


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

  1. Linkedin
  2. Instagram

Top comments (5)

Collapse
 
ladoxer profile image
Muhammed Labeeb

This tutorial is awesome! The clear step-by-step instructions and code examples make it easy for beginners like me to follow along.

I'm definitely interested in learning more about reducing the Docker image size, so I'm looking forward to your follow-up post!

Collapse
 
sc0v0ne profile image
sc0v0ne

Very Good. You captured many point important.

Collapse
 
vitfil profile image
Vitali Filinkou • Edited

I've never ever seen something more brilliant!

Collapse
 
lukman_am profile image
MUHAMMED LUKMAN A M

awesome tutorial..

Collapse
 
farhan9645 profile image
Farhan

nice content