Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy from docker to host inside CI build

I would like to know if it would be possible to somehow copy file/folder from inside docker to host but copying itself is executed form inside docker.

The reason is that, for example:

  • Commit file to repo
  • CI kicks in
  • Docker installs dependencies, builds the website files
  • Website files is copied from docker to /var/www/my-website (host)

When I was searching for a solution I've seen this command a lot docker cp <containerId>:/file/path/within/container /host/path/target however this is executed from HOST. I want to make the whole process automated.

Possible solution of course is to not use docker but straight-up SSH, that's what I'm doing now, but that is not the best option IMO.

Here is example of my .gitlab-ci.yml file that will explain what I want to achieve.

image: ubuntu:16.04

build:
    stage: build
    script:
        - apt-get update
        - apt-get upgrade -yy
        - apt-get install hugo -yy # Static site generator
        - hugo build # Build the website
        - cp -R ./build/* /var/www/my-website/ # Copy to the web root

Here is my runner configuration

[[runners]]
  name = "DOCKER-TEST"
  url = "https://gitlab.com/ci"
  token = "{{token}}"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "ubuntu:16.04"
    privileged = true
    disable_cache = false
    volumes = ["/cache", "/home/stan:/builds/stanislavromanov/test-docker:rw"]
  [runners.cache]
    Insecure = false
like image 956
Stan Avatar asked Jun 29 '16 10:06

Stan


Video Answer


2 Answers

You should be able to set a Docker volume where a directory in the container is mounted to a directory of the host.

In case of GitLab CI runners this can be specified during runner registration or later by modifying /etc/gitlab-runner/config.toml. Example:

[[runners]]
  url = "https://gitlab.com/ci"
  token = TOKEN
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "ubuntu:16.04"
    privileged = true
    disable_cache = false
    volumes = ["/path/to/bind/from/host:/path/to/bind/in/container:rw"]

See documentation for more info.

like image 134
tmt Avatar answered Nov 10 '22 02:11

tmt


Copying from a container to the host is not possible with something like docker cp. What you can do though is to mount the host-directory into the container, e.g.:

$ docker run ... -v /var/www/my-website:/website-on-host ...

and adapt your cp command in .gitlab-ci.yml as follows:

cp -R ./build/* /website-on-host/ # Copy to the web root
like image 25
Pit Avatar answered Nov 10 '22 01:11

Pit