Environmental changes greatly influence the evolution of populations. Here, we study the dynamics of a population of two strains, one growing slightly faster than the other, competing for resources in a time-varying binary environment modeled by a carrying capacity switching either randomly or periodically between states of abundance and scarcity. The population dynamics is characterized by demographic noise (birth and death events) coupled to a varying environment. We elucidate the similarities and differences of the evolution subject to a stochastically- and periodically-varying environment. Importantly, the population size distribution is generally found to be broader under intermediate and fast random switching than under periodic variations, which results in markedly different asymptotic behaviors between the fixation probability of random and periodic switching. We also determine the detailed conditions under which the fixation probability of the slow strain is maximal.