繁体   English   中英

docker compose:使用特定 shell 脚本启动容器的问题

[英]docker compose: issue to start a container with a specific shell script

我将使用 docker compose 启动一个具有特定 shell 脚本的容器。 例如,tomcat 容器启动 initweb.sh 创建空文件 /tmp/testweb

$ ls -lR 
.:
total 8
-rw-rw-r-- 1 xxxxxx xxxxxx  223 déc.   4 19:45 docker-compose.yml
drwxrwxr-x 2 xxxxxx xxxxxx 4096 déc.   4 19:37 web

./web:
total 4
-rwxrwxr-x 1 xxxxxx xxxxxx 31 déc.   4 19:37 initweb.sh


$ cat docker-compose.yml 
version: '3'
services:
  web:
    container_name: web
    hostname: web
    image: "tomcat:7.0-jdk8"
    ports:
      - 8080:8080
    volumes:
      - "./web/:/usr/local/bin/"
    command: sh -c "/usr/local/bin/initweb.sh"


$ cat web/initweb.sh 
#!/bin/bash
touch /tmp/testweb

当我执行 docker-compose up

$ docker-compose up -d
Creating network "tomcat_default" with the default driver
Creating web ... done

$ docker-compose run web ls -l /usr/local/bin/
total 4
-rwxrwxr-x 1 1000 1000 31 Dec  4 18:37 initweb.sh

$ docker-compose run web ls -l /tmp
total 4
drwxr-xr-x 1 root root 4096 Nov 24 01:29 hsperfdata_root

我的脚本 initweb.sh 的所有者不是 root,所以也许这就是它没有被执行的原因,但我不知道如何解决这个问题。

您需要使 initweb.sh 表现得像一个服务器进程:

#!/bin/bash
touch /tmp/testweb
sleep infinity

然后

~$ docker-compose up -d
Creating network "tmp_default" with the default driver
Creating web ... done
~$ docker-compose exec web ls -l /tmp
total 4 
drwxr-xr-x 1 root root 4096 Nov 24 01:29 hsperfdata_root
-rw-r--r-- 1 root root    0 Dec  4 22:39 testweb

暂无
暂无

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

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