This is fairly standard approximation theory, I think. Suppose we fix some polynomial degree, $n$. The set of polynomials of degree $n$ having zero first derivatives at start and end is a vector subspace, $S$. We are asking for the polynomial $p \in S$ that is "nearest" to our given function, $g$. Nearness can be measured using any reasonable norm. You suggested the $L_2$ norm, but others would work just as well. In many applications, the $L_\infty$ norm would be the most appropriate.
Suppose our norm comes from an inner product $\langle.,.\rangle$. Then a polynomial $p \in S$ is nearest to $g$ if $\langle p-g,q\rangle = 0$ for all $q \in S$. Since $S$ is finite dimensional, this is equivalent to saying that $\langle p-g,q\rangle = 0$ for all $q$ in some finite basis of $S$. This leads to a system of linear equations that you can solve to get the coefficients of $p$. Standard least-squares calculations in the case of the $L_2$ norm.
The $L_\infty$ norm does not come from an inner product, so it's much more difficult to handle. But, that's not what you asked about.