下方為dockerfile的內容
FROM alpine:latest
RUN apk update
RUN apk --no-cache add curl
ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.12/supercronic-linux-amd64 \
SUPERCRONIC=supercronic-linux-amd64 \
SUPERCRONIC_SHA1SUM=048b95b48b708983effb2e5c935a1ef8483d9e3e
RUN curl -fsSLO "$SUPERCRONIC_URL" \
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
&& chmod +x "$SUPERCRONIC" \
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
COPY my-cron /app/my-cron
WORKDIR /app
VOLUME ["/app"]
# RUN cron job
CMD ["/usr/local/bin/supercronic", "/app/my-cron"]
- 構建鏡像
在終端機執行:
docker image build -t my-cron .
docker image ls
- 創建容器(不指定-v參數)
docker run -d my-cron
docker volume ls
此時Docker會自動創建一個隨機名字的volume,存儲在Dockerfile定義的volume
VOLUME ["/app"]
創建容器(指定-v參數)
在創建容器的時候通過 -v 參數,我們可以手動的指定需要創建Volume的名字,以及對應於容器內的路徑,這個路徑是可以任意的,不必需要在Dockerfile裡通過VOLUME來定義。
把原本Dockerfile裡的VOLUME刪除(如下)
FROM alpine:latest
RUN apk update
RUN apk --no-cache add curl
ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.12/supercronic-linux-amd64 \
SUPERCRONIC=supercronic-linux-amd64 \
SUPERCRONIC_SHA1SUM=048b95b48b708983effb2e5c935a1ef8483d9e3e
RUN curl -fsSLO "$SUPERCRONIC_URL" \
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
&& chmod +x "$SUPERCRONIC" \
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
COPY my-cron /app/my-cron
WORKDIR /app
# RUN cron job
CMD ["/usr/local/bin/supercronic", "/app/my-cron"]
重新build鏡像,然後創建容器,加-v參數:
docker image build -t my-cron .
docker container run -d -v cron-data:/app my-cron
docker volume ls
docker volume inspect cron-data
環境清理
強制刪除所有容器,系統清理和volume清理
docker rm -f $(docker container ps -aq)
docker system prune -f
docker volume prune -f