nicolasDevDes
nicolasDevDes

Reputation: 117

How to clone a GitHub repo inside a directory mapped as a volume using docker-compose.yml?

I have a docker-compose.yml similar to:

version: '3'
services:
  my_service:
    image: ....

    container_name: ...
    volumes:
      - "./data:/my_service/data"

    command: >
      ....

After running docker-compose pull, I run manually git clone [email protected]:....., on the host machine, to clone a private GitHub repo inside the shared directory (mapped as volume). This is working fine but I'd like to know if it's possible to handle this manual step with Docker.

Doing some research I found some examples using a Dockerfile but I'd prefer to use only the docker-compose.yml if possible.

Upvotes: 2

Views: 3937

Answers (2)

jujule
jujule

Reputation: 11531

Here's an exemple that use a custom command on an nginx image to git clone some repository before starting the server :

version: '3'
services:
  nginx:
    image: nginx:latest
    volumes:
      - "./data:/my_service/data"
    command: ["bash", "-c", "apt-get update; apt-get install -y git; rm -rf /usr/share/nginx/html; git clone https://github.com/cloudacademy/static-website-example /usr/share/nginx/html; nginx -g \"daemon off;\";"]
    ports:
      - 8080:80

It uses a single line string with all the appropriate commands, that you need to adapt to your use-case. Also, note that depending on the image you use you may or may not be able to add packages.

This may help for a quick workaround, but see @Antonio Petricca answer for a more robust solution.

Upvotes: 1

Antonio Petricca
Antonio Petricca

Reputation: 11026

The right way to accomplish this is to write a custom docker file to and address it by docker compose.

Here is an example:

docker-compose file fragment*

version: '3'

services:
  my_service:
    build:
      context: 
      dockerfile: my_service.dockerfile

    # ... continue ...

my_service.dockerfile

FROM some_image:some_tag

RUN apt update -y && \
    apt install -y git

RUN cd some_folder && \
    git clone some_uri

Upvotes: 3

Related Questions