In recent years, U-Net has been widely utilized for the segmentation of medical biological images, demonstrating favorable outcomes. However, determining the optimal U-Net network structure for different datasets remains a challenge, often requiring an extensive architecture search or inefficient integration of various deep models for testing purposes. In this paper, we propose an automatic U-Net network design algorithm, U-Net-GA, based on the genetic algorithm. The algorithm effectively addresses the image discrimination task through the introduction of a new variable-length coding strategy, acceleration components, and genetic operators. The key advantage of the proposed algorithm lies in its "automatic" nature, enabling users to obtain the optimal U-Net network structure for a given image without requiring U-Net domain knowledge. The algorithm's effectiveness is demonstrated by its application to two different types of medical image dataset, namely, colorectal cancer and COVID-19 CT images, and a subsequent comparison with other advanced network structures. Experimental results demonstrate that the proposed algorithm exhibits superior performance compared with existing U-Net networks in terms of segmentation accuracy, Dice coefficient, Jaccard index, and loss index.