Search code examples
docker

Is it possible to disable manual docker container restarts?


We have a few containers in production which occasionally have issues and need to be restarted. However, there are a few additional steps we need to take when doing restarts due to service dependencies and how the service works, so we use Jenkins jobs to handle rebooting the service. Consequently, going in and using the docker restart syntax will leave the service in a bad state.

Not all of our ops team knows this though, as they're constantly onboarding and replacing team members. I would love to disable the docker restart syntax and if possible add a friendly "This container doesn't allow restarts, please contact devs" kind of message, but all my searches just turn up results with how to disable auto-restart, which is a different problem


Solution

  • One possibility is to shadow the docker command by writing a script.

    For example, you could make a script like this:
    /usr/local/bin/custom-docker

    #!/usr/bin/env bash
    
    # Checking for "docker restart"
    if [[ $1 == "restart" ]]; then
      echo "Please do not run the command 'docker restart' without contacting the devs."
      exit 1
    fi
    
    # Otherwise, pass all arguments to the real Docker command
    exec /usr/bin/docker "$@"
    

    Set execute permissions

    chmod +x /usr/local/bin/custom-docker
    

    Then alias to docker in your .profile file or an .<shell>rc file

    alias docker='/usr/local/bin/custom-docker'
    

    Now docker restart prints out the error message but everything else works as before.