Creating a Simple Hello World Web Application with Docker and Golang (Part 1)
Tags: docker, go, programming, beginners
Published: 2023-12-25 20:20:59

## **✋ 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 🖇️](https://youtu.be/3c-iBn73dDE?si=fQEmIcB96WAexkY9)


## 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.


![Docker image building process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eijk19lejcteqwejvbkh.png)
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. 
![Docker image listing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r85m6pr5seatt3o7lpp.png)

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 : 

![Project final result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twr76td2pz17k2lx67n9.png)


----------------------


## 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](www.linkedin.com/in/muhammed-arifp)
2. [Instagram](https://www.instagram.com/arifu_00/)