programing

도커 컴포지트.yml에서 도커 컨테이너를 리빌드하는 방법은?

subpage 2023. 10. 26. 21:02
반응형

도커 컴포지트.yml에서 도커 컨테이너를 리빌드하는 방법은?

서비스 범위는 docker-compose.yml에 정의되어 있습니다.서비스가 시작되었습니다.이 중 하나만 리빌드하고 다른 서비스를 시작하지 않고 시작해야 합니다.다음 명령을 실행합니다.

docker-compose up -d # run all services
docker-compose stop nginx # stop only one. but it is still running !!!
docker-compose build --no-cache nginx 
docker-compose up -d --no-deps # link nginx to other services

마지막에 나는 오래된 nginx 컨테이너를 받습니다.도커 컴포지트가 작동 중인 모든 컨테이너를 죽이지는 않습니다!

도커를 compose으로 올리다

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

또는 새로운 버전의 도커(docker).

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

--no-deps - 연결된 서비스를 시작하지 않습니다.

--build - 컨테이너를 시작하기 전에 이미지를 만듭니다.

도커 컴포지트 1.19 포함

docker-compose up --build --force-recreate --no-deps [-d] [<service_name>..]

하나 이상 없음service_name인수 누락된 경우 모든 이미지가 작성되고 모든 컨테이너가 다시 만들어집니다.

도움말 메뉴에서

Options:
    -d, --detach        Detached mode: Run containers in the background,
                        print new container names. Incompatible with
                        --abort-on-container-exit.
    --no-deps           Don't start linked services.
    --force-recreate    Recreate containers even if their configuration
                        and image haven't changed.
    --build             Build images before starting containers.

캐시 없음

캐시된 계층을 무시하도록 재구축하려면 먼저 새 이미지를 구축해야 합니다.

docker-compose build --no-cache [<service_name>..]

도움말 메뉴에서

Options:
    --force-rm              Always remove intermediate containers.
    -m, --memory MEM        Set memory limit for the build container.
    --no-cache              Do not use cache when building the image.
    --no-rm                 Do not remove intermediate containers after a successful build.

그런 다음 컨테이너를 다시 만듭니다.

docker-compose up --force-recreate --no-deps [-d] [<service_name>..]

그러면 문제가 해결됩니다.

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 

@HarlemSquirrel님께서 올려주신 것처럼, 그것은 최고이며 올바른 해결책이라고 생각합니다.

그러나 OP 특정 문제에 대답하려면 다음 명령과 같은 것이어야 합니다. 왜냐하면 그는 다음의 모든 서비스를 재생성하고 싶지 않기 때문입니다.docker-compose.yml파일, 그러나 오직.nginx나:

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

옵션 설명:

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.

아마 이 단계들이 정확하지는 않겠지만, 저는 이렇게 생각합니다.

정지 도커 구성:$ docker-compose down

경고: 다음 사항prune -a이미지가 모두 삭제되므로 다른 프로젝트에 영향을 미칠 수 있으므로 원하지 않을 수 있습니다.당신은 여기서 더 읽을 수 있습니다.

용기를 제거합니다.$ docker system prune -a

도커 구성 시작:$ docker-compose up -d

docker-compose stop nginx # stop if running
docker-compose rm -f nginx # remove without confirmation
docker-compose build nginx # build
docker-compose up -d nginx # create and start in background

rm으로 컨테이너를 제거하는 것은 필수입니다.제거하지 않으면 도커가 오래된 컨테이너를 시작합니다.

저에게는 두 가지를 모두 갖춘 도커 허브에서 새로운 의존성만 가져왔습니다.--no-cache그리고.--pull(에 제공되는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)

문제는 다음과 같습니다.

$ docker-compose stop nginx

작동하지 않았습니다(아직 실행 중이라고 했음).어쨌든 재구축하려는 경우에는 처치를 시도해 볼 수 있습니다.

$ docker-compose kill nginx

그래도 작동하지 않으면 도커로 직접 멈추십시오.

$ docker stop nginx

삭제하거나 삭제합니다.

$ docker rm -f nginx

그래도 작동하지 않으면 도커 버전을 확인하여 업그레이드할 수 있습니다.

버그일 수 있습니다. 시스템/버전과 일치하는지 확인할 수 있습니다.다음은 몇 가지 예입니다. https://github.com/docker/docker/issues/10589

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

이를 해결하기 위해 프로세스를 없애려고 할 수도 있습니다.

$ ps aux | grep docker 
$ kill 225654 # example process id

간단히 사용:

docker-compose build [yml_service_name]

교체하다[yml_service_name]귀하의 서비스 이름을 입력합니다.docker-compose.yml파일. 사용할 수 있습니다.docker-compose restart변화를 확실히 하기 위해서입니다.사용가능--no-cache캐시를 무시하는 겁니다

다음을 사용할 수 있습니다.

docker-compose build

도커 프로파일을 사용하는 경우:

docker-compose --profile profile_name build

nginx라는 이미지가 도커 합성 파일에 정의되어 있다고 생각합니다.이 경우 다음과 같은 키/값 쌍이 있습니다.image: repo_name. 이 경우, 새로운 이미지를 먼저 당겨보는 것이 좋습니다.docker pull repo_name. 그럼 서비스를 구축합니다.docker-compose build nginx. 그러면 새로 뽑은 이미지가 사용됩니다.그러면 가능합니다.docker-compose up -d nginx.

사실, 이 작업을 하는 동안에는 용기를 전혀 멈출 필요가 없습니다.

(그리고 이 작업을 자주 하는 경우에는 종종 오래된 이미지를 삭제해야 합니다.docker image ls,그리고 나서.docker image rm <id>TAG 값이 다음과 같은 모든 영상에 대해<none>. 상관없이 이미지를 계속 사용해야 할 경우 도커가 알려줄 것입니다.)

편집:docker-compose stop용기를 "죽여"(제거)하는 것이 아니라 단지 그들을 멈추게 할 뿐입니다.그들은 아직도 거기에 있어요, 일종의 정지 상태에요.정말로 죽이고 싶다면, 예를 들어 용기를 제거하고,docker-compose down대신 출력을 통해 이 명령어가 실제로 컨테이너를 제거하는 것을 알 수 있습니다.

docker image rm -f nginx

도커 nginx를 제거하고

docker-compose up -d

다른 서비스를 계속 실행하면서 nginx를 재생성하려면 다음과 같이 하십시오.

만:

$ docker-compose restart [yml_service_name]

언급URL : https://stackoverflow.com/questions/36884991/how-to-rebuild-docker-container-in-docker-compose-yml

반응형