Docker

How do I Enable Docker Compose Service?

Docker Compose is a well-known tool utilized for defining and executing numerous containers as a single service. Users can define the configuration for various Docker containers like images, volumes, ports, dependencies, etc., in one compose file using Docker Compose. It is helpful for developing and managing multi-component complicated applications. For this purpose, developers need to enable the Docker Compose service in their projects.

This article will demonstrate the procedure for enabling Docker Compose services.

How do I Enable Docker Compose Service?

To enable the Docker Compose service, follow the below-provided instructions:

  • Create a Compose file and specify the required services.
  • Enable the Docker Compose service using the “docker-compose up -d” command.
  • Verify enabled Compose service via the “docker ps” command.

Step 1: Create Compose File
First, create a “docker-compose.yml” file on Visual Studio Code and configure the desired services. For example, we have defined the below-mentioned services:

version: '3'
services:
  nginx:                    //first service
    image: nginx:latest
    ports:
      - "80:80"

  redis:                   //second service
     image: redis

In the above code:

  • version” defines the Docker Compose file’s version i.e., “3”.
  • services” key specifies the compose services. In our case, we have defined two services i.e., “nginx” and “redis”.
  • nginx” is the name of the first service that is using the “nginx:latest” Docker image and port “80:80”.
  • redis” is the name of the second service that is utilizing the “redis” Docker image.

This will pull the latest versions of the Nginx and Redis images and set them up to run within separate containers. The Nginx container is using the “80:80” port that allows to access the Nginx web server, while the Redis container uses the default Redis port within the container:

Step 2: Enable the Compose Service
Then, enable the Docker Compose service via the below-listed command:

docker-compose up -d

This command has enabled the Docker Compose service and the containers have been built and started.

Step 3: Verify Enabled Service
Next, type out the provided command to ensure that the Docker Compose service has been enabled or not:

docker ps

In the above output, two running containers can be seen which indicates that the Docker Compose service has been enabled successfully.

Bonus Tip: Disable the Compose Service
To disable the Docker Compose service, execute the following command:

docker-compose down

It can be observed that the Docker containers and network have been stopped and removed.

Conclusion

To enable the Docker Compose service, first, create a Compose file on Visual Studio Code and define the desired services. Next, utilize the “docker-compose up -d” command to start/enable the Compose service. Finally, verify the enabled Compose service via the “docker ps” command. This article has demonstrated the method for enabling Docker Compose services.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.