docker exec用&返回错误PID,Hyperledger Composer REST Server管理问题
I’ve run into this exact issue before with Docker exec and background processes—here’s what’s happening and how to fix it:
Why the PID is wrong
When you use docker exec -it cli bash &, the & is telling your host machine’s shell to run the docker exec process in the background—not the composer-rest-server inside the container. The PID you see returned is for the docker exec process on your host, not the actual REST server process running inside the CLI container. That’s why it’s useless for terminating the server later.
Solution 1: Run the command directly in the background (no interactive shell)
Skip the interactive bash terminal entirely and use Docker’s -d (detached) flag to run the REST server command directly in the container’s background. This avoids PID confusion and is cleaner:
docker exec -d cli nohup composer-rest-server -c <myCard> -p <myPort> > /var/log/rest-server.log 2>&1
The > /var/log/rest-server.log 2>&1 part redirects all output (stdout + stderr) to a log file inside the container, so you can check logs later with:
docker exec cli cat /var/log/rest-server.log
Solution 2: Capture the correct container-side PID when using interactive shell
If you still need to use the interactive bash terminal to start the server, save the container-side PID to a file when you launch it:
# Enter the container's interactive terminal docker exec -it cli bash # Start the REST server and save its PID to a temp file nohup composer-rest-server -c <myCard> -p <myPort> & echo $! > /tmp/rest-server.pid
To terminate the server later, you can either:
- Run this inside the container:
kill $(cat /tmp/rest-server.pid) - Or run this directly from your host:
docker exec cli kill $(cat /tmp/rest-server.pid)
Solution 3: Find and kill the process without a saved PID
If you forgot to save the PID, you can locate and terminate the server directly from your host:
# Find the PID of the composer-rest-server inside the container docker exec cli ps aux | grep composer-rest-server | grep -v grep | awk '{print $2}' # Kill the process in one command docker exec cli pkill -f composer-rest-server
内容的提问来源于stack exchange,提问作者Bosen

