I am trying to execute a Bash file upon the start of the Container but it seems to not working as expected.
Below are the files involved (to keep things simpler i have simplfied the bash file)
Dockerfile :
FROM python:3.9-slim
# Create a working directory
WORKDIR /app
# Copy requirements
COPY requirements.txt app/
# Install tmux for the container
RUN apt-get update && \
apt-get install -y tmux && \
apt-get clean
# Install requirements
RUN pip install --no-cache-dir -r app/requirements.txt
# Copy all files into the container's app directory
COPY . /app/
# # Make the .sh file executable
RUN chmod +x /app/start.sh
CMD ["/app/start.sh"]
start.sh :
#!/bin/bash
# Simple program to output a text into a txt file
OUTPUT_FILE="text.txt"
TEXT="Hello, this is a sample text written to text.txt."
echo "$TEXT" > "$OUTPUT_FILE"
Docker CLI used:
Buid: Docker build -t image_name .
Running in interactive mode : Docker run -it image_name /bin/bash
once i am inside the container, i tried to locate the text.txt file in /app dir which is supposed to be generated upon the start of the container but it was not there then i manually ran the start.sh and it worked perfectly fine.
>Solution :
The issue occurs because when you run the container interactively with docker run -it image_name /bin/bash, it overrides the default CMD directive (/app/start.sh) and instead of running your start.sh script, the container starts with /bin/bash as the entry point.
To fix it, run the container normally using:
docker run -it image_name
This will execute start.sh and create the text.txt file as expected.