First, encountered problems

I started the React project using Docker, and the container stopped after startup. The information is as follows:

yunfeideMacBook-Pro:portal-web-platform zhangyunfei$ make run docker rm -f portal-web-platform |true&&docker run --publish 3000:3000 --name portal-web-platform portal-web-platform:latest /bin/bash: warning: setlocale: LC_ALL: Cannot change locale (en_us.utF-8) YARN run v1.22.5 $node scripts/start.js ℹ "WDS" : Project is running at http://172.17.0.2/ ℹ "WDS" : Webpack output is served from ℹ "WDS" : Content Not from Webpack is served from /app/public ℹ "WDS" : 404S will fallback to/Starting the Development Server... Done in 4.46 s.Copy the code

It looks like it’s started, but it turns out that the “container” state is Exist, and the container stops. At first I thought it was a procedural problem, but it took me a long time to find a solution.

Second, the analysis

The main reason is that when you run in the background, the container created by the image will execute /bin/bash first, which means that when you run in the background (-d), the shell immediately exits. So unless the command is not running in the foreground, the container stops immediately

Note: When we execute YARN start locally, the terminal refreshes and then starts the program, which looks like “after the original terminal terminates, a new terminal window is started”. If it is in a Docker container, the container is terminated.

Third, solve

The solution is to provide a pseudo “tty n” for -d with either -i or -t.

docker run -t -d images:tags
Copy the code

The function of the Docker parameter -t

-t causes docker to assign a dummy terminal and bind it to the container’s standard input, and -i keeps the container’s standard input open.

The default command for system mirroring in Docker is bash. If the -ti bash command is not executed, the system will exit automatically. This is because if the input stream is not connected, it will end immediately. The -ti docker command assigns a dummy terminal to the container and takes over its stdin/stdout support for interactive operations. The bash command does not exit automatically.

Iv. Reference:

Blog.csdn.net/qq_19381989… www.cnblogs.com/ExMan/p/116…