As with most things, this is more clear in the book (emphasis mine):
'It is mine, I tell you. My own. My precious. Yes, my precious.'
The wizard’s face remained grave and attentive, and only a flicker in his deep eyes showed that he was startled and indeed alarmed. 'It has been called that before,' he said, 'but not by you.'
'But I say it now. And why not? Even if Gollum said the same once. It's not his now, but mine. And I shall keep it, I say.'
Fellowship of the Ring Book I Chapter 1: "A Long-Expected Party"
Bilbo had told Gandalf the true story of how he obtained the Ring from Gollum:
I wondered often how Gollum came by a Great Ring, as plainly it was - that at least was clear from the first. Then I heard Bilbo’s strange story of how he had “won” it, and I could not believe it. When I at last got the truth out of him, I saw at once that he had been trying to put his claim to the ring beyond doubt.
Fellowship of the Ring Book I Chapter 2: "The Shadow of the Past"
Presumably, Bilbo also mentioned Gollum's nickname.