天天看點

docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項

文章目錄

  • 1、前置
  • 2、entrypoint
  • 3、實際使用
    • ①: 确認鏡像設定的ENTRYPOINT是單指令還是可執行檔案
    • ②: 準備可執行檔案
    • ③: 運作鏡像
    • ④: 效果對比
      • 不覆寫效果:
      • 覆寫效果:
  • 4、注意事項

1、前置

1、entrypoint指令介紹

2、實際使用

3、注意事項

2、entrypoint

Dockerfile:

1. FROM java:8
2. MAINTAINER chaim
3. EXPOSE 8080
4. ............
5. ............
6. ENTRYPOINT ["./entrypoint.sh"]
7. #ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/powerBank.jar"]

           

直白一點說, entrypoint指令就是覆寫ENTRYPOINT(第6行或者第7行)指令的. 官方話說就是覆寫預設入口點

官網介紹位址: https://docs.docker.com/engine/reference/run/:

The ENTRYPOINT of an image is similar to a COMMAND because it specifies what executable to run when the container starts, but it is (purposely) more difficult to override. The ENTRYPOINT gives a container its default nature or behavior, so that when you set an ENTRYPOINT you can run the container as if it were that binary, complete with default options, and you can pass in more options via the COMMAND. But, sometimes an operator may want to run something else inside the container, so you can override the default ENTRYPOINT at runtime by using a string to specify the new ENTRYPOINT.

可執行檔案

docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項

3、實際使用

我們以鏡像: bladex/sentinel-dashboard 為列子進行操作一下

①: 确認鏡像設定的ENTRYPOINT是單指令還是可執行檔案

按标準來說, ENTRYPOINT是要配置可執行檔案的, 這樣别人可以配置對應的參數, 比如JVM等資料, 也友善使用者自行定義

第一種确認方式, 就是直接在dockerhub檢視:

docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項
docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項

第二種使用指令:

# 直接檢視鏡像詳情
docker inspect bladex/sentinel-dashboard:1.7.1
           
docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項

②: 準備可執行檔案

改個端口: 8898 改個項目名: sentinel

sentinel就是執行的jar包, 直接docker exec -it *** bash, 就能找到, 自己在使用的需要确認一下自己的實際情況

docker-sentinel.sh

#!/bin/bash

java -Djava.security.egd=file:/dev/./urandom -Dserver.port=8898 -Dcsp.sentinel.api.port=8719 -Dcsp.sentinel.dashboard.server=localhost:8898 -Dproject.name=sentinel -jar /bladex/sentinel/app.jar
           

③: 運作鏡像

docker run 方式:

docker run --name sentinel -p 8858:8898 -v "E:/Docker/sentinel/sh/:/docker/sentinel/" --entrypoint "/docker/sentinel/docker-sentinel.sh" -d  bladex/sentinel-dashboard:1.7.1
           

這個是docker-compose方式的:

# docker-compose方式(一樣, 采用了覆寫操作, environment就沒必要了)
# Compose 版本 Version 2支援更多的指令。Version 1将來會被棄用。
version: "3"

# 定義服務
services:

  # 為project定義服務
  sentinel:
    image: bladex/sentinel-dashboard:1.7.1
    ports:
      - 8858:8898
    # 挂載
    volumes:
      - "E://Docker/sentinel/sh/:/docker/sentinel/"
    # 覆寫預設入口點。
    entrypoint: /docker/sentinel/docker-sentinel.sh
    restart: always
    container_name: sentinel
    privileged: true
           

④: 效果對比

不覆寫效果:

docker run --name sentinel -p 8858:8858 -d  bladex/sentinel-dashboard:1.7.1
           
docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項
docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項

覆寫效果:

docker run --name sentinel -p 8858:8898 -v "E:/Docker/sentinel/sh/:/docker/sentinel/" --entrypoint "/docker/sentinel/docker-sentinel.sh" -d  bladex/sentinel-dashboard:1.7.1
           
docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項
docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項

4、注意事項

1、挂載也可以指定到檔案:

2、 –entrypoint必須是可執行檔案 , 下面這種就是有問題的

docker run --name sentinel -p 8858:8898 -v "E:/Docker/sentinel/sh/:/docker/sentinel/" --entrypoint "java -Dserver.port=8858 -Dproject.name=sentinel -jar /bladex/sentinel/app.jar" -d  bladex/sentinel-dashboard:1.7.1
           
docker覆寫鏡像預設指令之docker entrypoint1、前置2、entrypoint3、實際使用4、注意事項

3、docker-compose介紹: https://docs.docker.com/compose/compose-file/compose-file-v3/

Dockerfile介紹:https://docs.docker.com/engine/reference/builder/