0

I have a script /init that launches apache and neo4j. This script is already in the image ubuntu:14. The following is the content of /init:

service apache2 start
service neo4j start

From this image, I am creating another image with the following dockerfile

FROM ubuntu:v14
EXPOSE 80 80
ENTRYPOINT ["/init"]

When I run the command docker run -d ubuntu:v15, the container starts and then exit. As far as I understood, -d option runs the container in the background. Also, the script\init launches two daemons. Why does the container exit ?

Noor
  • 19,638
  • 38
  • 136
  • 254
  • 1
    Your script is pid 1, when pid 1 exits, the container dies. Try tailing the logs or something ref: https://stackoverflow.com/questions/49508874/how-do-avoid-a-docker-container-stop-after-the-application-is-stopped-descriptio/49509014#49509014 – johnharris85 Jul 11 '19 at 21:32

2 Answers2

1

When your Dockerfile specifies an ENTRYPOINT, the lifetime of the container is exactly the length of whatever its process is. Generally the behavior of service ... start is to start the service as a background process and then return immediately; so your /init script runs the two service commands and completes, and now that the entrypoint process is completed, the container exits.

Generally accepted best practice is to run only one process in a container. That's especially true when one of the processes is a database. In your case there are standard Docker Hub Apache httpd and neo4j images, so I'd start by using an orchestration tool like Docker Compose to run those two containers side-by-side.

David Maze
  • 130,717
  • 29
  • 175
  • 215
1

In fact, I think your first problem is the #! in init file, if you did not add something like #!/bin/bash at the start, container will complain like next:

shubuntu1@shubuntu1:~$ docker logs priceless_tu
standard_init_linux.go:207: exec user process caused "exec format error"

But even you fix above problem, you will still can't start your container, the reason same as other folks said: the PID 1 should always there, in your case after service xxx start finish, the PID 1 exit which will also result in container exit.

So, to conquer this problem you should set one command never exit, a minimal workable example for your reference:

Dockerfile:

FROM ubuntu:14.04
RUN apt-get update && \
    apt-get install -y apache2

COPY init /
RUN chmod +x /init

EXPOSE 80
ENTRYPOINT ["/init"]

init:

#!/bin/bash

# you can add other service start here
# e.g. service neo4j start as you like if you have installed it already

# next will make apache run in foreground, so PID1 not exit.
/usr/sbin/apache2ctl -DFOREGROUND
atline
  • 28,355
  • 16
  • 77
  • 113