乐闻世界logo
搜索文章和话题

How to restart a single container with docker- compose

1个答案

1

When managing multiple containers with Docker Compose, you may need to restart a single container. This is frequently used during development to apply the latest code changes or adjustments. The following are the steps to restart a single container using Docker Compose:

  1. Ensure your terminal is in the directory containing the docker-compose.yml file.

  2. Locate the service name in the docker-compose.yml file. In the docker-compose.yml file, find the service name corresponding to the container you want to restart. For example, if the file defines a web service, it might look like this:

    yaml
    version: '3.8' services: web: image: nginx ports: - "80:80" db: image: postgres ports: - "5432:5432"

    In this example, if you want to restart the web service, web is the service name you need.

  3. Use the Docker Compose command to restart the container. Use the following command format to restart the specified service:

    bash

docker-compose restart [service_name]

shell
For the above example, run: ```bash docker-compose restart web

This command stops the container for the web service and restarts it immediately.

Example:

Assume I am developing a small application using Flask and Redis. The docker-compose.yml file content is as follows:

yaml
version: '3.8' services: web: build: . ports: - "5000:5000" volumes: - .:/code depends_on: - redis redis: image: "redis:alpine"

During development, I made some changes to the Flask application and want to restart the web service to apply these changes. I run the following command in the directory containing docker-compose.yml:

bash
docker-compose restart web

This command restarts the web service without affecting the redis service.

This method is very useful for developers as it allows quick restart of specific services without interrupting other parts of the environment.

2024年8月10日 00:53 回复

你的答案