Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

How to make the entrypoint run a start service script without exiting container in docker-compose?

The docker-compose.yml is:

version: "3"

services:
  xx:
    image: xx:1.0
    container_name: instance
    command: /home/admin/start.sh start && tail -f /dev/null
    network_mode: "host"
    tty: true

I use docker-compose up to start the container.

The problem is the container will show Exited (0) after the start.sh start finished.

I know command or entrypoint will launch a pid 1 process, if the process was finished, the container would exit.

So, I add the tail -f /dev/null to prevent it from exiting, but I can't figure out why it still exit.

I just want start the service(/home/admin/start.sh start) and keep the container alive. What the command should be in the docker-compose.yml?

question from:https://stackoverflow.com/questions/65904284/how-to-make-the-entrypoint-run-a-start-service-script-without-exiting-container

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should run the application as a foreground process. Do not use a "service" script or anything else that launches the application as a daemon.

# Run the application itself, not a "start" script that launches
# a background process
command: /home/admin/admin_application

Typically this main command is a property of the image: whenever you start the container this is the thing you'll almost always want to run. That means it's more appropriate to specify this as the CMD in your Dockerfile:

CMD ["/home/admin/admin_application"]

Then in your docker-compose.yml you don't need to override this value. You also shouldn't usually need to specify container_name: (Compose can assign this on its own), tty: (only needed for interactive programs), or network_mode: host (which generally disables Docker networking). Your docker-compose.yml can be as little as

version: "3.9" # "3" means "3.0"
services:
  xx:
    image: xx:1.0
    # ports: ["8080:8080"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...