A function $f:[0,\pi]\to\mathbb{R}$ can be written as $$f(x)=\frac{a_0}{2}+\sum_{k=1}^\infty a_k\cos(kx),$$ where $$a_k=\frac{2}{\pi}\int_0^\pi f(x)\cos(kx)dx.$$
A substitution can shift the domain $[0,\pi]$ to any arbitrary finite interval.
Suppose we consider a function $f:[0,\infty)\to\mathbb{R}$ with unbounded domain. Can we find a cosine series for $f$ on this non-compact domain (we may assume that $f$ has exponential decay)?
It is not clear from your question what you are trying to do. I think you have three options:
Use a change of variable such as $\theta = 2 \tan^{-1} x$ which transforms $[0,\infty)$ to $[0,\pi)$. Then for a function $f:[0,\infty)$ you could obtain the cosine series for $f(\tan(\theta/2))=a_0/2 + \sum a_n \cos n \theta$. This results in a cumbersome expansion, $$f(x) = \frac{a_0}{2} + \sum_n a_n \cos (2n \tan^{-1} x),$$ where the expression for coefficients is, $$ a_n = \frac{2}{\pi}\int_0^\infty f(\tan \theta / 2) d\theta .$$ Any other one to one function that maps $[0,\infty)$ onto $[0,\pi)$ would also work.
Settle for a large but finite interval using the method you already described.
Use another form of expansion tailored to an infinite domain. One such is obtained using the Laguerre polynomials, that form a basis on $[0,\infty)$. There is plenty of information on these. They can be used to create an infinite convergent series for suitably behaved functions $f$.