The most effective solution I've found to situations where my brain is not cooperating is to develop a routine and to make sure my environment is conducive to whatever it is I feel I should be doing.
I used to have trouble with insomnia. I have much less trouble now that I have a set time when I usually go to bed. We put up room darkening curtains, made the bed super comfy, and I no longer take my tablet/phone to bed with me. If I'm going to bed, I'm going to sleep, not to read. About a half hour before "bed time", I read something light that lets my brain wind down and right before bed I have an end-of-the-day routine I won't bore you with. The result is that I've conditioned myself to be ready to sleep when I go to bed.
It's easy to do everything except work when you're working from home. I've found that just trying to exert my will and try really hard to be focused is just like trying really hard to get to sleep when I'm having a bout of insomnia... I end up doing more of the behavior I'm trying to stop. Setting a routine for myself and setting up an environment where it's easy to stay focused works better, and has the side effect that I don't have to beat myself up for not having enough willpower.
All of these suggestions are meant to be "within reason". If you decide you're going to start working at 8 AM every day, you're not a failure if one day you start at 8:15 AM. The point is to train your brain to expect certain activities at certain times, not send yourself to bootcamp.
Step one: Get on a schedule. You should have a time that you start working, a time you have lunch, and a time you stop working. You should get up at about the same time every morning and have a few things that you do every day before you go to work: have a cup of coffee, have breakfast, read the latest from some media you like, etc. and when you're done doing those things, you go to work. My husband and I tend to the garden, and I do a daily quest for a video game I play. It can be whatever it is you like to do to start your day but it needs to fit between the time you usually wake up every day and the time you go to work. You should not roll out of bed and go straight to work. You should allow yourself at least 30 minutes of activities you do every morning.
On days I'm working from home, I start work at the end of the routine. On days where I have to go into the office, I start my commute. The point is, after my morning routine is done I'm in "work mode".
Step two: Set up an area where you work at home, and that's all you do there during work hours. If you want to check your social media, you should have to get up from that area and go somewhere else. It's easy for me because I have a work laptop that I can't do personal stuff on. If you are connecting with your personal computer, consider setting up a desktop or even a separate account that is configured just for work-related stuff.
Get yourself a comfortable chair, good lighting, a nice keyboard and mouse... set it up so that it feels like an office and you are comfortable working there. I realize in a small apartment, there may not be enough space to dedicate to just work. Do what you can to try to have a spot with a proper desk and chair and try to reserve it for work-like activities.
Step three: Try it for a week and pay attention to when you lose focus. I found around a certain time mid-morning I get restless, so that's now my scheduled break time and I get up, make some tea and get a snack. Notice what distracts you in your environment and make it harder to get to. I'm pretty good at ignoring my phone, but if you aren't, you might consider silencing it during working hours and setting up "VIP" contacts whose calls will always ring.
What will work is different for everyone. Try out a specific routine and environment, pay attention to what is working for you and what is not then keep adjusting it until you find the right balance.