We investigate properties which ensure that a given finite graph is the commuting graph of a group or semigroup. We show that all graphs on at least two vertices such that no vertex is adjacent to all other vertices is the commuting graph of some semigroup. Moreover, we obtain complete classifications of the graphs with an isolated vertex or edge that are the commuting graph of a group and the cycles that are the commuting graph of a centrefree semigroup.