code

이미지를 깔끔하게 빌드하기 위해 Docker를 강제하는 방법

codestyles 2020. 10. 2. 22:19
반응형

이미지를 깔끔하게 빌드하기 위해 Docker를 강제하는 방법


아래 명령을 사용하여 Docker 파일에서 Docker 이미지를 빌드했습니다.

$ docker build -t u12_core -f u12_core .

동일한 명령으로 다시 빌드하려고 할 때 다음과 같은 빌드 캐시를 사용하고 있습니다.

Step 1 : FROM ubuntu:12.04
 ---> eb965dfb09d2
Step 2 : MAINTAINER Pavan Gupta <pavan.gupta@gmail.com>
 ---> Using cache
 ---> 4354ccf9dcd8
Step 3 : RUN apt-get update
 ---> Using cache
 ---> bcbca2fcf204
Step 4 : RUN apt-get install -y openjdk-7-jdk
 ---> Using cache
 ---> 103f1a261d44
Step 5 : RUN apt-get install -y openssh-server
 ---> Using cache
 ---> dde41f8d0904
Step 6 : RUN apt-get install -y git-core
 ---> Using cache
 ---> 9be002f08b6a
Step 7 : RUN apt-get install -y build-essential
 ---> Using cache
 ---> a752fd73a698
Step 8 : RUN apt-get install -y logrotate
 ---> Using cache
 ---> 93bca09b509d
Step 9 : RUN apt-get install -y lsb-release
 ---> Using cache
 ---> fd4d10cf18bc
Step 10 : RUN mkdir /var/run/sshd
 ---> Using cache
 ---> 63b4ecc39ff0
Step 11 : RUN echo 'root:root' | chpasswd
 ---> Using cache
 ---> 9532e31518a6
Step 12 : RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
 ---> Using cache
 ---> 47d1660bd544
Step 13 : RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
 ---> Using cache
 ---> d1f97f1c52f7
Step 14 : RUN wget -O aerospike.tgz 'http://aerospike.com/download/server/latest/artifact/ubuntu12'
 ---> Using cache
 ---> bd7dde7a98b9
Step 15 : RUN tar -xvf aerospike.tgz
 ---> Using cache
 ---> 54adaa09921f
Step 16 : RUN dpkg -i aerospike-server-community-*/*.deb
 ---> Using cache
 ---> 11aba013eea5
Step 17 : EXPOSE 22 3000 3001 3002 3003
 ---> Using cache
 ---> e33aaa78a931
Step 18 : CMD /usr/sbin/sshd -D
 ---> Using cache
 ---> 25f5fe70fa84
Successfully built 25f5fe70fa84

캐시는 에어로 스파이크가 설치되었음을 보여줍니다. 그러나이 이미지에서 생성 된 컨테이너 내부에서 찾을 수 없으므로 캐시를 사용하지 않고이 이미지를 다시 빌드하고 싶습니다. Docker가 캐시없이 깨끗한 이미지를 다시 빌드하도록하려면 어떻게해야합니까?


있다 --no-cache옵션 :

docker build --no-cache -t u12_core -f u12_core .

이전 버전의 Docker에서는을 전달해야 --no-cache=true했지만 더 이상 그렇지 않습니다.


극단적 인 경우, 반복되는 빌드 실패를 해결하는 유일한 방법은 다음을 실행하는 것입니다.

docker system prune

명령은 확인을 요청합니다.

WARNING! This will remove:
    - all stopped containers
    - all volumes not used by at least one container
    - all networks not used by at least one container
    - all images without at least one container associated to them
Are you sure you want to continue? [y/N]

물론 이것은 질문에 대한 직접적인 대답은 아니지만 일부 생명을 구할 수 있습니다 ... 그것은 저를 구했습니다.


The command docker build --no-cache . solved our similar problem.

Our Dockerfile was:

RUN apt-get update
RUN apt-get -y install php5-fpm

But should have been:

RUN apt-get update && apt-get -y install php5-fpm

To prevent caching the update and install separately.

See: Best practices for writing Dockerfiles


I would not recommend using --no-cache in your case.

You are running a couple of installations from step 3 to 9 (I would, by the way, prefer using a one liner) and if you don't want the overhead of re-running these steps each time you are building your image you can modify your Dockerfile with a temporary step prior to your wget instruction.

I use to do something like RUN ls . and change it to RUN ls ./ then RUN ls ./. and so on for each modification done on the tarball retrieved by wget

You can of course do something like RUN echo 'test1' > test && rm test increasing the number in 'test1 for each iteration.

It looks dirty, but as far as I know it's the most efficient way to continue benefiting from the cache system of Docker, which saves time when you have many layers...


With docker-compose try docker-compose up -d --build --force-recreate


To ensure that your build is completely rebuild, including checking the base image for updates, use the following options when building:

--no-cache - This will force rebuilding of layers already available

--pull - This will trigger a pull of the base image referenced using FROM ensuring you got the latest version.

The full command will therefore look like this:

docker build --pull --no-cache --tag myimage:version .

Same options are available for docker-compose:

docker-compose build --no-cache --pull

You can manage the builder cache with docker builder

To clean all the cache with no prompt: docker builder prune -af

참고URL : https://stackoverflow.com/questions/35594987/how-to-force-docker-for-a-clean-build-of-an-image

반응형