skip to Main Content

Here is my docker compose file.

version: '3'
services:
    web:
        image: emarcs/nginx-git
        ports:
            - 8081:80
        container_name: Avida
        working_dir: /usr/share/nginx/html
        command: bash -c "git clone https://github.com/raju/temp.git && echo "cloned successfully" && mv Avida-ED-Eco /usr/share/nginx/html && echo "Successfully moved the file""
             
        volumes:
             - srv:/srv/git
             - logs:/var/log/nginx
        environment:
            GIT_POSTBUFFER: 1048576  
        stdin_open: true 
        tty: true 
    firefox:
        image: jlesage/firefox
        ports:
            - 5800:5800
        volumes: 
            - my-vol:/data/db
        depends_on: 
            - web
volumes:
     my-vol:
        driver: local
     srv:
        driver : local
     logs:
        driver : local

What I am doing is I am using a docker nginx image with git installed on it and using that image to clone a directory and moving that directory to ngnix HTML path to read it. but after cloning the container exits and the code goes away. How can I keep container running without exiting with code 0. I tried some options such as tty: true and std_in: true nothing works.

container exit with code 0

2

Answers


  1. So keep it running.

        command: 
         - bash
         - -ec
         - | 
           git clone https://github.com/raju/temp.git
           echo "cloned successfully"
           mv -v Avida-ED-Eco /usr/share/nginx/html
           echo "Successfully moved the file""
           # keep it running
           sleep infinity
             
    

    But you would rather create a Dockerfile, in which you would prepare the image.

    I changed the format, you can read about | in YAML documentation. I replaced && by set -e, you can read about it https://mywiki.wooledge.org/BashFAQ/105 .


    If you want to do some modification and then start nginx, you should:

    • docker inspect the docker image and get the command it uses, or inspect the Dockerfile the image was built with
    • invoke the shell as you do and do the stuff that you want to do
    • and then call the command that it had previously calling
    Login or Signup to reply.
  2. You’re trying to set an individual container’s command: to copy some code into the container, every time it starts up. It’d be better to write a Dockerfile to do this work, only once. You can start a Dockerfile FROM any image you want and do any setup work you need to do in the container. It will inherit the ENTRYPOINT and/or CMD from the base image if you don’t override them.

    # Dockerfile
    FROM emarcs/nginx-git
    RUN mkdir /x
     && git clone https://github.com/raju/temp.git /x
     && mv /x/temp/Avida-ED-Eco /usr/share/nginx/html
     && rm -rf /x
    # (Can you `COPY ./ /usr/share/nginx/html/` instead?)
    

    Then in your Compose file, specify to build: an image instead of using the Docker Hub image:; do not override command:; and do not overwrite parts of the image with named volumes.

    version: '3.8'
    services:
      web:
        build: .
        ports:
          - 8081:80
        # this is all that's required
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search