The problem here is that you've neglected the effect of firing the bullet on the plane itself. It turns out we can account for the bullet's extra energy by examining the energy lost by the plane. To resolve this apparent contradiction, we need to examine the problem in terms of conservation of momentum:
In the second case, where the bullet is fired from a gun on the plane, conservation of momentum gives us $$m_b v_0 + m_p v_0 = m_b (2 v_0) + m_p v_1$$ $$v_1 = \frac{m_p - m_b}{m_p} v_0$$ You can see that the velocity of the plane is slightly reduced after the bullet is fired. 771
This must mean that the plane has lost some kinetic energy, too:
$$\Delta KE_p = KE_{p, 1} - KE_{p, 0}$$ $$\Delta KE_p = \frac 1 2 m_p v_1^2 - \frac 1 2 m_p v_0^2$$ $$\Delta KE_p = \frac 1 2 m_p (\frac{m_p - m_b}{m_p} v_0)^2 - \frac 1 2 m_p v_0^2$$ $$\Delta KE_p = \frac 1 2 m_p (\frac{m_p - m_b}{m_p} v_0)^2 - \frac 1 2 m_p v_0^2$$ $$\Delta KE_p = \frac 1 2 \frac{(m_p - m_b)^2}{m_p} v_0^2 - \frac 1 2 m_p v_0^2$$ $$\Delta KE_p = \frac 1 2 (-2m_p + m_b)\frac{m_b}{m_p} v_0^2$$
Here we can simplify this a bit by assuming that $2 m_p >> m_b$:
$$\Delta KE_p = -2 \frac 1 2 m_b v_0^2$$ $$\Delta KE_p = -2 KE_{b, 0}$$
That's the solution there - the amount of energy that the plane loses is equal to twice as much as the bullet started with. So in your situation, the bullet started out with 1 MJ of energy, and the plane loses 2 MJ of energy, which accounts for the anomalous extra 2 MJ of energy that the bullet gains.
Note: If you are concerned about that last approximation we made, consider that we're just making up for the fact that we already cheated when we said the bullet doubled its speed when we fired it from the plane - that can't be true if we are to conserve momentum. This approximation is just making up for that previous unacknowledged approximation. 512