2

I have simple container with some service.

When I restarting server without stopping this container, I can't start it again.

Error message is:

pidfile found, try stopping another running service or delete /var/run/service.pid

I know that I can

  1. run new container from image and delete stopped one
  2. create new image from stopped container and re-run it with new entrypoint. Something like rm -f /var/run/service.pid && original_entrypoint.sh

But I want simply do something like

docker rm_file container:/var/run/service.pid; docker start container

Because it is most simple and fast to start solution.

Isn't here is no way to access container's fs without completely rebuild it? This operation is looking so useful...

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • can you mount `/var/run` to some host path when run docker, so when restart you can simply delete file from host? – Lei Yang Jul 06 '21 at 02:27
  • @LeiYang Thank you for comment, but not sure if holding temporary files at host is a good idea. – rzlvmp Jul 06 '21 at 02:30

2 Answers2

6

Answering by myself using hints from another answer

  1. Find where directory stored on docker host:

    export LOCAL_DIR=$(docker inspect -f '{{ .GraphDriver.Data.UpperDir }}' container_name)
    
  2. Remove file locally:

    sudo rm -f ${LOCAL_DIR}/run/service.pid
    
  3. Run container:

    docker start container_name
    

Or all in one:

sudo rm -f "$(docker inspect -f '{{ .GraphDriver.Data.UpperDir }}' container_name)/run/service.pid" && docker start container_name
rzlvmp
  • 7,512
  • 5
  • 16
  • 45
-1

I always delete the old container and run a new one. This works consistently with every application and runtime, and doesn't involve trying to figure out how to manually reset the container filesystem to its initial state. I almost never use docker start.

docker rm container_name
docker run -d --name container_name ...

If you're in a context where the old pid file might be left behind (maybe it's in a bind-mounted host directory) you can use an entrypoint wrapper script to clean it up:

#!/bin/sh
rm -f /var/run/service.pid
exec "$@"

In your Dockerfile, make this script be the ENTRYPOINT. The last line will run the image's CMD as the main container process.

...
COPY entrypoint.sh . # must be executable, may already be there
ENTRYPOINT ["./entrypoint.sh"] # must be JSON-array form
CMD same as before

(Your question references an original_entrypoint.sh; if you already have this setup, edit the existing entrypoint script in your local source tree and add the rm -f line in there.)

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thank you. That is useful information, but your solution suppose to rerun (rebuild) container, and as I said in my question that is no way for me in this time. – rzlvmp Jul 07 '21 at 01:08