After extensive testing I have found out that the problem is with the level on which the door is in relation to the surrounding. Each door needs to have at least 4 blocks in each direction on the same level as the door itself.

    0000000000000
    001111XXXXXX0
    001111X1111X0
    001111X1BB1X0
    001111D1111X0
    001111X1111X0
    001111X1111X0
    001111X1111X0
    001111XXXXXX0
    0000000000000

`0`,`1` - ground levels, `X` - wall, `B` - bed, `D` - door