Where did this offensive slang term originate?

We’ve been watching a few Vietnam war films in the house recently and of course the term “gook” has sprung up quite often in them with reference to the inhabitants of Vietnam. Where did this term come from? Was it made up by US servicement during the war or before that?

The Wordwizard Clubhouse - Gook