English is not unambiguous, so there is no absolute rule. However, generally speaking the last noun is modified by the preceding nouns. "Sofa box container", has container as the last word, so it refers to a container, and the type of container is one for a sofa box.
When you hyphenate the meaning doesn't change much, so, from your example:
Queen-killer — a queen who is also a killer
Is not correct, a "queen-killer" is a person who kills queens. The hyphen just tightens the relationship.
The emphasis is the same with the genitive but the relationship is a little more ambiguous:
Queen's killer
Can mean a lot of things, the two most obvious candidates being a killer who works for the queen or a person who killed the queen. Nonetheless, the rule still applies, it is a killer, and the type of killer is "queen's".
So the bottom line is that usually the last word is the main word, the rest are modifiers.
For your example, database containing machines would be appropriate, since the main subject is the machine, and the rest say what type of machine. In this particular instance though the idiom would be just plain database machines, or database servers.
In English there's no such thing as a plural form for an adjective or a preposition. So if you have a compound noun, like "court martial", to make it plural you should pluralize the noun and not the adjective. Hence, "courts martial". There aren't two martials; there are two courts.
Similarly, "passersby" make sense because there are two "passers", not two "byes". "Two spoonsful" because there are two spoons that are full, not two fulls that are spoon. Etc.
But "flyby" is in a different category. It is a noun formed by combining a verb and a preposition. There are not two "byes", but neither are there two "flies". Similarly with "takeover" and "shutout". There is no "inner noun" in these cases to pluralize. So it is the combined word as a whole that is a noun, and it makes sense to pluralize it as a whole word and not as a compound.
Best Answer
English can use attributive noun phrases in place of many expressions with "of." For example, "her box of poems" can become "her poem box". Linguists don't like to model this by saying that poem has become an adjective because of the way they interact with real adjectives. E.g. "Her Elizabethan poetry box." Even without the hyphen, most readers just can't accept that Elizabethan modifies box. So "Poem boxes lined her shelves" would indeed begin with two nouns, if you follow that model.