Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
‘Hello, World!’
Application:
How to
Dockerize
Golang
Application
www.bacancytechnology.com
Introduction
Do you find getting started with Docker
intimidating? Do you want to take the first
step and successfully dockerize golang
application? Are you having trouble finding
a basic step-by-step tutorial to dockerize
golang application? All these questions and
just one answer – If yes, you’ve chosen the
correct tutorial!


The purpose of this tutorial is to get your
hands on Docker for the starters. In this
guideline, we will build a ‘Hello, World’
application and follow simple steps to
dockerize the golang app. Without further
ado, let’s get started with our tutorial.
Prerequisites
to Dockerize
Golang
Application
Before building the application, make sure
your system is installed with docker and
golang. If not, then visit the links below to
install.


⦿Install Docker
⦿Install Golang
Project Setup
First of all, let’s create the main.go file and
initialize the application using the
command go mod init


Our project structure will look like this-


sample-dockerize-app
|– main.go
|– Dockerfile


Our main.go will contain the following code.


package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/", func(rw
http.ResponseWriter, r *http.Request) {
response := map[string]string{
"message": "Welcome to Dockerized app",
}
json.NewEncoder(rw).Encode(response)
})
router.HandleFunc("/{name}", func(rw
http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
var message string
if name == "" {
message = "Hello World"
} else {
message = "Hello " + name
}
response := map[string]string{
"message": message,
}
json.NewEncoder(rw).Encode(response)
})
log.Println("Server is running!")
fmt.Println(http.ListenAndServe(":8081",
router))
}
Create a
Docker Image
As the Docker documentation says-


An image includes everything needed to run
an application- the code or binary,
runtimes, dependencies, and any other
filesystem objects required.


An image consists of your app definition
and everything needed to run the
application in the simplest terms.


For creating a docker image, you need to
write steps in the config file. The
conventional and most preferred file name
is Dockerfile, but you can use any name of
your choice. However, in my opinion, it’s
always better to follow standards.


Now, create a file named Dockerfile and
write the following code.
As the Docker documentation says-


An image includes everything needed to run
an application- the code or binary,
runtimes, dependencies, and any other
filesystem objects required.


An image consists of your app definition
and everything needed to run the
application in the simplest terms.


For creating a docker image, you need to
write steps in the config file. The
conventional and most preferred file name
is Dockerfile, but you can use any name of
your choice. However, in my opinion, it’s
always better to follow standards.


Now, create a file named Dockerfile and
write the following code.
# Build Stage
# First pull Golang image
FROM golang:1.17-alpine as build-env
# Set environment variable
ENV APP_NAME sample-dockerize-app
ENV CMD_PATH main.go
# Copy application data into image
COPY . $GOPATH/src/$APP_NAME
WORKDIR $GOPATH/src/$APP_NAME
# Budild application
RUN CGO_ENABLED=0 go build -v -o
/$APP_NAME
$GOPATH/src/$APP_NAME/$CMD_PATH
# Run Stage
FROM alpine:3.14
# Set environment variable
ENV APP_NAME sample-dockerize-app
# Copy only required data into this image
COPY --from=build-env /$APP_NAME .
# Expose application port
EXPOSE 8081
# Start app
CMD ./$APP_NAME
Once done with the file, run the below
command to update your application’s
dependencies


go mod tidy
Build and Run
Docker Image
Use the below command to build the docker
image.


docker build --rm -t golang-docker-example .


After executing the command successfully
your screen will display following output.
This command will build the image and tag
it with the name golang-docker-example.
For now, it will just build the image and
won’t do anything. The next time would be
running the image for handling requests.
The running image is what we call a
container.
Use the below command to build the docker
image.


docker build --rm -t golang-docker-example .


After executing the command successfully
your screen will display following output.
This command will build the image and tag
it with the name golang-docker-example.
For now, it will just build the image and
won’t do anything. The next time would be
running the image for handling requests.
The running image is what we call a
container.
Use the following command to run the
image.


docker run -p 8081:8081 golang-docker-
example




Successfully executed!






The -p flag is used for defining the port
binding. Our application inside the
container will be running on the 8081 port
and we will bind it to the host port, i.e.,
8081. You can use a different port to bind as
well. For that, you can run -p
$HOST_PORT:8081, for example -p
3000:8081.
Once you are done, Voila! Your application
will be running on port 8081. You have now
successfully dockerized golang app. Open
the browser and hit http://localhost:8081 to
check your application.


Your browser will display










You can also run
http://localhost:8081/{name}* and replace
{name}* with anything that will print the
message having the variable.
Source Code:
Golang
Docker
Example
Github
You can run the below command to clone
the github repo to play around with the
code, and feel free to visit the golang docker
example github.


git clone
https://github.com/DekivadiyaKishan/gola
ng-docker-example.git
Conclusion
So, this was a beginner tutorial on how to
dockerize golang application. I hope the
tutorial has helped you to at least get
started with Docker. If you are a Golang
enthusiast and want to explore Golang,
please visit the Golang tutorials page.


If you are looking for a skilled developer
who can help you with your project
requirements, contact Bacancy and hire
golang developer.
Thank You

More Related Content

What's hot

PhoneGap JavaScript API vs Native Components
PhoneGap JavaScript API vs Native ComponentsPhoneGap JavaScript API vs Native Components
PhoneGap JavaScript API vs Native Components
TechAhead
 
Flask
FlaskFlask
Modern web app with REACT
Modern web app with REACTModern web app with REACT
Modern web app with REACT
AndryRajohnson
 
How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2
Katy Slemon
 
What’s new in laravel 9
What’s new in laravel 9What’s new in laravel 9
What’s new in laravel 9
Katy Slemon
 
Instrumentación de entrega continua con Gitlab
Instrumentación de entrega continua con GitlabInstrumentación de entrega continua con Gitlab
Instrumentación de entrega continua con Gitlab
Software Guru
 
React 101
React 101React 101
React 101
AndryRajohnson
 
MoSync Cross Platform mobile app development
MoSync  Cross Platform mobile app developmentMoSync  Cross Platform mobile app development
MoSync Cross Platform mobile app development
Uday Kothari
 
How to create a local Android open source project mirror in 6 easy steps
How to create a local Android open source project mirror in 6 easy stepsHow to create a local Android open source project mirror in 6 easy steps
How to create a local Android open source project mirror in 6 easy steps
Deveo
 
Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!
Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!
Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!
Simon Walton
 
Maven, Eclipse And OSGi Working Together
Maven, Eclipse And OSGi Working TogetherMaven, Eclipse And OSGi Working Together
Maven, Eclipse And OSGi Working Together
Carlos Sanchez
 
Android Wear Code Lab
Android Wear Code LabAndroid Wear Code Lab
Android Wear Code Lab
Gerard
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
Katy Slemon
 
Continuous integration with Git & CI Joe
Continuous integration with Git & CI JoeContinuous integration with Git & CI Joe
Continuous integration with Git & CI Joe
Shawn Price
 
React Native - Getting Started
React Native - Getting StartedReact Native - Getting Started
React Native - Getting Started
Tracy Lee
 
Develop FOSS project using Google Code Hosting
Develop FOSS project using Google Code HostingDevelop FOSS project using Google Code Hosting
Develop FOSS project using Google Code Hosting
Narendra Sisodiya
 
Quick flask an intro to flask
Quick flask   an intro to flaskQuick flask   an intro to flask
Quick flask an intro to flask
juzten
 
Gradle Play Publisher Plugin
Gradle Play Publisher PluginGradle Play Publisher Plugin
Gradle Play Publisher Plugin
Daniel Kao
 
Continuous Integration of Mobile Apps with Docker and Appium
Continuous Integration of Mobile Apps with Docker and AppiumContinuous Integration of Mobile Apps with Docker and Appium
Continuous Integration of Mobile Apps with Docker and Appium
Emergya
 
Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!
Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!
Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!
Michał Ćmil
 

What's hot (20)

PhoneGap JavaScript API vs Native Components
PhoneGap JavaScript API vs Native ComponentsPhoneGap JavaScript API vs Native Components
PhoneGap JavaScript API vs Native Components
 
Flask
FlaskFlask
Flask
 
Modern web app with REACT
Modern web app with REACTModern web app with REACT
Modern web app with REACT
 
How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2
 
What’s new in laravel 9
What’s new in laravel 9What’s new in laravel 9
What’s new in laravel 9
 
Instrumentación de entrega continua con Gitlab
Instrumentación de entrega continua con GitlabInstrumentación de entrega continua con Gitlab
Instrumentación de entrega continua con Gitlab
 
React 101
React 101React 101
React 101
 
MoSync Cross Platform mobile app development
MoSync  Cross Platform mobile app developmentMoSync  Cross Platform mobile app development
MoSync Cross Platform mobile app development
 
How to create a local Android open source project mirror in 6 easy steps
How to create a local Android open source project mirror in 6 easy stepsHow to create a local Android open source project mirror in 6 easy steps
How to create a local Android open source project mirror in 6 easy steps
 
Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!
Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!
Swamp 2019: She Promoted Her Helm Chart: You Won't Believe What Happened Next!
 
Maven, Eclipse And OSGi Working Together
Maven, Eclipse And OSGi Working TogetherMaven, Eclipse And OSGi Working Together
Maven, Eclipse And OSGi Working Together
 
Android Wear Code Lab
Android Wear Code LabAndroid Wear Code Lab
Android Wear Code Lab
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
 
Continuous integration with Git & CI Joe
Continuous integration with Git & CI JoeContinuous integration with Git & CI Joe
Continuous integration with Git & CI Joe
 
React Native - Getting Started
React Native - Getting StartedReact Native - Getting Started
React Native - Getting Started
 
Develop FOSS project using Google Code Hosting
Develop FOSS project using Google Code HostingDevelop FOSS project using Google Code Hosting
Develop FOSS project using Google Code Hosting
 
Quick flask an intro to flask
Quick flask   an intro to flaskQuick flask   an intro to flask
Quick flask an intro to flask
 
Gradle Play Publisher Plugin
Gradle Play Publisher PluginGradle Play Publisher Plugin
Gradle Play Publisher Plugin
 
Continuous Integration of Mobile Apps with Docker and Appium
Continuous Integration of Mobile Apps with Docker and AppiumContinuous Integration of Mobile Apps with Docker and Appium
Continuous Integration of Mobile Apps with Docker and Appium
 
Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!
Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!
Eclipse RCP outside of Eclipse IDE - Gradle to the rescue!
 

Similar to ‘Hello, world!’ application how to dockerize golang application

Docker Introduction.pdf
Docker Introduction.pdfDocker Introduction.pdf
Docker Introduction.pdf
OKLABS
 
How to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorialHow to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorial
Katy Slemon
 
Docker workshop GDSC_CSSC
Docker workshop GDSC_CSSCDocker workshop GDSC_CSSC
Docker workshop GDSC_CSSC
GDSC UofT Mississauga
 
Containerize spring boot application with docker
Containerize spring boot application with dockerContainerize spring boot application with docker
Containerize spring boot application with docker
Sunil kumar Mohanty
 
Start your adventure with docker
Start your adventure with dockerStart your adventure with docker
Start your adventure with docker
Sagar Dash
 
Multi-stage Docker builds to make building easy!
Multi-stage Docker builds to make building easy!Multi-stage Docker builds to make building easy!
Multi-stage Docker builds to make building easy!
Milindu Sanoj Kumarage
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupal
DrupalDay
 
Docker @ Atlogys
Docker @ AtlogysDocker @ Atlogys
Tips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podmanTips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podman
Thierry Gayet
 
Docker Command Line, Using and Choosing containers
Docker Command Line, Using and Choosing containers Docker Command Line, Using and Choosing containers
Docker Command Line, Using and Choosing containers
Will Hall
 
Academy PRO: Docker. Part 2
Academy PRO: Docker. Part 2Academy PRO: Docker. Part 2
Academy PRO: Docker. Part 2
Binary Studio
 
Build, Deploy and Run Node Js Application on Azure using Docker
Build, Deploy and Run Node Js Application on Azure using DockerBuild, Deploy and Run Node Js Application on Azure using Docker
Build, Deploy and Run Node Js Application on Azure using Docker
Osama Mustafa
 
[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In Action[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In Action
Hazem Saleh
 
Docker in everyday development
Docker in everyday developmentDocker in everyday development
Docker in everyday development
Justyna Ilczuk
 
DevOps: Docker Workshop
DevOps: Docker WorkshopDevOps: Docker Workshop
DevOps: Docker Workshop
Joonas Hämäläinen
 
Docker Basics
Docker BasicsDocker Basics
Docker Basics
ParagKhalas
 
Docker Basic to Advance
Docker Basic to AdvanceDocker Basic to Advance
Docker Basic to Advance
Paras Jain
 
Docker Essentials Workshop— Innovation Labs July 2020
Docker Essentials Workshop— Innovation Labs July 2020Docker Essentials Workshop— Innovation Labs July 2020
Docker Essentials Workshop— Innovation Labs July 2020
CloudHero
 
Academy PRO: Docker. Lecture 2
Academy PRO: Docker. Lecture 2Academy PRO: Docker. Lecture 2
Academy PRO: Docker. Lecture 2
Binary Studio
 
Getting Started with Docker
Getting Started with Docker Getting Started with Docker
Getting Started with Docker
Anup Segu
 

Similar to ‘Hello, world!’ application how to dockerize golang application (20)

Docker Introduction.pdf
Docker Introduction.pdfDocker Introduction.pdf
Docker Introduction.pdf
 
How to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorialHow to dockerize rails application compose and rails tutorial
How to dockerize rails application compose and rails tutorial
 
Docker workshop GDSC_CSSC
Docker workshop GDSC_CSSCDocker workshop GDSC_CSSC
Docker workshop GDSC_CSSC
 
Containerize spring boot application with docker
Containerize spring boot application with dockerContainerize spring boot application with docker
Containerize spring boot application with docker
 
Start your adventure with docker
Start your adventure with dockerStart your adventure with docker
Start your adventure with docker
 
Multi-stage Docker builds to make building easy!
Multi-stage Docker builds to make building easy!Multi-stage Docker builds to make building easy!
Multi-stage Docker builds to make building easy!
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupal
 
Docker @ Atlogys
Docker @ AtlogysDocker @ Atlogys
Docker @ Atlogys
 
Tips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podmanTips pour sécuriser ses conteneurs docker/podman
Tips pour sécuriser ses conteneurs docker/podman
 
Docker Command Line, Using and Choosing containers
Docker Command Line, Using and Choosing containers Docker Command Line, Using and Choosing containers
Docker Command Line, Using and Choosing containers
 
Academy PRO: Docker. Part 2
Academy PRO: Docker. Part 2Academy PRO: Docker. Part 2
Academy PRO: Docker. Part 2
 
Build, Deploy and Run Node Js Application on Azure using Docker
Build, Deploy and Run Node Js Application on Azure using DockerBuild, Deploy and Run Node Js Application on Azure using Docker
Build, Deploy and Run Node Js Application on Azure using Docker
 
[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In Action[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In Action
 
Docker in everyday development
Docker in everyday developmentDocker in everyday development
Docker in everyday development
 
DevOps: Docker Workshop
DevOps: Docker WorkshopDevOps: Docker Workshop
DevOps: Docker Workshop
 
Docker Basics
Docker BasicsDocker Basics
Docker Basics
 
Docker Basic to Advance
Docker Basic to AdvanceDocker Basic to Advance
Docker Basic to Advance
 
Docker Essentials Workshop— Innovation Labs July 2020
Docker Essentials Workshop— Innovation Labs July 2020Docker Essentials Workshop— Innovation Labs July 2020
Docker Essentials Workshop— Innovation Labs July 2020
 
Academy PRO: Docker. Lecture 2
Academy PRO: Docker. Lecture 2Academy PRO: Docker. Lecture 2
Academy PRO: Docker. Lecture 2
 
Getting Started with Docker
Getting Started with Docker Getting Started with Docker
Getting Started with Docker
 

More from Katy Slemon

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfReact Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfData Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfHow Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfWhat’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
Katy Slemon
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfWhy Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
Katy Slemon
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfHow Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
How to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdfHow to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdf
Katy Slemon
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfSure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfIoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfUnderstanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfThe Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfNew Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfHow to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfChoose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfFlutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfAngular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfHow to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfRuby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 

More from Katy Slemon (20)

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfReact Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfData Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfHow Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfWhat’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfWhy Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfHow Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
 
How to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdfHow to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdf
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfSure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfIoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfUnderstanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfThe Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfNew Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfHow to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfChoose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfFlutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfAngular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfHow to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfRuby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
 

Recently uploaded

UiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs ConferenceUiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs Conference
UiPathCommunity
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
SynapseIndia
 
一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理
一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理
一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理
uuuot
 
Performance Budgets for the Real World by Tammy Everts
Performance Budgets for the Real World by Tammy EvertsPerformance Budgets for the Real World by Tammy Everts
Performance Budgets for the Real World by Tammy Everts
ScyllaDB
 
Hire a private investigator to get cell phone records
Hire a private investigator to get cell phone recordsHire a private investigator to get cell phone records
Hire a private investigator to get cell phone records
HackersList
 
What Not to Document and Why_ (North Bay Python 2024)
What Not to Document and Why_ (North Bay Python 2024)What Not to Document and Why_ (North Bay Python 2024)
What Not to Document and Why_ (North Bay Python 2024)
Margaret Fero
 
5G bootcamp Sep 2020 (NPI initiative).pptx
5G bootcamp Sep 2020 (NPI initiative).pptx5G bootcamp Sep 2020 (NPI initiative).pptx
5G bootcamp Sep 2020 (NPI initiative).pptx
SATYENDRA100
 
Navigating Post-Quantum Blockchain: Resilient Cryptography in Quantum Threats
Navigating Post-Quantum Blockchain: Resilient Cryptography in Quantum ThreatsNavigating Post-Quantum Blockchain: Resilient Cryptography in Quantum Threats
Navigating Post-Quantum Blockchain: Resilient Cryptography in Quantum Threats
anupriti
 
Why do You Have to Redesign?_Redesign Challenge Day 1
Why do You Have to Redesign?_Redesign Challenge Day 1Why do You Have to Redesign?_Redesign Challenge Day 1
Why do You Have to Redesign?_Redesign Challenge Day 1
FellyciaHikmahwarani
 
DealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 editionDealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 edition
Yevgen Sysoyev
 
Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...
BookNet Canada
 
Verti - EMEA Insurer Innovation Award 2024
Verti - EMEA Insurer Innovation Award 2024Verti - EMEA Insurer Innovation Award 2024
Verti - EMEA Insurer Innovation Award 2024
The Digital Insurer
 
Data Protection in a Connected World: Sovereignty and Cyber Security
Data Protection in a Connected World: Sovereignty and Cyber SecurityData Protection in a Connected World: Sovereignty and Cyber Security
Data Protection in a Connected World: Sovereignty and Cyber Security
anupriti
 
@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time
@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time
@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time
amitchopra0215
 
AC Atlassian Coimbatore Session Slides( 22/06/2024)
AC Atlassian Coimbatore Session Slides( 22/06/2024)AC Atlassian Coimbatore Session Slides( 22/06/2024)
AC Atlassian Coimbatore Session Slides( 22/06/2024)
apoorva2579
 
Coordinate Systems in FME 101 - Webinar Slides
Coordinate Systems in FME 101 - Webinar SlidesCoordinate Systems in FME 101 - Webinar Slides
Coordinate Systems in FME 101 - Webinar Slides
Safe Software
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
Aurora Consulting
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
ArgaBisma
 
this resume for sadika shaikh bca student
this resume for sadika shaikh bca studentthis resume for sadika shaikh bca student
this resume for sadika shaikh bca student
SadikaShaikh7
 
Blockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre timesBlockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre times
anupriti
 

Recently uploaded (20)

UiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs ConferenceUiPath Community Day Kraków: Devs4Devs Conference
UiPath Community Day Kraków: Devs4Devs Conference
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
 
一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理
一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理
一比一原版(msvu毕业证书)圣文森山大学毕业证如何办理
 
Performance Budgets for the Real World by Tammy Everts
Performance Budgets for the Real World by Tammy EvertsPerformance Budgets for the Real World by Tammy Everts
Performance Budgets for the Real World by Tammy Everts
 
Hire a private investigator to get cell phone records
Hire a private investigator to get cell phone recordsHire a private investigator to get cell phone records
Hire a private investigator to get cell phone records
 
What Not to Document and Why_ (North Bay Python 2024)
What Not to Document and Why_ (North Bay Python 2024)What Not to Document and Why_ (North Bay Python 2024)
What Not to Document and Why_ (North Bay Python 2024)
 
5G bootcamp Sep 2020 (NPI initiative).pptx
5G bootcamp Sep 2020 (NPI initiative).pptx5G bootcamp Sep 2020 (NPI initiative).pptx
5G bootcamp Sep 2020 (NPI initiative).pptx
 
Navigating Post-Quantum Blockchain: Resilient Cryptography in Quantum Threats
Navigating Post-Quantum Blockchain: Resilient Cryptography in Quantum ThreatsNavigating Post-Quantum Blockchain: Resilient Cryptography in Quantum Threats
Navigating Post-Quantum Blockchain: Resilient Cryptography in Quantum Threats
 
Why do You Have to Redesign?_Redesign Challenge Day 1
Why do You Have to Redesign?_Redesign Challenge Day 1Why do You Have to Redesign?_Redesign Challenge Day 1
Why do You Have to Redesign?_Redesign Challenge Day 1
 
DealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 editionDealBook of Ukraine: 2024 edition
DealBook of Ukraine: 2024 edition
 
Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...Transcript: Details of description part II: Describing images in practice - T...
Transcript: Details of description part II: Describing images in practice - T...
 
Verti - EMEA Insurer Innovation Award 2024
Verti - EMEA Insurer Innovation Award 2024Verti - EMEA Insurer Innovation Award 2024
Verti - EMEA Insurer Innovation Award 2024
 
Data Protection in a Connected World: Sovereignty and Cyber Security
Data Protection in a Connected World: Sovereignty and Cyber SecurityData Protection in a Connected World: Sovereignty and Cyber Security
Data Protection in a Connected World: Sovereignty and Cyber Security
 
@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time
@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time
@Call @Girls Pune 0000000000 Riya Khan Beautiful Girl any Time
 
AC Atlassian Coimbatore Session Slides( 22/06/2024)
AC Atlassian Coimbatore Session Slides( 22/06/2024)AC Atlassian Coimbatore Session Slides( 22/06/2024)
AC Atlassian Coimbatore Session Slides( 22/06/2024)
 
Coordinate Systems in FME 101 - Webinar Slides
Coordinate Systems in FME 101 - Webinar SlidesCoordinate Systems in FME 101 - Webinar Slides
Coordinate Systems in FME 101 - Webinar Slides
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
 
this resume for sadika shaikh bca student
this resume for sadika shaikh bca studentthis resume for sadika shaikh bca student
this resume for sadika shaikh bca student
 
Blockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre timesBlockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre times
 

‘Hello, world!’ application how to dockerize golang application

  • 3. Do you find getting started with Docker intimidating? Do you want to take the first step and successfully dockerize golang application? Are you having trouble finding a basic step-by-step tutorial to dockerize golang application? All these questions and just one answer – If yes, you’ve chosen the correct tutorial! The purpose of this tutorial is to get your hands on Docker for the starters. In this guideline, we will build a ‘Hello, World’ application and follow simple steps to dockerize the golang app. Without further ado, let’s get started with our tutorial.
  • 5. Before building the application, make sure your system is installed with docker and golang. If not, then visit the links below to install. ⦿Install Docker ⦿Install Golang
  • 7. First of all, let’s create the main.go file and initialize the application using the command go mod init Our project structure will look like this- sample-dockerize-app |– main.go |– Dockerfile Our main.go will contain the following code. package main import ( "encoding/json" "fmt" "log" "net/http" "github.com/gorilla/mux" )
  • 8. func main() { router := mux.NewRouter() router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { response := map[string]string{ "message": "Welcome to Dockerized app", } json.NewEncoder(rw).Encode(response) }) router.HandleFunc("/{name}", func(rw http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) name := vars["name"] var message string if name == "" { message = "Hello World" } else { message = "Hello " + name } response := map[string]string{ "message": message,
  • 11. As the Docker documentation says- An image includes everything needed to run an application- the code or binary, runtimes, dependencies, and any other filesystem objects required. An image consists of your app definition and everything needed to run the application in the simplest terms. For creating a docker image, you need to write steps in the config file. The conventional and most preferred file name is Dockerfile, but you can use any name of your choice. However, in my opinion, it’s always better to follow standards. Now, create a file named Dockerfile and write the following code.
  • 12. As the Docker documentation says- An image includes everything needed to run an application- the code or binary, runtimes, dependencies, and any other filesystem objects required. An image consists of your app definition and everything needed to run the application in the simplest terms. For creating a docker image, you need to write steps in the config file. The conventional and most preferred file name is Dockerfile, but you can use any name of your choice. However, in my opinion, it’s always better to follow standards. Now, create a file named Dockerfile and write the following code.
  • 13. # Build Stage # First pull Golang image FROM golang:1.17-alpine as build-env # Set environment variable ENV APP_NAME sample-dockerize-app ENV CMD_PATH main.go # Copy application data into image COPY . $GOPATH/src/$APP_NAME WORKDIR $GOPATH/src/$APP_NAME # Budild application RUN CGO_ENABLED=0 go build -v -o /$APP_NAME $GOPATH/src/$APP_NAME/$CMD_PATH # Run Stage FROM alpine:3.14 # Set environment variable ENV APP_NAME sample-dockerize-app
  • 14. # Copy only required data into this image COPY --from=build-env /$APP_NAME . # Expose application port EXPOSE 8081 # Start app CMD ./$APP_NAME Once done with the file, run the below command to update your application’s dependencies go mod tidy
  • 16. Use the below command to build the docker image. docker build --rm -t golang-docker-example . After executing the command successfully your screen will display following output. This command will build the image and tag it with the name golang-docker-example. For now, it will just build the image and won’t do anything. The next time would be running the image for handling requests. The running image is what we call a container.
  • 17. Use the below command to build the docker image. docker build --rm -t golang-docker-example . After executing the command successfully your screen will display following output. This command will build the image and tag it with the name golang-docker-example. For now, it will just build the image and won’t do anything. The next time would be running the image for handling requests. The running image is what we call a container.
  • 18. Use the following command to run the image. docker run -p 8081:8081 golang-docker- example Successfully executed! The -p flag is used for defining the port binding. Our application inside the container will be running on the 8081 port and we will bind it to the host port, i.e., 8081. You can use a different port to bind as well. For that, you can run -p $HOST_PORT:8081, for example -p 3000:8081.
  • 19. Once you are done, Voila! Your application will be running on port 8081. You have now successfully dockerized golang app. Open the browser and hit http://localhost:8081 to check your application. Your browser will display You can also run http://localhost:8081/{name}* and replace {name}* with anything that will print the message having the variable.
  • 21. You can run the below command to clone the github repo to play around with the code, and feel free to visit the golang docker example github. git clone https://github.com/DekivadiyaKishan/gola ng-docker-example.git
  • 23. So, this was a beginner tutorial on how to dockerize golang application. I hope the tutorial has helped you to at least get started with Docker. If you are a Golang enthusiast and want to explore Golang, please visit the Golang tutorials page. If you are looking for a skilled developer who can help you with your project requirements, contact Bacancy and hire golang developer.