Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to execute Bash inside Docker Container

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading