When should you use the docker-compose's "--service-ports" flag?

Docker is one of the terms that we hear at least once in the course of our lives. Either it’s referring to a person related to the docks or the well-renowned platform for packaging and running an application.

Docker is an open platform that enables us to distinguish applications from infrastructure and is used for standardizing the environments for development. This means having an application that isn’t bound to a specific infrastructure and can be considered portable. These applications are then packaged with their necessary dependencies to execute in a lightweight, Container structure. Lightweight containers enable us to execute multiple containers on a single host system. 

Simple illustration explaining the concept of Docker.
Simple illustration explaining the concept of Docker.

Note: To go in-depth on how we use Docker, visit this Answer.

What is Docker Compose?

As mentioned, we can see how different applications can be executed on a host system. But what if we need multiple applications that interlink to execute the needed application? This would lead us to start each individual application, which can be considered tedious and inefficient. Docker Compose is a tool specifically developed to resolve the problem at hand. The central concept that enables us to overcome the problem is called Multi Containers. 

Illustration of how a single service in Docker Compose is comprised of multiple containers.
Illustration of how a single service in Docker Compose is comprised of multiple containers.

For example, we need two different containers interlinked for a specific project. This could be solved through multi containers that create a single image A self-contained software package including all necessary dependencies and configurations to run a specific application or service., starting each container as a serviceA containerized component of an application or system defined in a YAML file for easier orchestration of multiple containers..

What do the flags mean?

When we execute the docker-compose command, we use different flags to customize the Docker Compose operations and how the service works. These flags are passed through as arguments, which will affect the outcome. We can look at the structure of the ‘docker compose’ command and the most common flags used. 

docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]

Flags most commonly used:

  • -f: Specify the path to the docker-compose.yml file (can be used with multiple files).

  • -p: Specify an alternate project name (overrides the directory name).

  • Up: Create and start containers.

  • Down: Stop and remove containers, networks, and volumes.

  • Start: Start services.

  • stop : Stop services.

  • restart: Restart services.

  • build: Build images before starting containers.

  • pull: Pull service images.

  • logs: View output logs from services.

  • ps: List running containers.

What is the ‘--service-ports’ flag, and why do we use it?

The --service-ports flag is used in conjunction with the ‘run’ command. Let’s see what the run command does and how it is implemented.

Implementation of run command

docker compose run [OPTIONS] <SERVICE> [COMMAND]
  • [options]: We use this argument to pass options and other flags to modify the result of the command.

  • <service>: We pass the name of the service defined in the Docker Compose configuration.

  • [command]: We use this argument to pass the command we want the service container to execute.

docker-compose run: command is a one-time command against a service. We can further break this down to understand that this command executes a specific command within a service container. Moreover, the container is stopped and removed after the command completes its execution, explaining the term ‘one-time command'.

How does the run command relate to the ‘--service-ports’ flag?

In the run command, Docker does not create any ports specified in the service configuration. This is done to prevent port collisions with the already-opened ports. However, if you want the ports to be created and mapped to the host system ports, we use the ‘--service-ports’ flag.

A further explanation tells us that this refers to exposing and accessing the ports of a service running in a Docker container on the host system.

Implementation

docker compose run --service-ports <SERVICE> [COMMAND]

 

Lets take a small quiz to build a stronger understanding.

Assessment

Q

What is Docker Compose?

A)

A containerization platform

B)

A tool for managing multiple containers

C)

A programming language

D)

A version control system

Conclusion

Docker Compose is a tool to alleviate the problems faced when multiple containers have to be started for one specific service. There are a multitude of flags and other arguments that can modify the behavior of the commands to the user’s liking. 

  

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved