I collect some of the little skills used in my Dockerfiles and also keep in the record for my reference.
--build-arg & ARG
you can pass your parameter via --build-arg in your docker build command and use ARG to receive the value in Dockerfile. For instance:
Use wget to download a directory
ENTRYPOINT and CMD instruction
USER instruction
Multi-Stage-Build
https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds
--build-arg & ARG
you can pass your parameter via --build-arg in your docker build command and use ARG to receive the value in Dockerfile. For instance:
<<Your Dockerfile>>
FROM tensorflow/tensorflow:latest-gpu
ARG MCUT_USER # get parameter value from docker build --build-arg=XXXX
...
...
...
$ sudo docker build --build-arg UBUNTU_USER=danny -t my_image:v0 .
Use wget to download a directory
<<Your Dockerfile>>
...
ARG MODEL1_LINK=https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin_20190329_105530/pedestrian-detection-adas-0002/
# Download OpenVINO pre-trained models
RUN wget -r -np -nH --cut-dirs=5 -R index.html* $MODEL1_LINK
...
ENTRYPOINT and CMD instruction
<<Your Dockerfile>>
...
ENTRYPOINT ["fixuid"]
CMD ["bash", "/startup.sh"]
# which means ==> "fixuid bash /startup.sh"
USER <user>[:<group>] or
USER <UID>[:<GID>]
<<Your Dockerfile>>
...
# Add user & group ==> danny:danny
RUN groupadd -r danny && \
useradd -d /workspace -r -g danny danny && \
usermod -s /bin/bash danny
# or add user & group id
RUN addgroup --gid 1000 docker && \
adduser --uid 1000 --ingroup danny && \
--home /workspace --shell /bin/bash && \
--disabled-password --gecos "" danny
USER danny:danny
Multi-Stage-Build
https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds
<<Your Dockerfile>>
...
FROM gcr.io/kaggle-images/python-tensorflow-whl:1.13.1-py36-2 as tensorflow_whl
...
COPY --from=tensorflow_whl /tmp/tensorflow_gpu/*.whl /tmp/tensorflow_gpu/
...
P.S: COPY --from=0 ==> means to use the previous build's resultReference:
Example:
alpine-nginx.dockerfile
FROM alpine:3.4
ENV NGINX_VERSION 1.11.1
RUN apk --update add pcre-dev openssl-dev \
&& apk add --virtual build-dependencies build-base curl \
&& curl -SLO http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz \
&& tar xzvf nginx-${NGINX_VERSION}.tar.gz \
&& cd nginx-${NGINX_VERSION} \
&& ./configure \
--with-http_ssl_module \
--with-http_gzip_static_module \
--prefix=/usr/share/nginx \
--sbin-path=/usr/local/sbin/nginx \
--conf-path=/etc/nginx/conf/nginx.conf \
--pid-path=/var/run/nginx.pid \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
&& make \
&& make install \
&& ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
&& cd / \
&& apk del build-dependencies \
&& rm -rf \
nginx-${NGINX_VERSION} \
nginx-${NGINX_VERSION}.tar.gz \
/var/cache/apk/*
VOLUME ["/var/cache/nginx"]
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
apk --update add
什麼時候apk --no-cache add
Alpine Linux 從 3.3 開始有一個帶有 apk 的選項--no-cache
。
以前--update add
用with安裝後好像rm -rf /var/cache/apk/*
會
刪除不必要的垃圾文件,現在--no-cache
可以了。
apk add --virtual
用於
--virtual
您可以通過指定apk 來安裝軟件包,
但如果您使用此選項使用臨時名稱進行安裝,您
可以稍後使用臨時名稱一次性刪除它們。
- 虛擬的
RUN apk add --no-cache --virtual .ruby-builddeps curl gcc make .. && \
curl ... && \
... && \
make install ... && \
apk del .ruby-builddeps
這樣可以防止被刪除,非常方便!
No comments:
Post a Comment