Dockerfile如何将文件名设置为env

时间:2018-07-23 19:00:01

标签: docker dockerfile

dockerfile中是否可以将文件名设置为env?

例如下面是我的docker文件。我只想打印出文件Test的此名称部分。每个文件名看起来都一样,但_之前的文本不同。

例如,可以有Test_0.1.zip Hello_0.1.zip Moon_0.1.zip

我如何从Test Hello Or Moon这样的文件名中取出文本,并在dockerfile中将其设置为ENV?

FROM openjdk

ADD Test_0.1.zip .
RUN unzip Test_0.1.zip

3 个答案:

答案 0 :(得分:3)

如果我对您的理解正确,则想用一个变量替换文件名的第一部分。

您可以使用build args实现此目的。这是一个示例:

const Sequelize = require("sequelize");

var sequelize = new Sequelize("***database**", "**user**", "**password**", {
    host: "ec2-54-227-240-7.compute-1.amazonaws.com",
    dialect: "postgres",
    port: 5432,
    dialectOptions: {
        ssl: true
    }
});

var Employee = sequelize.define('Employee', {
    employeeNum: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    firstName: Sequelize.STRING,
    lastName: Sequelize.STRING,
    email: Sequelize.STRING,
    SSN: Sequelize.STRING,
    addressStreet: Sequelize.STRING,
    addressCity: Sequelize.STRING,
    addressState: Sequelize.STRING,
    addressPostal: Sequelize.STRING,
    maritalStatus: Sequelize.STRING,
    isManager: Sequelize.BOOLEAN,
    employeeManagerNum: Sequelize.INTEGER,
    status: Sequelize.STRING,
    department: Sequelize.INTEGER,
    hireDate: Sequelize.STRING
}, {
    createdAt: false, // disable createdAt
    updatedAt: false // disable updatedAt
});

var Department = sequelize.define('Department', {
    departmentId: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    departmentName: Sequelize.STRING
}, {
    createdAt: false, // disable createdAt
    updatedAt: false // disable updatedAt
});

function initialize() {
    return new Promise((resolve, reject) => {
        sequelize.sync({ force: true }).then(() => {
            Employee.create().then(function(employee) {
                resolve();
            }).catch(() => {
                reject("Unabale to sync the database");
            });

            Department.create().then(function(department) {
                resolve();
            }).catch(() => {
                reject("Unabale to sync the database");
            });
        });
    });
};

要构建图像,必须指定构建标签:

FROM openjdk
ARG filename
ADD ${filename}_0.1.zip .
RUN unzip ${filename}_0.1.zip

参考:

https://docs.docker.com/engine/reference/builder/#arg

答案 1 :(得分:0)

据我了解,您想提取输入文件名的一部分,并将其设置为环境变量。

每个RUN将打开一个新的shell,因此您不能在它们之间共享env变量。而且您也无法基于ENV

的输出来分配RUN

您最好的选择是创建一个ENTRYPOINT脚本来帮您完成。例如:

定义一个entrypoint.sh脚本来提取文本:

export FILE_TEXT=$(echo $filename | grep -o -e "Text\|Hello\|Moon")

您的Dockerfile将filename参数传递给入口点:

FROM openjdk
ARG filename
ENV ${filename}
ENTRYPOINT /bin/bash entrypoint.sh

答案 2 :(得分:0)

使用<script src="https://d3js.org/d3.v5.min.js"></script> <svg></svg>文件并在那里设置文件名。

.env文件:

.env

Dockerfile:

FILE_NAME=<filename>