映象相關命令
# 下載映象
docker pull xxx
# 搜素映象
docker search xxx
# 檢視已經下載了哪些映象
docker images
# 檢視已下載映象的id
docker images -q
# 刪除映象
docker rmi containerId/SOURCE_IMAGE[:TAG]
#刪除所有映象
docker rmi $(docker images -q)
docker images -q | xargs docker rmi
# 刪除tag為<none>的映象
docker rmi | xargs docker images | awk '{if ($2 == "<none>" )print $3}' | xargs docker rmi -f
容器相關的命令
# 載入一個本地映象
docker load -i tomcat-8.0-jre.tar
# 啟動一個容器
docker run -dp 8080:8080 tomcat-8.0-jre --name tomcat
# 檢視正在執行的容器
docker ps
# 檢視全部容器的id
docker ps -q
# 檢視全部容器
docker ps -a
#執行 重啟 停止 殺掉容器
docker start/restart/stop/kill containerId/name
# 刪除已建立的容器
docker rm containerId/name:tag
docker rm $(docker ps -q)
# 檢視日誌
docker logs containerId/name
# 實時顯示日誌
docker logs -tf containerId/name
# 行首新增時間 的日誌
docker logs -tail 5 containerId/name
# 檢視容器資源情況
docker top containerId
# 進入容器的互動模式
docker exec -it containerId bash
# 複製 容器中的檔案、目錄到 宿主機
docker cp containerId:/root/test.txt /root/
# 複製 宿主機的檔案、目錄到 容器中
docker cp a.txt containerId:/root/
# 打包一個映象
docker commit -m "description xx" -a "author" containerId SOURCE_IMAGE[:TAG]
docker save redis:latest -o redis-cluster.tar
# 上傳到docker hub倉庫
docker tag redis:latest docker.io/whalefall541/redis:0.01
docker push docker.io/whalefall541/redis:0.01
# 檢視docker 網路配置
docker network ls
# 建立自定義網橋
docker mynetwork create mynetwork
# 將容器指定到 建立的網橋上
docker run -dp 80:80 --network mynetwork redis
# 刪除網橋
docker newnetwork rm mynetwork
# 檢視網橋
docker inpsect mynetwork
# 資料卷指定
# docker run -dp 80:80 tomcat -v /root/webapps:/root/webapps
docker run -dp 80:80 tomcat -v aa:/root/webapps
# 資料卷建立
docker volume create volume_1
# 刪除所有未使用的卷
docker volume prune
docker rm volume_1
# 檢視資料卷
docker inspect volume_1
Dockerfile
- ADD
- COPY
- ENV
- EXPOSE
- FROM
- LABEL
- STOPSIGNAL
- USER
- VOLUME
- WORKDIR
- ONBUILD (when combined with one of the supported instructions above)
詳細教程請看官網[Dockerfile][https://docs.docker.com/engine/reference/builder/]
Understand how CMD and ENTRYPOINT interact
Both CMD
and ENTRYPOINT
instructions define what command gets executed when running a container.
There are few rules that describe their co-operation.
Dockerfile should specify at least one of
CMD
orENTRYPOINT
commands.ENTRYPOINT
should be defined when using the container as an executable.CMD
should be used as a way of defining default arguments for anENTRYPOINT
command
or for executing an ad-hoc command in a container.CMD
will be overridden when running the container with alternative arguments.
The table below shows what command is executed for different ENTRYPOINT
/ CMD
combinations:
No ENTRYPOINT | ENTRYPOINT exec_entry p1_entry | ENTRYPOINT ["exec_entry", "p1_entry"] | |
---|---|---|---|
No CMD | error, not allowed | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry |
CMD ["exec_cmd", "p1_cmd"] | exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry exec_cmd p1_cmd |
CMD ["p1_cmd", "p2_cmd"] | p1_cmd p2_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry p1_cmd p2_cmd |
CMD exec_cmd p1_cmd | /bin/sh -c exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd |
Note
If
CMD
is defined from the base image, settingENTRYPOINT
will
resetCMD
to an empty value. In this scenario,CMD
must be defined in the
current image to have a value.You can override the ENTRYPOINT setting using --entrypoint,
but this can only set the binary to exec (no sh -c will be used).
一個簡單的案例
拷貝springboot檔案到 容器並 指定spring配置檔案執行
FROM java
ENV JARNAME="demo-0.0.1-SNAPSHOT.jar"
WORKDIR /root/app
RUN /bin/bash -c "echo 'aaaa' >> 1.txt"
EXPOSE 80
VOLUME ["/root/app"]
ENTRYPOINT ["java","-jar","demo-0.0.1-SNAPSHOT.jar", "-Dspring.config.location"]
# 注意此處的引數 覆蓋時要制定容器內 或者容器能訪問到的位置才行
CMD ["application.yml"]
#ENTRYPOINT ["echo","/root"]
#CMD ["/root/app"]
docker build -t springboot:0.01 .
docker run -v /dockerlearn/dockerfilelearn:/root/app -p 80:80 --network rediscluster_default --rm springboot:0.01
Note
- 如果
ENTRYPOINT ["exec_entry", "p1_entry"]
後面還有
**CMD ["p1_cmd", "p2_cmd"]**
那麼CMD中的引數將全部追加到 ENTRYPOINT
當docker run
後面跟引數時會覆蓋掉CMD中的引數- CMD ENTRYPOINT 陣列形式 都是exec模式,不支援解析本地變數
如果想解析 使用這個命令ENTRYPOINT ["sh","-c","java -jar $JARNAME"]
- 如果使用了
ENTRYPOINT ["sh","-c",..]
或者直接使用ENTRYPOINT "echo Hello world"
那麼直接就無法覆蓋引數了
參考文獻