log/partially executing services in docker compose

In Docker Compose, you can selectively execute services defined in your docker-compose.yml file. This is particularly useful during development or troubleshooting when you want to start a specific service without starting the entire stack.

Consider the following docker-compose.yml file:

 1version: "3"
 2
 3services:
 4  webserver:
 5    image: nginx:latest
 6    ports:
 7      - 80:80
 8      - 443:443
 9    restart: always
10    volumes:
11      - ./nginx/conf/:/etc/nginx/conf.d/:ro
12      - ./certbot/www:/var/www/certbot/:ro
13      - ./certbot/conf/:/etc/nginx/ssl/:ro
14  certbot:
15    image: certbot/certbot:latest
16    volumes:
17      - ./certbot/www/:/var/www/certbot/:rw
18      - ./certbot/conf/:/etc/letsencrypt/:rw

Suppose you only want to start the webserver service. You can achieve this by using the following command:

1docker-compose up webserver -d

This command tells Docker Compose to only start the webserver service in detached mode (-d), ignoring other services defined in the docker-compose.yml file. This way, you can efficiently manage and test individual components of your application without starting unnecessary services.