How can I run docker containers in detached mode within a committed image

I have pulled an image called php:5.6-apache, run it, touch a file in /var/www/html, then commit it.

Then I try to run the new image in detached mode:

# docker run --name test -d staraple/php:v1

But the container exited immediately after started.

IMAGE                COMMAND              CREATED                STATUS       
staraple/php:v2  "apache2 -DFOREGROUN   4 seconds ago       Exited (0) 3 seconds ago                       

Fisrt I though the new image lost the CMD after a commit action.

Refer to , CMD will reserved after commit;

My problem is why my container could not run in detached mode when I got a CMD. php.5.6-apache is official, and I just touch a blank file in it.

When I check json for images in /var/lib/docker/graph, I found my new image has a different Cmd config from the original one:

"Cmd":["/bin/sh","-c","#(nop) CMD [apache2 -DFOREGROUND]"]

docker logs show message: https (pid 1) already running , other than [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' .

1 Like

I’m still thinking about it, but I think this makes sense.

if you run the original, then kill it (ie, don’t shut down apache cleanly) and then commit it, your commited image will still have the apache pid on the FS (even though its not running).

The ‘right’ way to do this, would be to make your own Dockerfile that is FROM php:4.5-apache, OR to make sure you shut down the apache in your initial run cleanly, so it cleans up.

I do wonder if our service based images shouldn’t use a pid1 that does this in a more robust way, but tbh, i rather have it as simple as possible.

1 Like