Pre-requisite: DockerĀ
A popular containerization tool called Docker enables developers to quickly package programs and their environments, enabling shorter iteration times and greater resource efficiency while delivering the same intended environment each time. A container orchestration tool called Docker Compose makes it easier to meet the needs of contemporary apps. You can operate a number of linked containers simultaneously. Developers may concurrently operate, grow, and expand a container using orchestration technologies rather than operating containers manually.
Steps to Deploy a Go Web App
Step 1: Implementing a Go Web App Example
You will set up your workspace and construct a basic Go web app in this stage, which you will later containerize. The potent gorilla/mux request router, selected for its adaptability and speed, will be used by the Go app. You will save all of your data for this lesson in /go-docker. For this folder to be created, enter the following command:
$ mkdir ~/go-docker
Ā
Step 2: The main file is where youāll keep your sample Go web app. go. Use your text editor to create it:
$ nano main.go
Step 3: Using Letās Encrypt to deploy with Docker.
Itās crucial that you use HTTPS to safeguard your app. Youāll use Docker Compose to deploy nginx-proxy and its Letās Encrypt add-on in order to do this. This configuration takes care of protecting your app using HTTPS by automatically managing TLS certificate issuance and renewal. It also secures Docker containers that are proxied using nginx-proxy.
The file nginx-proxy-compose.yaml will include the Docker Compose settings for nginx-proxy. Run it to create it:
$ nano nginx-proxy-compose.yaml
Step 4: Dockerizing the Go Web App.
You will construct a Dockerfile in this part that will tell Docker how to generate an immutable image for your Go web app.
$ nano Dockerfile
Step 5: Run to set up the nginx-proxy
You will run the nginx-proxy and set up it. Follow the below command and run it on your terminal:
$ docker compose -f nginx-proxy-compose.yaml up -d
Step 6: Creating and Running the Docker Compose File
Youāll now construct the Docker Compose config file and write the configuration needed to execute the Docker image you built in the previous stage. Next, youāll run it to see whether it works properly. The Docker Compose config file, in general, defines the containers, configurations, networks, and volumes that the program needs. You can also specify that these elements start and stop as one.
In a file called go-app-compose.yaml, you will keep the Docker Compose settings for the Go web app. Run it to create it:
$ nano go-app-compose.yaml
Step 7: Now use Docker Compose to execute your Go web app in the background. Then your Docker Compose executes the Go web app. Youāll now construct the Docker Compose file and write the configuration needed to execute the Docker image you built in the previous stage. Follow the below command:
$ docker compose -f go-app-compose.yaml up -d
Step 8: Go to https://your domain/hello now. On the /hello route from Step 1, the message you specified in your code will load.
Conclusion
Your Go web app has now been successfully deployed on Ubuntu 22.04 using Docker and Nginx. Because the environment in which an application is performed is assured to remain the same each time it is run, maintaining apps using Docker takes less time. The gorilla/mux package includes more complex functionality like naming routes and delivering static files and has great documentation.