One of the most powerful tools in the Docker ecosystem is the Docker Compose utility. Docker is a command-line utility with a default installation that allows us to define and run multiple containers from a single entity.
Using Docker Compose, we can define all the services, tools, and dependencies we need to run a given application using YAML in a file called docker-compose.yml.
We can invoke the docker-compose tool and point it to the defined file. The Docker engine will then use all the instructions defined in the file to start all the required services and deploy the applications.
In this tutorial, we will explore the workings of the docker-compose exec command. We will start by describing what the command does and the supported parameters, and then provide some basic examples of using the commands in real docker examples.
Command Usage
The docker compose exec command allows us to execute a given command in the shell of a running container. For example, you can use the docker exec command to start or stop a defined service in the container automatically.
The following shows the syntax of the docker-compose exec command:
The command parameters are as expressed below:
- OPTIONS – used to specify the action or behavior of the exec command. You can check the supported options in the section below:
- SERVICE – specifies the service name defined in the docker-compose.yaml file.
- COMMAND [ARGS…] – this is used to specify the command and any arguments you wish to execute inside the container of the specified service.
Command Options
The following are the supported options you can specify when running the exec command:
- –detach or -d – This tells the docker engine to run the command in the background or in detached mode.
- –env or -e – Used to set a given environment variable or multiple environment variables.
- –index – This specifies the index of the container if the service has multiple replicas.
- –no-TTY or -T – This option disables pseudo-TTY allocation. By default, the docker compose exec command will allocate a TTY to the specified command.
- –user or -u – Specifies the user that should run the specified command.
- –workdir or -w – Specifies the path to the workdir directory for the command.
- –dry-run – This tells docker to run the command in dry run mode.
Example 1 – Accessing a Shell Inside Container
Let us start by demonstrating how to use the exec command inside a container. Suppose we a service called hello defined in the docker-compose.yml file. We can open a shell within the container using the exec command, as demonstrated below:
Example 2 – Running Command as Different User
We can also run a command inside the container as a different user by using the -u parameter. An example command is as shown:
In this case, we tell the docker compose command to run the command echo “hello world” as the linuxhint user in the hello service of the container.
Conclusion
In this post, we learned how to configure and use the docker compose exec command to interact with and execute commands in the services defined in the docker-compose.yml file of a given container.