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.
Best Answer
Yes, this is grammatical. The first noun is acting like an adjective. As Colin notes, you can't always put a noun in a the same position as an adjective bu it does work as 'N N'.
'Cheese casserole' isn't a casserole that happens to have a lot of cheese (which is what is implied by 'cheesy casserole'); somehow 'cheese ' is essential to the casserole that is a 'cheese casserole'.
The official term for this usage of a noun as a modifier is Noun Adjunct.
It is not an Adjectival Noun which is the other direction, an adjective that acts like a noun.