I've never had luck keeping them out of food.
Sealed containers work, but cause fruit to ripen and spoil quickly, and they are small enough to get in anything with an opening.
However, a simple fruit fly trap can keep them in check:
Cut a 2l soda bottle where the top taper ends (above the label).
Add a few drops dish soap to a few tbsp of apple juice, beer or cider vinegar.
Pieces of fruit can also be used.
Invert the severed top of the bottle (sans cap), and seal with a piece of duct tape around the edge.
Set near the fruit and wait.
Open it up to empty every few days.
Google will give you a variety of other designs that you can build if you are more ambitious.
cooking.stackexchange.com/users/624/Adam Shiemke
Flies can lay eggs and continue to emerge even after the produce that attracted them in the first place is already gone. After getting rid of the produce the next time, wipe the area down with a kitchen cleaning spray and wait to see if more flies appear.
If not, then give it a go again and keep an eye on the progression of the produce, refrigerating it if it is getting softer than you expect before you get around to using it.
Oftentimes the fruit flies are traveling with the produce (microscopic eggs) so try washing things like tomatoes, peaches, etc.
first before setting out on the counter/shelves.
cooking.stackexchange.com/users/426/Darin Sehnert
Mesh cover.
http://www.calibex.com/food-mesh-cover/zzcalibex1zB1z0--search-html?nxtg=23f30a1c0520-A5A11C9A30B6FFB3 http://www.thekitchenstore.com/030734063118.html http://www.amazon.com/Handy-Gourmet-Set-Food-Covers/dp/B000EYFV4M/ref=sr_1_7?ie=UTF8&s=home-garden&qid=1280514882&sr=1-7
cooking.stackexchange.com/users/1443/Ocaasi
I personally am using balsamic vinegar and dish soap.
Because I have so many tomatoes and peppers all at once they are laying on the counter
so I have put this into tiny plastic bowls (2-3 in diameter from the dollar store) and placed them among the fruit.
So far it seems to help.
There are still a few but nothing like it was.
cooking.stackexchange.com/users/39239/Gail Lyons
Place the produce on the counter-top or on a plate, and then, use a upside-down "mesh-type" colander as a lid to keep the produce covered.
Flies wont be able to get in, but there will be enough ventilation.
Depending on the produce, you can obviously also store it in the fridge (not tomatoes).
cooking.stackexchange.com/users/470/joyjit
This is a trick I've learned since I home-brew kombucha- same method of keeping flies out.
Get a larger bowl or a pot and put the fruit in gently.
Then throw a kitchen towel over the top and secure with a couple tight rubber bands around the top of the pot/bowl.
Make sure no fruit flies are on the fruit when the rubber band is secured.
This should keep them out, I'd just check the fruit once a day to check on ripeness and when the fruit is ripe, move it to the fridge.
I currently have 3 bowls going right now after my husband came home with 2 large crates one of peaches and one of plumbs.
I have probably 45 plumbs in one container and 15 in each of the others, and so far all is fine and happy- fly free!
cooking.stackexchange.com/users/67625/meghan
What is a good way of keeping fresh produce (in particular, from the garden) and herbs in the kitchen without attracting fruit flies? We dispose of tomatoes etc as soon as they start to go bad, but it seems like leaving out the produce that would be kept out is attracting fruit flies.  There weren't fruit flies before so I am wondering if there might be a better way (ie a ventilated canister?) of keeping the produce?
Fruit Flies and Storage of Fresh Produce
cooking.stackexchange.com
<food-safety><storage-method><produce>
cooking.stackexchange.com/questions/3780
cooking.stackexchange.com/users/15/mfg
Covering the produce with a mesh cover or using a simple fruit fly trap should keep them in check. Try using a fruit fly trap or covering the produce with a mesh cover.
Try using a fruit fly trap or covering the produce with a mesh cover.