To study turbulent thermal convection, one often chooses a Rayleigh-Bénard flow configuration, where a fluid is confined between a heated bottom plate, a cooled top plate of the same shape, and insulated vertical sidewalls. When designing a Rayleigh-Bénard setup, for specified fluid properties under Oberbeck-Boussinesq conditions, the maximal size of the plates (diameter or area), and maximal temperature difference between the plates, Δmax, one ponders: Which shape of the plates and aspect ratio Γ of the container (ratio between its horizontal and vertical extensions) would be optimal? In this article, we aim to answer this question, where under the optimal container shape, we understand such a shape, which maximizes the range between the maximal accessible Rayleigh number and the critical Rayleigh number for the onset of convection in the considered setup, Rac,Γ. First we prove that Rac,Γ∝(1+cuΓ−2)(1+cθΓ−2), for some cu>0 and cθ>0. This holds for all containers with no-slip boundaries, which have a shape of a right cylinder, whose bounding plates are convex domains, not necessarily circular. Furthermore, we derive accurate estimates of Rac,Γ, under the assumption that in the expansions (in terms of the Laplace eigenfunctions) of the velocity and reduced temperature at the onset of convection, the contributions of the constant-sign eigenfunctions vanish, both in the vertical and at least in one horizontal direction. With that we derive Rac,Γ≈(2π)4(1+cuΓ−2)(1+cθΓ−2), where cu and cθ are determined by the container shape and boundary conditions for the velocity and temperature, respectively. In particular, for circular cylindrical containers with no-slip and insulated sidewalls, we have cu=j112/π2≈1.49 and cθ=(j̃11)2/π2≈0.34, where j11 and j̃11 are the first positive roots of the Bessel function J1 of the first kind or its derivative, respectively. For parallelepiped containers with the ratios Γx and Γy, Γy≤Γx≡Γ, of the side lengths of the rectangular plates to the cell height, for no-slip and insulated sidewalls we obtain Rac,Γ≈(2π)4(1+Γx−2)(1+Γx−2/4+Γy−2/4). Our approach is essentially different to the linear stability analysis, however, both methods lead to similar results. For Γ≲4.4, the derived Rac,Γ is larger than Jeffreys' result Rac,∞J≈1708 for an unbounded layer, which was obtained with linear stability analysis of the normal modes restricted to the consideration of a single perturbation wave in the horizontal direction. In the limit Γ→∞, the difference between Rac,Γ→∞=(2π)4 for laterally confined containers and Jeffreys' Rac,∞J for an unbounded layer is about 8.8%. We further show that in Rayleigh-Bénard experiments, the optimal rectangular plates are squares, while among all convex plane domains, circles seem to match the optimal shape of the plates. The optimal Γ is independent of Δmax and of the fluid properties. For the adiabatic sidewalls, the optimal Γ is slightly smaller than 1/2 (for cylinder, about 0.46), which means that the intuitive choice of Γ=1/2 in most Rayleigh-Bénard experiments is right and justified. For the given plate diameter D and maximal temperature difference Δmax, the maximal attainable Rayleigh number range is about 3.5 orders of magnitudes smaller than the order of the Rayleigh number based on D and Δmax. Deviations from the optimal Γ lead to a reduction of the attainable range, namely, as log10(Γ) for Γ→0 and as log10(Γ−3) for Γ→∞. Our theory shows that the relevant length scale in Rayleigh-Bénard convection in containers with no-slip boundaries is ℓ∼D/Γ2+cu=H/1+cu/Γ2. This means that in the limit Γ→∞, ℓ equals the cell height H, while for Γ→0, it is rather the plate diameter D.