docker:tips_for_building_a_docker_image
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
docker:tips_for_building_a_docker_image [2016/10/17 13:48] – created peter | docker:tips_for_building_a_docker_image [2020/05/13 08:43] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Docker - Tips For Building a Docker Image ====== | ||
- | |||
- | ===== How to effectively build and use docker image to speed up our deployment? ===== | ||
- | |||
- | This would be important to in both our development and release cycle. | ||
- | |||
- | |||
- | ===== Cache Internet Download As Much As Possible ===== | ||
- | |||
- | Usually deployment need to download hundreds of MB data from the internet. | ||
- | |||
- | The more we cache, the faster the deployment would be. The ultimate goal is to support **offline all-in-one deployment** from the Docker image. | ||
- | |||
- | |||
- | ===== Treat Docker Image As Plain OS Golden Image ===== | ||
- | |||
- | Docker is powerful, but we also have lots of VM or bare metal deployment. | ||
- | |||
- | Here is an example, if you’re using kitchen for CI. By default, we test with customized Docker image. When we specify IMAGE_NAME to ubuntu: | ||
- | |||
- | < | ||
- | --- | ||
- | driver: | ||
- | name: docker | ||
- | driver_config: | ||
- | instance_name: | ||
- | use_sudo: false | ||
- | privileged: true | ||
- | remove_images: | ||
- | image: <%= ENV.fetch(' | ||
- | </ | ||
- | | ||
- | |||
- | ===== Audit All Packages And Services Installed In Docker Image ===== | ||
- | |||
- | Start a docker container from the image, then list and check all packages/ | ||
- | |||
- | For What? Firstly we want to keep the docker image as small as possible, so that image delivery will be fast. Secondly the more packages/ | ||
- | |||
- | |||
- | ===== Clean Shutdown All Services, At The End Of Docker Build ===== | ||
- | |||
- | If we don’t do this, services will be killed at the end of docker image. | ||
- | |||
- | |||
- | ===== Add Verification Steps To Double Confirm The Image ===== | ||
- | |||
- | When things are changed, we may need to rebuild the docker image from time to time. To confirm everything is fine, we can add automatic verification logic in the docker build process. | ||
- | |||
- | Here is a sample with all above practices applied. | ||
- | |||
- | <code bash> | ||
- | ########## How To Use Docker Image ############### | ||
- | ## docker run -t -d --privileged -p 8022:22 \ | ||
- | ## denny/ | ||
- | ## | ||
- | ################################################## | ||
- | |||
- | FROM denny/ | ||
- | MAINTAINER Deny < | ||
- | ARG devops_branch=master | ||
- | ARG working_dir=/ | ||
- | ################################################## | ||
- | # Install basic packages | ||
- | RUN apt-get -yqq update && \ | ||
- | apt-get -yqq install curl && \ | ||
- | apt-get -yqq install openssh-server && \ | ||
- | apt-get install -y sudo lsb-release && \ | ||
- | # Install chef | ||
- | curl -L https:// | ||
- | # clean up files to make this docker layer smaller | ||
- | rm -rf / | ||
- | rm -rf / | ||
- | apt-get clean && apt-get autoclean | ||
- | ################################################## | ||
- | # checkout code | ||
- | RUN bash / | ||
- | git@github.com: | ||
- | ${devops_branch} && \ | ||
- | echo " | ||
- | \" | ||
- | > / | ||
- | |||
- | # Chef all-in-one deployment. This step takes minutes | ||
- | RUN echo " | ||
- | > / | ||
- | chef-solo --config / | ||
- | # Clean up to make docker image smaller | ||
- | rm -rf /tmp/* /var/tmp/* && \ | ||
- | rm -rf / | ||
- | rm -rf / | ||
- | rm -rf / | ||
- | apt-get clean && apt-get autoclean | ||
- | ################################################## | ||
- | |||
- | # Shutdown services | ||
- | RUN service couchbase-server stop || true && \ | ||
- | service elasticsearch stop || true && \ | ||
- | service nagios3 stop || true && \ | ||
- | service apache2 stop || true && \ | ||
- | service haproxy stop || true && \ | ||
- | service nagios-nrpe-server stop || true && \ | ||
- | rm -rf / | ||
- | rm -rf / | ||
- | rm -rf / | ||
- | rm -rf / | ||
- | |||
- | # Verify docker image | ||
- | RUN test -f / | ||
- | test -f / | ||
- | test -f / | ||
- | test -f / | ||
- | dpkg -s haproxy | grep " | ||
- | |||
- | # Clean up to make docker image smaller | ||
- | RUN rm -rf /tmp/* /var/tmp/* / | ||
- | rm -rf / | ||
- | rm -rf / | ||
- | apt-get clean && apt-get autoclean | ||
- | |||
- | EXPOSE 22 | ||
- | CMD ["/ | ||
- | ################################################## | ||
- | </ | ||
- | |||
docker/tips_for_building_a_docker_image.1476712082.txt.gz · Last modified: 2020/07/15 09:30 (external edit)