将主机的环境变量传递给dockerfile

时间:2020-07-02 14:30:04

标签: amazon-web-services docker environment-variables dockerfile amazon-elastic-beanstalk

我的Dockerfile如下所示:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 5000
ADD target/*.jar app.jar
ENV JAVA_OPTS=""

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/.urandom -jar /app.jar"]

我想将RDS_HOSTNAME之类的环境变量传递给Docker容器。我应该如何修改该文件来做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以在期间通过ENV

  • 构建时间
  • 运行时间

要在构建时间中设置ENV,您需要在Dockerfile中进行修改。

def xr_reshape(A, dim, newdims, coords):
    """ Reshape DataArray A to convert its dimension dim into sub-dimensions given by
    newdims and the corresponding coords.
    Example: Ar = xr_reshape(A, 'time', ['year', 'month'], [(2017, 2018), np.arange(12)]) """


    # Create a pandas MultiIndex from these labels
    ind = pd.MultiIndex.from_product(coords, names=newdims)

    # Replace the time index in the DataArray by this new index,
    A1 = A.copy()

    A1.coords[dim] = ind

    # Convert multiindex to individual dims using DataArray.unstack().
    # This changes dimension order! The new dimensions are at the end.
    A1 = A1.unstack(dim)

    # Permute to restore dimensions
    i = A.dims.index(dim)
    dims = list(A1.dims)

    for d in newdims[::-1]:
        dims.insert(i, d)

    for d in newdims:
        _ = dims.pop(-1)


    return A1.transpose(*dims)

并在构建期间通过ARG RDS_HOSTNAME ENV RDS_HOSTNAME="${RDS_HOSTNAME}" ENV。

RDS_HOSTNAME

运行时间:

如评论中所述,您可以通过

docker build --build-arg RDS_HOSTNAME=$RDS_HOSTNAME -t my_image .

使用第二种方法,如果有人访问,您的Docker容器将不会提供容器信息,但是您每次运行容器时都需要通过,而第一次,您只需要在构建期间进行一次即可。