Auto remove container with docker-compose.yml

前端 未结 4 571
渐次进展
渐次进展 2020-12-23 13:25

docker-compose run has a flag --rm that auto removes the container after run. I am wondering if theres an equivalent config with docker-compose.yml

相关标签:
4条回答
  • 2020-12-23 13:37

    I haven't found any option to help you define this behavior in the docker-compose.yml file and I think the explanation is the following:

    docker-compose up builds, (re)creates, starts, and attaches to containers for a service.

    Since your images are built and the containers of your service have started, you can then use docker-compose stop and docker-compose start to start/stop your service. This is different from docker-compose down which:

    Stops containers and removes containers, networks, volumes, and images created by up.

    Problem with what you are trying to do:

    If you docker-compose up and one of your containers finishes its task and gets (auto)removed, then you can't docker-compose stop and docker-compose start again. The removed container will not be there to start it again.


    You might want to take a look at:

    • docker-compose: option to automaticaly remove container after run in docker-compose.yml
    • What is the difference between docker-compose up and docker-compose start?
    0 讨论(0)
  • 2020-12-23 13:39

    Simply run docker-compose up && docker-compose rm -fsv

    0 讨论(0)
  • 2020-12-23 13:58

    I'm not sure I understand, docker-compose run --user is an option, and the docker-compose.yml supports the user key (http://docs.docker.com/compose/yml/#working95dir-entrypoint-user-hostname-domainname-mem95limit-privileged-restart-stdin95open-tty-cpu95shares).

    0 讨论(0)
  • 2020-12-23 14:01

    My solution to this was to create a little bash script that automatically removes containers afterwards - It doesn't work with detached mode, but I suppose that might not be such a big issue.

    If you're on macOS, you can put this script in usr/local/bin. Assuming it's named d-c, you can then run chmod +x usr/local/bin/d-c to make it executable. On Windows, I have no idea how to get this working, but on Linux it should be similar.

    #! /bin/bash
    
    if [[ $1 == "up" ]]; then
        # runs "docker-compose up" and then "docker-compose down"
        docker-compose up "${@:2}"; docker-compose down
    elif [[ $1 == "run" ]]; then
        # "d-c run" automatically adds the --rm flag
        docker-compose run --rm "${@:2}"
    else
        # any other d-c command runs docker-compose normally
        docker-compose "${@:1}"
    fi
    

    Edit:

    I've updated my script so that detached mode will work normally:

    #! /bin/bash
    
    # check for -d, --detached
    DETACHED=false
    for (( i=1; i <= "$#"; i++ )); do
      ARG="${!i}"
      case "$ARG" in
        -d|--detach)
          DETACHED=true
          ;;
      esac
    done
    
    if [[ $1 == "run" ]] && [[ $DETACHED == false ]]; then
        docker-compose run --rm "${@:2}"
    elif [[ $1 == "up" ]] && [[ $DETACHED == false ]]; then
        docker-compose up "${@:2}"; docker-compose down
    else
        docker-compose "${@:1}"
    fi
    
    0 讨论(0)
提交回复
热议问题