Home>Article>Operation and Maintenance> How to solve the problem of docker executing exec error

How to solve the problem of docker executing exec error

藏色散人
藏色散人 Original
2022-01-12 14:12:37 8441browse

Solution to the error when docker executes exec: 1. Replace the first line in the script with "/bin/bash"; 2. Execute "docker exec -d service_name /bin/bash -c '/opt /start.sh'".

How to solve the problem of docker executing exec error

The operating environment of this article: centos7 system, Docker version 20.10.11, Dell G3 computer.

Docker exec failed to execute script solution

##Background introduction

There is a script in the docker container that needs to be run in the background when used. However, calling this script through docker exec never gets the desired result. As a result, the path of exploration began!

Original script

docker exec -it service_name /bin/bash -c '/opt/start.sh'

The function of this start.sh is to start two services in the background

#!/bash nohup start1.sh > start1.log 2>&1 & sleep 5 nohup start2.sh 2>&1 & echo 'server started'

Problem location

1. Irregular script writing

From the above command, you can see the formula used when docker is started

/bin/bashAnd the script What is specified in is indeed/bash. We all know that the content of the first line of the script specifies the path of the shell script interpreter, and this specified path can only be placed on the first line of the file. Therefore, you need to replace the first line in the script with/bin/bashso that they use the same interpreter

2. There is a problem with the startup parameters

The

-itparameter is used during the execution of docker exec.How to solve the problem of docker executing exec error

    -t allows docker to allocate a pseudo terminal and bind it to the container's standard Enter
  • -i to keep the standard input of the container open.
In this way, only the first start.sh can be executed after executing the background script, and the second one cannot be started. Function, because the terminal also exited after execution,

See in help, there is a -d, background cloud script, this one just meets our requirements

Modification process

docker exec -d service_name /bin/bash -c '/opt/start.sh'
#!/bin/bash nohup start1.sh > start1.log 2>&1 & sleep 5 nohup start2.sh 2>&1 & echo 'server started'

Recommended learning: "

docker tutorial"

The above is the detailed content of How to solve the problem of docker executing exec error. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn