简体   繁体   English

如何强制IBM Bluemix中的docker容器连接卷?

[英]How to force a docker container in IBM Bluemix to connect a volume?

is there any way to refuse to start a container where there is no volume connected to certain path? 有没有办法拒绝启动没有连接到某个路径的卷的容器?

I have a mysql container and want to block starting the container (exit with an error) where there is no volume connected to /var/lib/mysql to host volume. 我有一个mysql容器,并希望阻止启动容器(退出并显示错误),其中没有卷连接到/ var / lib / mysql到主机卷。 There is an option in IBM Bluemix for adding a volume to the container with the container's specified path. IBM Bluemix中有一个选项,用于使用容器的指定路径向容器添加卷。 I need to prevent starting the container if someone forget to add the volume. 如果有人忘记添加音量,我需要阻止启动容器。

Alternatively, is it possible to find out if there is a volume from the host to the container from inside the container? 或者,是否可以从容器内部找出从主机到容器的容积? (Adding a check to the entrypoint) (在入口点添加支票)

Container volumes contain the name of the container in the mount path, so basically you can create an entry point script in your Dockerfile that checks for the presence of the volume name. 容器卷包含装载路径中容器的名称,因此基本上您可以在Dockerfile中创建一个入口点脚本,用于检查卷名称是否存在。 I used df | grep volume-name 我用过df | grep volume-name df | grep volume-name to check if volume is mounted in the entry point script. df | grep volume-name用于检查是否在入口点脚本中装入了卷。 This is an example of running df on container with volume mounted (volume here is adsdatabase): 这是在装有卷的容器上运行df的示例(此处的卷是adsdatabase):

# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/docker-8:16-212573961-302136b327f5923d7c9b8c6c2cf62c0783 10190136 466892 9182572 5% /
tmpfs 132022832 0 132022832 0% /dev
shm 65536 0 65536 0% /dev/shm
nfsdal0901d.service.softlayer.com:/IBM01SV531277_366/adsdatabase 20971520 23168 20948352 1% /data
/dev/sdb 11522552336 200321280 10741487072 2% /etc/hosts
udev 132009268 4 132009264 1% /dev/tty

I recommend you don't use a common name, like "data", for the volume name or the grep would match that. 我建议您不要使用常用名称(如“data”)作为卷名称,否则grep会匹配该名称。

Here are the steps I did: 以下是我做的步骤:

  1. Create an entry point script that checks for the presence of the volume using df command (see sample entrypoint.sh file below) 创建一个入口点脚本,使用df命令检查卷的存在(请参阅下面的示例entrypoint.sh文件)
  2. Create a Dockerfile and add ENTRYPOINT command to run your entry point script (see sample Dockerfile file below) 创建一个Dockerfile并添加ENTRYPOINT命令来运行您的入口点脚本(参见下面的示例Dockerfile文件)
  3. Create a new image and push to your Bluemix registry, for example: 创建一个新映像并推送到Bluemix注册表,例如:

    $ docker build -t ads-volumesample . $ docker tag ads-volumesample registry.ng.bluemix.net/namespace/ads-volumesample $ docker push registry.ng.bluemix.net/namespace/ads-volumesample

  4. Create a volume - the name of the volume matches the check in the entry point script, for example: 创建卷 - 卷的名称与入口点脚本中的检查匹配,例如:

    $ cf ic volume create adsdatabase

  5. Create a new container and add the new volume: 创建一个新容器并添加新卷:

    $ cf ic run -v adsdatabase:/data --name ads-volumesample1 registry.ng.bluemix.net/namespace/ads-volumesample

If you do not add the volume when creating the container, the container will fail and will automatically shutdown in a few minutes. 如果在创建容器时未添加卷,容器将失败并在几分钟后自动关闭。

entrypoint.sh entrypoint.sh

#!/bin/bash
set -e

if df | grep adsdatabase > /dev/null; then
   echo "Found volume"
else
   echo "Volume not found"
   exit 1
fi

exec "$@"

Dockerfile Dockerfile

FROM registry.ng.bluemix.net/ibmnode

ADD ./app /node

ENV NODE_ENV production

RUN cd /node && npm install
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 3000

CMD ["node", "/node/app.js"]

您可以简单地运行一个脚本来检查应该挂载卷的路径(在您的情况下是/ var / lib / mysql)是否包含卷内容(它确认卷已正确挂载)请记住: - 即使未安装卷,也存在卷装入路径,您必须检查其内容而不是路径本身 - 如果您在第一次启动卷时运行初始化过程,您可以预期在此刻卷为空并且装载point也是空的:所以在初始化时跳过检查,或者具有特定的情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM