To say "aber and doch mean but" is way too simplified. Both words are words with lots of different meanings:
doch
Adverb
Sie sagte, sie würde daheim bleiben, aber sie ist doch gekommen.
She came allthough she said she would stay at home.
Answer particle
Used to anser a question, containing a negation, if you want to say that the negation is not the case.
Ich habe dir doch nicht weh getan? - Doch, das hast du.
I hope, I didn't hurt you? - Yes, you did.
Modal particle
Modal particles are very common in German, but don't exist in English. Read more about modal particles here (in German) or on Wikipedia in German or in English.
The usual way to translate modal particles is to ignore them.
Mach doch was du willst!
Do what you want!
Conjunction
Ich habe ihn eingeladen, doch er wollte nicht kommen.
I invited him, but he didn't want to come.
part of the phrase »wenn auch - so doch«
Wenn auch die Schauspieler schlecht waren, so hat mir doch das Stück gefallen.
Even if the actors was bad, so I still liked the play.
aber
Conjunction
Ich habe ihn eingeladen, aber er wollte nicht kommen.
I invited him, but he didn't want to come.
Ich habe ihn eingeladen, er aber wollte nicht kommen.
I invited him, but he didn't want to come.
Ich habe ihn eingeladen, er wollte aber nicht kommen.
I invited him, but he didn't want to come.
Modal particle
Der Film war aber heftig!
The movie was violent!
adverb
Outdated and rare, only in the phrase »aber und aber«
Aber und aber schlug die Glocke im Turm.
Again and again the bell stroke in the tower.
Conclusion
Both words can be translated as »but« only when they are used as conjunctions. In this case they are synonyms, so you can freely choose between them.
But as shown above, the conjunction »aber« has a special feature, that the conjunction »doch« doesn't have. In some cases you can move the subject and if you want even the finite verb of the sentence that is following aber in front of aber. This creates an extra focus on that subject:
Alle flüchteten vor dem Feind, doch einige mutige Kämpfer blieben im Dorf.
Alle flüchteten vor dem Feind, aber einige mutige Kämpfer blieben im Dorf.
Alle flüchteten vor dem Feind, einige mutige Kämpfer aber blieben im Dorf.
Alle flüchteten vor dem Feind, einige mutige Kämpfer blieben aber im Dorf.
All fled from the enemy, but some courageous fighters remained in the village.