Run bash script file when starting a custom docker container created from a custom Dockerfile
My working directory has the following files:
.
└── docker-compose.yml
where:
/docker-compose.yml
version: '3'
services:
webserver:
image: nginx:1.15.12-alpine
container_name: webserver
restart: unless-stopped
ports:
- "80:80"
To start the webserver I just do:
$ docker-compose up -d webserver
To enter into that container I do:
$ docker exec -it webserver /bin/sh
At this point so far so good.
Now my goal is: when the container: webserver
starts, run multiple commands.
For that, I tried:
- Changed this line on the:
/docker-compose.yml
[old] image: nginx:1.15.12-alpine
[new] build: ./docker-files/nginx-1.15.12-alpine
- Created the following two files:
/docker-files/nginx-1.15.12-alpine/Dockerfile
FROM nginx:1.15.12-alpine
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
/docker-files/nginx-1.15.12-alpine/docker-entrypoint.sh
#!/bin/sh
# Multiple commands below
echo "Hello World" > /document.txt
but now, when I run:
$ docker-compose up -d webserver
and then:
$ docker ps -a
I get as Status: Restarting (1) 5 seconds ago
;
Then, when I run:
$ docker-compose logs webserver
I get:
webserver | standard_init_linux.go:211: exec user process caused "no such file or directory"
webserver | standard_init_linux.go:211: exec user process caused "no such file or directory"
webserver | standard_init_linux.go:211: exec user process caused "no such file or directory"
webserver | standard_init_linux.go:211: exec user process caused "no such file or directory"
webserver | standard_init_linux.go:211: exec user process caused "no such file or directory"
Any idea of what I’m doing wrong here?
I just want to run the commands inside: docker-entrypoint.sh
which creates a the file: document.txt
on the root directory of the container.
Thanks!
Source: Docker Questions