您如何根据目录名称命名Docker映像?

时间:2020-10-20 05:57:35

标签: bash docker dockerfile

我希望更频繁地使用docker,最终得到的模板目录结构有点像:

My-New-Project/
    |- Dockerfile
    |- build.sh
    |- start.sh
    |- src/...

build.shstart.sh只是通常的docker build -t my-new-project .docker run -p 8888:8888 -v $(pwd):/workdir my-new-project

但是,我需要为每个新项目修改build.shstart.sh中的图像名称-如何使用bash来基于目录基名称设置图像名称。

此外,如果有最佳做法禁止您做我正在做的事情,请告诉我。

1 个答案:

答案 0 :(得分:2)

结合How to set image name in Dockerfile?Get current directory name (without full path) in a Bash script的答案,您需要在build.sh和start.sh中做的只是:

const i18n = new NextI18Next({ otherLanguages: ['ko'], defaultLanguage: 'en', ns: ['translations'], defaultNS: 'translations', localePath: path.resolve('../../public/locales'), }) export default i18n export const appWithTranslation = i18n.appWithTranslation export const useTranslation = i18n.useTranslation // ... ///////// import { appWithTranslation, ... } from "..." appWithTranslation() docker build -t "${PWD##*/}"

或者,假设您还需要像示例(How to convert a string to lower case in Bash?)中的名称一样,使用小写字母:

docker run -p 8888:888 -v $(pwd):/workdir "${PWD##*/}"

dirname=${PWD##*/} docker build -t "${dirname,,}" 使用shell参数扩展从路径中删除其他目录,而${PWD##*/}是将大写转换为小写的另一个目录。