0

I would to start a container with specific shell script using docker compose. For example, a tomcat container starts initweb.sh creating the empty file /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

When I execute 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

The owner of my script initweb.sh is not root, so maybe that's why it is not executed but I don't know how to resolve this issue.

ARTESUAVE
  • 3
  • 1
  • Does this help? https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/30140911/can-i-control-the-owner-of-a-bind-mounted-volume-in-a-docker-image/30141637&ved=2ahUKEwislu-TpLXtAhXPVsAKHUJDCigQFjACegQIBxAC&usg=AOvVaw1sYuLdW2U9I6GFWClfHngT – Raman Sailopal Dec 04 '20 at 21:37
  • `/tmp` is not persistent... each time you start docker /tmp should always start empty – jordanm Dec 04 '20 at 22:30
  • Each `docker-compose run` command starts a new container with a different per-container filesystem. In your main container, when the startup script starts the server after touching the file, it should see the created file. – David Maze Dec 05 '20 at 01:46

1 Answers1

0

You need to make initweb.sh to behave like a server process :

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

then

~$ 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
Philippe
  • 20,025
  • 2
  • 23
  • 32