docker - 将dockerfile + bash脚本转换为OpenShift部署配置

标签 docker openshift

我有这个dockerfile:

FROM centos:latest
COPY mongodb.repo /etc/yum.repos.d/mongodb.repo

RUN yum update -y && \
    yum install -y mongodb-org mongodb-org-shell iproute nano

RUN mkdir -p /data/db && chown -R mongod:mongod /data

我可以使用docker在本地构建和运行:
docker build -t my-image .
docker run -t -d -p 27017:27017 --name my-container my-image
docker cp mongod.conf my-container:/etc/mongod.conf
docker exec -u mongod my-container "mongod --config /etc/mongod.conf"

现在,我想在OpenShift中运行容器。我设法建立并将图像推送到 namespace 。并创建了下面的运行容器的 deploymentConfig -就像我可以在本地进行的操作一样。
- apiVersion: v1
  kind: DeploymentConfig
  metadata:
    name: my-app
    namespace: my-namespace
  spec:
    replicas: 1
    selector:
      app: my-app
    template:
      metadata:
        labels:
          app: my-app
          deploymentconfig: my-app
      spec:
        containers:
        - name: my-app
          image: ${IMAGE}
          command: ["mongod"]
          ports:
           - containerPort: 27017 
          imagePullPolicy: Always

当我单击部署时,镜像已成功拉出,但出现错误:
exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating

为什么在/data/db文件夹上没有读/写?从上面的 Dockerfile 可以看出,该文件夹已创建,并且 mongod 用户是该文件夹的所有者。

是否有必要以某种方式授予 mongod 用户对该部署文件夹中的文件夹的读/写权限?

最佳答案

  • Docker文件将以未知,没有任何歧义的非root用户身份运行(请简单地想象一下mongodb以1000000001用户身份运行,但不能保证将使用所选的数字)。这可能意味着mongod用户不是导致这些问题的选定用户,因此请check the documentation获取支持任意用户ID的准则。

    For an image to support running as an arbitrary user, directories and files that may be written to by processes in the image should be owned by the root group and be read/writable by that group. Files to be executed should also have group execute permissions.

    Adding the following to your Dockerfile sets the directory and file permissions to allow users in the root group to access them in the built image:

    RUN chgrp -R 0 /some/directory && \   <- Set the group to 0 (the root group)
    chmod -R g=u /some/directory          <- Here you will set your permissions
    
  • 您的mongod.conf可以作为 ConfigMap 挂载-一个Openshift对象,通常用于管理可以作为(只读)卷
  • 挂载的配置

    编辑-命令说明
  • chgrpchownchown在这里也可以很好地使用,但是由于我们只对更新“组”感兴趣,因此chgrp提供了一点额外的安全性,以确保这是唯一更改的内容(由于输入错误的命令等)。
  • chmod -R g=u
  • chmod用于更改文件权限;
  • -R告诉它通过路径递归(如果存在子目录,它们也将获得相同的权限);
  • g=u的意思是“组=用户”或“为组赋予与该用户已经存在的权限相同的权限”
  • 关于docker - 将dockerfile + bash脚本转换为OpenShift部署配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55796409/

    相关文章:

    amazon-web-services - 在 AWS Fargate 上暂停容器

    rest - 相当于流程模板命令的 Openshift REST API 是什么

    javascript - 是否有必要在 OpenShift 上使用 Forever.js?

    mongodb - 如何等待 docker 容器启动并运行?

    docker - 无法从 docker hub 拉取公共(public) MongoDB 镜像

    docker - Docker运行<image>总是在standard_init_linux.go:207上出错:exec用户进程在Windows上导致 “no such file or directory”

    python - pip install 返回 -9

    git - Openshift v3 Online 上的构建器角色

    docker - 除了 root 之外,您如何运行 Openshift Docker 容器?

    docker - 如何在Windows 10中安装minikube和docker?