DennisLi
DennisLi

Reputation: 4156

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?

Upvotes: 0

Views: 448

Answers (1)

David Maze
David Maze

Reputation: 158778

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"]

Upvotes: 1

Related Questions