How to Profile Go Code Running in a Container
Aug 01, 2025 am 05:31 AMTo perform performance analysis of Go code in the container, you must first enable pprof and expose the port, and then collect data through go tool pprof. The specific steps are: 1. Import the net/http/pprof package in the Go application and start the HTTP server to expose the debugging interface; 2. Open port 6060 through EXPOSE and -p parameters in the container configuration, or use port-forward in Kubernetes; 3. Use go tool pprof http://localhost:6060/debug/pprof/profile and other commands on the host to obtain performance data such as CPU, heap memory or goroutine; 4. Optionally optimize symbol parsing by copying binary files to the host or providing HTTP access; 5. In production environments, the pprof interface should be avoided, and access can be controlled through authentication, firewall, temporary port forwarding or functional switches; in addition, if the port cannot be exposed, you can enter the container to generate a profile file and copy it to local analysis. The entire process needs to ensure network connectivity and symbol parsability, while taking into account security.
Profiling Go code running in a container is a common need when optimizing performance in production-like environments. While containers add a layer of isolation, Go's built-in profiling tools (via the net/http/pprof
package) make it surprisingly straightforward — as long as you set things up correctly. Here's how to do it effectively.

1. Enable pprof
in Your Go Application
The first step is to expose profiling endpoints using Go's net/http/pprof
package. This doesn't require adding new routes if you're already running an HTTP server — just import the package:
import _ "net/http/pprof" import "net/http" // Somewhere in your app go func() { http.ListenAndServe("0.0.0.0:6060", nil) }()
This automatically registers several profiling endpoints under /debug/pprof/
, such as:

-
/debug/pprof/profile
– CPU profile (30-second default) -
/debug/pprof/heap
– Heap allocation -
/debug/pprof/goroutine
– Goroutine dump -
/debug/pprof/block
,/debug/pprof/mutex
– Contention profiles
? Tip: Use port
6060
or another non-conflicting port. Avoid exposing this on public interfaces in production.
2. Expose the pprof Port in the Container
Make sure the port used by pprof
is accessible from the host. In your Dockerfile
or container runtime config, expose and publish the port:

EXPOSE 6060
When running the container:
docker run -p 6060:6060 my-go-app
If you're using Kubernetes, ensure the port is exposed in the container spec and accessible via port-forwarding:
kubectl port-forward pod/my-pod 6060:6060
Now localhost:6060/debug/pprof/
on your host maps to the container.
3. Collect Profiles from the Host
Once the port is exposed, use go tool pprof
to fetch profiles directly from the containerized app:
CPU Profile (30 seconds)
go tool pprof http://localhost:6060/debug/pprof/profile
Heap Profile
go tool pprof http://localhost:6060/debug/pprof/heap
Goroutine Profile
go tool pprof http://localhost:6060/debug/pprof/goroutine
?? Note: The CPU profile endpoint blocks for 30 seconds by default. Don't use it in production unless you know the impact.
For shorter durations, use query params:
go tool pprof 'http://localhost:6060/debug/pprof/profile?seconds=10'
4. Optimize Symbol Resolution (Optional but Recommended)
By default, pprof
downloads the binary from the container to resolve symbols. If your container doesn't serve the binary over HTTP, you may get:
Fetching binary from target: <url> Failed to fetch symbolized binary
To fix this, either:
- Serve the binary via HTTP (eg, copy it to a public path and serve it)
- Copy the binary to the host and point
pprof
to it:
# Copy binary from container docker cp my-container:/path/to/app ./app-binary # Use it with pprof go tool pprof -binary ./app-binary http://localhost:6060/debug/pprof/heap
Or build your binary with debug symbols and make sure it's accessible.
5. Security and Production Considerations
While pprof
is powerful, exposing it publicly is a security risk:
- Never expose
/debug/pprof
on public endpoints in production. - Use authentication , firewall rules, or restrict access to internal networks.
- In Kubernetes, consider using temporary port-forwarding only during debugging.
- Disable or firewall the pprof port in production images when not needed.
Alternatively, enable pprof only behind a feature flag or in debug builds.
Bonus: Profile Without Exposing Ports (Advanced)
If you can't expose ports, you can still generate profiles from inside the container:
Exec into the container:
docker exec -it my-container sh
Install
curl
andgo
tools (if available), then:curl -o cpu.pprof 'http://localhost:6060/debug/pprof/profile?seconds=30'
Copy the profile out:
docker cp my-container:/tmp/cpu.pprof ./cpu.pprof
Analyze locally:
go tool pprof cpu.pprof
This avoids network exposure but requires shell access.
Profiling Go in containers isn't much different from profiling locally — you just need to wire up the network and manage symbol access. With
pprof
and a bit of container networking, you can get deep insights into CPU, memory, and goroutine behavior.Basically: expose the port, import
net/http/pprof
, and usego tool pprof
against the container's endpoint. Just don't leave it open to the internet.The above is the detailed content of How to Profile Go Code Running in a Container. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Install RedHatPodman on Windows 11 or 10 Follow the steps below to install RedHatPodman on your Windows machine using Command Prompt or Powershell: Step 1: Check System Requirements First, you have to make sure that your Windows system is running with the latest updates so that it can meet the requirements to run Podman requirements. You should be using Windows 11 or Windows 10 version 1709 (Build 16299) or higher and you have to enable Windows Subsystem for Linux 2 (WSL2) and VM features, well if they are not activated yet then you can use The two-step command executes this

According to Huawei’s official news, the Open Atomic Developer Conference, with the theme of “Everything for Developers”, was held in Wuxi for two days, from December 16 to 17. The conference was led by the Open Atomic Open Source Foundation, Huawei, and Inspur. , DaoCloud, Xieyun, Qingyun, Hurricane Engine, as well as the OpenSDV Open Source Alliance, openEuler community, OpenCloudOS community and other member units jointly initiated the construction of the AtomHub Trusted Mirror Center, which is officially open for public testing. AtomHub adheres to the concepts of co-construction, co-governance, and sharing, and aims to provide open source organizations and developers with a neutral, open and co-constructed trusted open source container mirror center. In view of the instability and uncontrollability of image warehouses such as DockerHub, and some

As a lightweight virtualization platform based on container technology, Docker has been widely used in various scenarios. In a production environment, high availability and automatic failure recovery of containers are crucial. This article will introduce how to use Docker for container failure recovery and automatic restart, including specific code examples. 1. Configuration of automatic container restart In Docker, the automatic restart function of the container can be enabled by using the --restart option when running the container. Common options are: no: do not automatically restart. silent

How to sort STL containers in C++: Use the sort() function to sort containers in place, such as std::vector. Using the ordered containers std::set and std::map, elements are automatically sorted on insertion. For a custom sort order, you can use a custom comparator class, such as sorting a vector of strings alphabetically.

The most common container types in C++STL are Vector, List, Deque, Set, Map, Stack and Queue. These containers provide solutions for different data storage needs, such as dynamic arrays, doubly linked lists, and key- and value-based associative containers. In practice, we can use STL containers to organize and access data efficiently, such as storing student grades.

Hello, I am Brother Zheng. WeChat's mini program is a very good experience, simple and quick to use. I have been learning to use mini programs these days. I have summarized three ways to use Python as the backend of mini programs for your reference. Method 1. WeChat cloud hosting [1]. Advantages: No need to purchase a server, no domain name registration required, billing based on usage, DevOps automation, security authentication, suitable for people with no operation and maintenance experience. Disadvantages: The cost is definitely slightly higher than the cost of building a self-built server. Just like the same model, automatic transmission cars are more expensive than manual transmission cars. The so-called cloud hosting is a Docker container. You only need to get a warehouse, which can be any of github, gitlab, and gitee.

The Servlet container is an application that provides the Servlet running environment. It is responsible for managing the life cycle of the Servlet and providing necessary WEB services, such as security, transactions, etc. There are many types of Servlet containers, the most common of which are Tomcat and Jetty. The main functions of the Servlet container are life cycle management: The Servlet container is responsible for managing the life cycle of the Servlet, including startup, initialization, service and destruction. Web services: The Servlet container provides web services, such as security, transactions, etc. Resource management: Servlet container manages resources, such as Servlet, jsP, html pages, etc. Class loading: The Servlet container is responsible for adding

Introduction to how to use Docker to back up and restore containers: When using Docker to deploy containerized applications, we often need to back up and restore containers. Backing up containers can ensure data security, and recovery operations can help us quickly recover problematic containers. This article will introduce how to use Docker to back up and restore containers, and provide detailed code examples. Container backup Container backup can be performed by exporting a container snapshot. Docker provides a tool called do
