The docker implementation container and host seamlessly invoke the shell command

  • 2021-10-25 08:20:04
  • OfStack

As shown below:


nsenter -t 1 -m -u -n -i sh -c "echo hello world! "

Add privilege permissions to the docker container and set pid: "host"

Supplement: Execute shell scripts or commands in the docker container on the host machine

Common command forms:


docker exec -it master /bin/bash -c 'echo $PATH'
docker exec -it master /bin/bash -c 'cd /home/bigdata/shell && ./test.sh'

If environment variables are involved in the shell script, or if JAVA_HOME is not set is prompted, you can add the following command to the script


export JAVA_HOME=jdk Path 

Supplement: Run the commands executed in container in docker on the docker host machine

The script is as follows:


docker exec -u root xxx(container Name ) bash -c ' Command 1 &&  Command 2'

Practical examples are as follows:


docker exec -u root mycentos bash -c 'mkdir /home/test.txt && chmod 777 /home/test.txt && runuser - oracle -c "rm -rf /home/test.txt"'

Related articles: