How to rebuild docker container in docker-compose.yml?

前端 未结 10 2021
闹比i
闹比i 2021-01-29 17:14

There are scope of services which defined in docker-compose.yml. These service have been started. I need to rebuild only one of these and start it without up other services. I r

相关标签:
10条回答
  • 2021-01-29 17:40

    Simply use :

    docker-compose build [yml_service_name]
    

    Replace [yml_service_name] with your service name in docker-compose.yml file. You can use docker-compose restart to make sure changes are effected. You can use --no-cache to ignore the cache.

    0 讨论(0)
  • 2021-01-29 17:42

    This should fix your problem:

    docker-compose ps # lists all services (id, name)
    docker-compose stop <id/name> #this will stop only the selected container
    docker-compose rm <id/name> # this will remove the docker container permanently 
    docker-compose up # builds/rebuilds all not already built container 
    
    0 讨论(0)
  • 2021-01-29 17:47

    As @HarlemSquirrel posted, it is the best and I think the correct solution.

    But, to answer the OP specific problem, it should be something like the following command, as he doesn't want to recreate ALL services in the docker-compose.yml file, but only the nginx one:

    docker-compose up -d --force-recreate --no-deps --build nginx
    

    Options description:

    Options:
      -d                  Detached mode: Run containers in the background,
                          print new container names. Incompatible with
                          --abort-on-container-exit.
      --force-recreate    Recreate containers even if their configuration
                          and image haven't changed.
      --build             Build images before starting containers.
      --no-deps           Don't start linked services.
    
    0 讨论(0)
  • 2021-01-29 17:49

    For me it only fetched new dependencies from Docker Hub with both --no-cache and --pull (which are available for docker-compose build.

    # other steps before rebuild
    docker-compose build --no-cache --pull nginx # rebuild nginx
    # other steps after rebuild, e.g. up (see other answers)
    
    0 讨论(0)
  • 2021-01-29 17:50

    docker-compose up

    $ docker-compose up -d --no-deps --build <service_name>
    

    --no-deps - Don't start linked services.

    --build - Build images before starting containers.

    0 讨论(0)
  • 2021-01-29 17:51

    The problem is:

    $ docker-compose stop nginx
    

    didn't work (you said it is still running). If you are going to rebuild it anyway, you can try killing it:

    $ docker-compose kill nginx
    

    If it still doesn't work, try to stop it with docker directly:

    $ docker stop nginx
    

    or delete it

    $ docker rm -f nginx
    

    If that still doesn't work, check your version of docker, you might want to upgrade.

    It might be a bug, you could check if one matches your system/version. Here are a couple, for ex: https://github.com/docker/docker/issues/10589

    https://github.com/docker/docker/issues/12738

    As a workaround, you could try to kill the process.

    $ ps aux | grep docker 
    $ kill 225654 # example process id
    
    0 讨论(0)
提交回复
热议问题