skip to Main Content

I have a Dockerfile which works if i make a container out of it, but doesnt work completely when i run it via compose yaml file.

so below is the Dockerfile, and i copy a shell.sh from my localmachine to container, all ok in this case.

Dockerfile:

FROM alpine:latest
LABEL maintainer="[email protected]"
RUN mkdir -p /tmp/hasham
COPY ./shell.sh /tmp/hasham/
RUN chmod 775 /tmp/hasham/shell.sh
RUN echo '*/1  *  *  *  *    /tmp/hasham/shell.sh' > /etc/crontabs/root
CMD crond && sleep infinite


However the problem is whenever I run this dockerfile as a part of docker compose, the shell script created on the container is empty. the filename is there in the container but with no contents. I dont understand why is this happening only when running through compose. I tried with another simple text file also and got the same problem, file is created but is with zero size and no contents.

please can someone tell me what am i doing wrong?

below is the yaml file im using and it is referencing the above Dockerfile for container generation.

test.yaml

services:
    my_app:
        container_name: myapp
        build:
            dockerfile: ./alpine.Dockerfile
        networks:
            - mynetwork
networks:
    mynetwork:
        name: mynetwork
        driver: bridge

please help !

thanks

H

dont think its anything to do with permissions?

docker build works fine (create image and then container)

docker compose doesnt work fine, i get empty /tmp/hasham/shell.sh (composing using the above dockerfile)

docker compose --project-name my_project -f test.yaml up -d

2

Answers


  1. Chosen as BEST ANSWER

    resolved.

    so whenever I was running the compose command, it was not making a new image but was using an old image which was created when i was testing, and probably during that very first build that empty shell.sh was put into the image. on every subsequent run of compose up and compose down, it was just creating and deleting the containers.

    i had to delete the image and then run compose which created a new correct image and now everything is fine.


  2. You can force docker-compose to rebuild the image using the --build option of the up subcommand, described here.

    docker compose --project-name my_project -f test.yaml up --build -d

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search