A Royal Navy sailor escaped a jail sentence today after admitting downloading indecent images of boys while serving as part of a protection force on board an oil platform in the Persian Gulf.

Leading Seaman Andrew Lomax, who has medals for service in Iraq and Northern Ireland and two for good conduct, pleaded guilty at Portsmouth Naval Base court martial centre to three charges of downloading a total of 865 images on to his laptop, mobile phone and portable hard drive.

The photos and movies mostly involved boys aged between 12 and 15, with two in the worst category and another 138 in the second worst category.

The court martial heard that the 31-year-old was serving as part of the joint US-UK Iraqi maritime protection force onboard the Al-Basrah oil platform in the summer of 2010 when he downloaded the images.

The illegal use of the internet onboard was picked up by the US authorities who sent their investigators to the oil platform and Lomax admitted that he was responsible.

Lieutenant Commander David Goddard, prosecuting, said that Lomax had used the search terms ''boys, puberty and paedophile'' on file-sharing website Limewire to download the images as well as from a site called ''Nudist John''.

He said: ''It had started out as a curiosity, then it had built up.

''He said that if he could take a pill to stop his interest in young boys, he would.''

Lieutenant Commander Kara Chadwick, defending, said that Lomax's interest in child pornography came when he was coping with the grief of losing his mother, who died of cancer in February 2010.

She said that his mother had first been diagnosed in 2000 with breast cancer but had gone into remission.

This had returned in 2008 and her condition worsened at Christmas 2009.

She said: ''It was an incredibly hard time for him.

''LS Lomax arranged her funeral after her death.

''He did find it hard to cope and he was suffering significant amounts of grief.

''It (downloading the child pornography) was for him a form of escapism, an attempt to recapture a happier time when he was 13 or 14 when he had his first sexual experience.''

She added that his grief was compounded by the death of his grandfather in July 2010.

She said that Lomax, who is openly gay and in a relationship, had sought counselling after being arrested and admitting the offences in November 2010.

Lt Cdr Chadwick said that the Royal Navy had a firewall in place to prevent sailors from downloading movies and other shared files but Lomax had used software to get round this.

Judge Advocate Robert Hill said that ''being out on an oil rig alone'' had led to the offending.

Lomax was sentenced to a services' community order involving a three-year sexual offender management course and 120 hours of unpaid work.

He was also dismissed from the Navy, banned from working with children and ordered to sign the sex offenders register.

He was also told that if he was to use a computer outside of work in the next five years it must be fitted with software that retains the internet browsing history so it could be inspected by police.

Judge Advocate Hill said that the sexual offender management course was chosen over a period of custody as it was ''the best way that you can address your problem and so that it does not escalate into something more serious''.

He added that the case was not about the defendant being homosexual and that his offences were serious and not about his ''nostalgia'' for previous sexual encounters.