Keywords: Docker Compose | detached mode | interactive terminal
Abstract: This article provides an in-depth exploration of configuring detached mode and interactive terminals in Docker Compose. Through analysis of a practical case, it explains how to convert complex docker run commands into docker-compose.yml files, with a focus on mapping flags like -d, -i, and -t. Based on Docker official documentation, the article offers best practice recommendations and addresses common issues such as container exit problems.
Configuring Detached Mode and Interactive Terminals in Docker Compose
In Docker containerization, the complexity of docker run commands often leads developers to adopt Docker Compose for service orchestration. This article analyzes a specific case to detail how to configure detached mode and interactive terminals in Docker Compose, along with related best practices.
Case Background and Problem Analysis
The original docker run command is as follows:
docker run -d --name test -v /etc/hadoop/conf:/etc/hadoop/conf -v /usr/lib/python2.7/dist-packages/hdinsight_common:/usr/lib/python2.7/dist-packages/hdinsight_common -v /etc/hive/conf/:/etc/hive/conf/ -v /etc/tez/conf/:/etc/tez/conf/ -v /usr/hdp/2.4.2.0-258/sqoop/lib/:/usr/hdp/2.4.2.0-258/sqoop/lib/ -i -t hdinsight /bin/bash
This command includes multiple volume mounts, the detached mode flag -d, interactive flag -i, and terminal flag -t. When converting to Docker Compose configuration, the main challenge is correctly mapping these flags in the docker-compose.yml file.
Docker Compose Configuration Analysis
According to the Docker Compose official documentation, most docker run command parameters have corresponding Compose configuration items. Below is a detailed explanation of key configurations:
Detached Mode (-d Flag)
The detached mode flag -d is not a service configuration item in Docker Compose but a parameter for the docker-compose command. The correct usage is:
docker-compose up -d
Or when using the run command:
docker-compose run -d hdinsight
This runs the container in the background, similar to docker run -d.
Interactive Terminal (-i and -t Flags)
For the interactive flag -i and terminal flag -t, Docker Compose handles them differently:
- The
docker-compose runcommand enables an interactive terminal by default, so explicit configuration of-iand-tflags is usually unnecessary. - If these properties need to be configured in the
docker-compose.ymlfile, usestdin_open: true(corresponding to-i) andtty: true(corresponding to-t). Example:
version: '3'
services:
hdinsight:
image: hdinsight
stdin_open: true
tty: true
Note that when starting multiple containers with docker-compose up, it is not possible to interact with all containers simultaneously, making interactive mode less applicable in such scenarios.
Best Practice Recommendations
Based on case analysis and Docker official documentation, we recommend the following best practices:
- Prefer
docker-compose upoverdocker-compose run: Theupcommand is more suitable for launching and managing multi-container applications, as it automatically handles all defined services. - Configure Volume Mounts Properly: In
docker-compose.yml, volume mounts should be clearly listed to ensure correct mapping between host and container paths. Example:
volumes:
- /etc/hadoop/conf:/etc/hadoop/conf
- /usr/lib/python2.7/dist-packages/hdinsight_common:/usr/lib/python2.7/dist-packages/hdinsight_common
<ol start="3">
Exited), possible causes include:- Incorrect entrypoint or command configuration, causing the container to terminate after task completion.
- Lack of interactive terminal configuration, preventing the container from remaining running.
- It is advised to check logs for diagnosis:
docker logs <container_id>.
Supplementary Configuration Example
Incorporating insights from other answers, here is a complete docker-compose.yml configuration example:
version: '3'
services:
hdinsight:
image: hdinsight
container_name: ABC
volumes:
- /etc/hadoop/conf:/etc/hadoop/conf
- /usr/lib/python2.7/dist-packages/hdinsight_common:/usr/lib/python2.7/dist-packages/hdinsight_common
- /etc/hive/conf/:/etc/hive/conf/
- /etc/tez/conf/:/etc/tez/conf/
- /usr/hdp/2.4.2.0-258/sqoop/lib/:/usr/hdp/2.4.2.0-258/sqoop/lib/
stdin_open: true
tty: true
labels:
- "HDInsight client VM"
Start command:
docker-compose up -d
To attach to a running container, use:
docker attach <container_id>
And detach with Ctrl+P, Ctrl+Q.
Conclusion
Configuring detached mode and interactive terminals in Docker Compose simplifies container management significantly. Key points include: using docker-compose up -d for detached mode, configuring interactive terminals with stdin_open: true and tty: true, and properly handling volume mounts and container exit issues. Developers should refer to Docker official documentation to ensure accurate configuration and application of best practices.