You need to learn, from experience, that "friends" and "colleagues" are two different categories. Both of them useful, but each in a different way. And some people can belong to both categories, but that's an exception, not a rule.
Good colleagues are very important. They are the people you are going to spend with half of the time when you are not sleeping. You will spend more time with them than with anyone else, including the people who live with you. So how you interact with them can make your days pleasant or miserable for the following few years.
Even after you leave the job, if you stay in contact (like, having a lunch together once in a year), you can give each other good professional advice, recommend good jobs, and provide a reference. That can make a big difference, too.
So it is definitely NOT "all in vain". However...
The fact that you spend so much time together and face the same problems, this will all instantly disappear the very moment you leave the job. (It may also evaporate gradually if they leave the job sooner. This part is completely outside your control.) So leaving the job means losing it all, but staying in the job is not a guarantee of keeping it. Better not to get attached. (Being attached also weakens your negotiating position against your employer.) This is the opposite of friendship, when you expect your friends to be there for you especially when you face important changes in your life.
Friends, you choose freely. Colleagues, you get randomly assigned. It is important to be able to have good relationships with people you get randomly assigned. But this is completely different from friendship, where you choose people depending on how much you click with each other, and on the potential for the relationship to grow. If you find out that someone is only capable of a shallow level of friendship, you recognize they are a waste of time, and you replace them with a better friend. When the person happens to be your colleague, you continue to be polite to each other, understanding that this is the level you can't get beyond, but it is still valuable to keep this level. You have different expectations for friends and colleagues. With friends, the relationships start organically, and they grow over time. With colleagues, the relationships start artificially, and they mostly remain at the same level. With friends, you have an emotional connection; you take risks and make yourself vulnerable, in hope of making your connection deeper. With colleagues, you behave professionally, in hope of staying at the same level indefinitely.
(Now, there are situations like having a beer with your colleagues after the work, or a teambuilding organized by your company. They blur the lines; in case of teambuilding it is intentional brainwashing. Don't get confused. Relax and enjoy the situation, but don't forget that you have actual friends outside your work who are not with you now; remember to get in contact with them later.)
Sometimes, you accidentally get randomly assigned a colleague who is actually a friend material. Someone, who you would become friends with, if instead you would have met somewhere else; someone whom you would remember and try to meet again. If the feeling is mutual, great. But it is easy and perhaps tempting to overestimate this. You will probably find the true answer only after you (or the other person) leave this job.