Dockerfile
:
FROM ubuntu:latest
ARG DEBIAN_FRONTEND=nointerative
WORKDIR /var/photogram
COPY . .
RUN chmod +x /var/photogram/install.sh
CMD /var/photogram/install.sh
install.sh
:
#!/bin/bash
echo "hello world"
touch hello.txt
touch sad.txt
Structure of files:
Tholkappiar2003@docker:~/docker$ tree
.
├── Dockerfile
└── install.sh
1 directory, 2 files
Docker Build: docker build -t photo:local .
Docker Run: docker run -it photo:local bash
No Errors and nothing.
After running the container:
root@8dbd47fa58e3:/var/photogram# ls
Dockerfile install.sh
I can run the script inside the Docker image which gives the output but it should be done when starting the container automatically.
There is no hello.txt
file or sad.txt
file which would be created by the install.sh
script.
The commands in install.sh
should be executed when running the container.
2
Answers
You are overwriting the
CMD
you defined in yourDockerfile
. So if you want to execute the script when the container starts, then use:If instead you want to execute the script whenever you open a bash session, then place the script in
~/.bashrc
:This is because in the
Dockerfile
you use aCMD
and when you start a new container, you specified thebash
command which automatically replaces yourCMD
. On the other hand, if you want the script to be executed when creating a new container, putENTRYPOINT
instead.Run new container:
This will cause the script to be executed but you will no longer be able to connect in
bash
unless you enter anotherENTRYPOINT
Run container and connect shell: