Run your Go application with Docker

Tushar Sheth
2 min readDec 18, 2020
Shanghai port
Sanghai Port

When I started working with Golang, I searched about how to create docker images for my apps and how to run them. Creating Go Web application and deploying them with docker is as easy as it gets. You need just two things.

  1. Go App
  2. Dockerfile

1. Create a Go web Server:

Go web server code:

main.go

2. Docker file:

Dockerfile is the blueprint of docker image. It tells which type of application we are building and what are its necessory components.

Dockerfile

3. Build docker image

docker build -t myapp .

This command creates a docker image named myapp. We can check generated image with command docker images. It will show generated docker image details (name, id, tag, size, relative time)

4. Run docker container

Now we have our docker image running. We need to run it on our system on port 8080.

docker run --rm -it -p 8080:8080 myapp
  • — rm: causes docker to automatically remove the container when it exists.
  • -it: run docker container in interactive mode
  • -p: port specification

You should see the console message: server is listening on port: 8080.

Verify setup:

To verify our app is running or not, go to http://localhost:8080/myapp or run command

curl http://localhost:8080/myapp

Output would be: Hello, “/myapp”%

Code:

The code mentioned above is hosted at: https://github.com/TusharSheth/go101

--

--

Tushar Sheth

Creator. Automation Enthusiast. I love making things simple and abstract.