Docker

Docker Compose Exec

Docker has become one of modern development’s most influential and powerful tools. It offers incredible features that allow developers to configure and run containerized applications in nearly any environment.

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:

docker compose exec [OPTIONS] SERVICE COMMAND [ARGS...]

The command parameters are as expressed below:

  1. OPTIONS – used to specify the action or behavior of the exec command. You can check the supported options in the section below:
  2. SERVICE – specifies the service name defined in the docker-compose.yaml file.
  3. 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:

  1. –detach or -d – This tells the docker engine to run the command in the background or in detached mode.
  2. –env or -e – Used to set a given environment variable or multiple environment variables.
  3. –index – This specifies the index of the container if the service has multiple replicas.
  4. –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.
  5. –user or -u – Specifies the user that should run the specified command.
  6. –workdir or -w – Specifies the path to the workdir directory for the command.
  7. –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:

$ docker compose exec hello sh

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:

$ docker compose exec -u=linuxhint hello echo “hello world”

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list