Dopability in semiconductors plays a crucial role in device performance. Using the first-principles density-functional theory calculations, we investigate systematically the doping properties of layered MX2 (M= Mo, W; X=S, Te) by replacing M or X with the groups III, V and VII elements. It is found that the defect BM is hard to form in MX2 due to the large formation energy originating from the crystal distortion, while AlM is easy to realize compared to the former. In MoS2, WS2 and MoTe2, Al is the most desirable p-type dopant under anion-rich conditions among the group III components, since AlM has relatively low transition and formation energies. With respect to the doping of the group V elements, it is found that the substitutions on the cation sites have deeper defect levels than those on the anion sites due to the strong electronegativity. AsTe and SbTe in MoTe2 and WTe2 are trend to form shallow acceptors under cation-rich conditions, indicating high hole-concentrations for p-type doping, whereas SbS in MoS2 and PTe in WTe2 are shown to be good p-type candidates under cation-rich conditions. In despite of that the substitutions of group VII on X site have low formation energies, the transition energies are too high to achieve n-type MoS2 and WS2. Nevertheless, for MoTe2, the substitutions with the group VII elements on the anion sites are suitable for n-type doping on account of the shallow donor levels and low formation energies under Mo-rich condition. As to WTe2, F is the only potential donor due to the shallow transition energy of FTe. Our findings of filtering out unfavorable and identifying favorable dopants in MX2 are very valuable for experimental implementations.