githubdockerfiledocker-run

Issue with Git Pull while running Docker Container


I'm encountering an issue while attempting to execute a git pull command within a Docker container. Here's the Dockerfile configuration:

FROM ubuntu:latest
RUN apt update
RUN apt upgrade -y
RUN apt install git -y
RUN git clone https://github.com/Sanjay-Sagar-2005/PortOS-WrokSpace.git
CMD cd PortOS-WrokSpace && git pull && ls && pwd

I want the git repository to be up to date whenever the container start's

However, when I build and run the Docker container using docker run -it test, everything seems to work fine. The git pull command fetches the latest changes from the repository without any issues. enter image description here But when I run docker run -it test bash and manually check the repository's status, it appears that the repository is not up to date. It seems like the git pull command does not function properly when I enter the container interactively. enter image description here


Solution

  • If you execute docker run -it <container_name> bash, you start the container with the bash command interactively (only works when the container contains bash, in other cases it could be sh). This overrides the CMD [...] command from the dockerfile. You could use ENTRYPOINT for your docker build to change the order of executions (https://docs.docker.com/engine/reference/builder/#entrypoint).

    FROM ubuntu:latest
    
    # everything in one command to reduce layers
    RUN apt update && \
        apt upgrade -y && \
        apt install git -y
    
    RUN git clone https://github.com/Sanjay-Sagar-2005/PortOS-WrokSpace.git
    
    # change workdir, then you dont have to use cd <dir>
    WORKDIR ./PortOS-WrokSpace
    
    ENTRYPOINT ["git", "pull"]
    
    CMD ls && pwd