This guide explains how to use a Docker-based Go Development OS
129
This guide explains how to use a Docker-based Go Development OS, which includes Golang, Git, and Vim for coding. You will learn how to build, run, and use ports for accessing applications.
If the image is available on Docker Hub, pull it using:
docker pull priyanshuksharma/go-dev-os:latest
This downloads the image locally, so you don’t have to build it manually.
To start the container with an interactive shell:
docker run -it --rm priyanshuksharma/go-dev-os:latest
This will drop you into a bash shell, where you can write and run Go code.
By default, any files created inside a container are lost when it stops. To save your code, mount a local folder:
docker run -it --rm -v $(pwd):/workspace priyanshuksharma/go-dev-os:latest
/workspace./workspace will be saved on your host machine.If you're running a Go web application, you need to expose ports.
docker run -it --rm -p 8080:8080 priyanshuksharma/go-dev-os:latest
-p 8080:8080 flag maps port 8080 inside the container to port 8080 on your machine.localhost:8080), you can access it from your browser.Inside the running container, create a file server.go:
nano server.go
Paste this code:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go in Docker!")
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Server running on port 8080...")
http.ListenAndServe(":8080", nil)
}
Run it:
go run server.go
Now, open http://localhost:8080 in your browser.
To keep the container running in the background:
docker run -d -p 8080:8080 --name go-container priyanshuksharma/go-dev-os:latest
-d flag runs it in the background.--name go-container gives it a name.To stop it later:
docker stop go-container
If the container is running in the background, you can access it using:
docker exec -it go-container bash
After building your image, you can push it to Docker Hub for easy access:
docker login
docker push priyanshuksharma/go-dev-os:latest
| Action | Command |
|---|---|
| Pull the image | docker pull priyanshuksharma/go-dev-os:latest |
| Run interactive shell | docker run -it --rm priyanshuksharma/go-dev-os:latest |
| Mount local directory | docker run -it --rm -v $(pwd):/workspace priyanshuksharma/go-dev-os:latest |
| Run with port | docker run -it --rm -p 8080:8080 priyanshuksharma/go-dev-os:latest |
| Run in detached mode | docker run -d -p 8080:8080 --name go-container priyanshuksharma/go-dev-os:latest |
| Stop container | docker stop go-container |
| Access running container | docker exec -it go-container bash |
| Push image to Docker Hub | docker push priyanshuksharma/go-dev-os:latest |
Content type
Image
Digest
sha256:e29992901…
Size
243 MB
Last updated
over 1 year ago
docker pull priyanshuksharma/go-dev-os