chr4 / cache_warmer

Small tool to warm-up HTTP caches, e.g. services like nginx, written in Rust
GNU General Public License v3.0
20 stars 3 forks source link

How to run from docker #1

Closed rvencu closed 4 years ago

rvencu commented 4 years ago

Hi, please be kind and explain how to run the main command from the docker container. Thanks.

chr4 commented 4 years ago

The provided Dockerfile is to build the image using Docker, e.g. if you need to cross-compile the binary for Linux.

I've been running it natively, because of performance reasons. You can easily adjust the Dockerfile to not only build the binary but also run it, just use RUN for building it, and then CMD for running.

rvencu commented 4 years ago

Good hint! I seek to keep this cache warmer separate from the main server configuration but still hosted on it, so docker seemed a very good idea. Thanks

rvencu commented 4 years ago

I think there one more problem, how can I pass the local file (list or URL) into the docker instance? Would it work as an url or something?

chr4 commented 4 years ago

You could either mount your urls.txt using docker's -v command, or (haven't tested this properly) try piping it in via stdin:

docker run -i my-cache-warmer cache_warmer /dev/stdin < urls.txt
rvencu commented 4 years ago

I have done this dockerfile

FROM ubuntu:16.04
RUN apt-get update \
 && apt-get install -y --no-install-recommends --no-install-suggests \
    ca-certificates curl libssl-dev build-essential pkg-config \
 && rm -rf /var/lib/apt/lists/*
RUN curl https://sh.rustup.rs -sSf | \
    sh -s -- --default-toolchain stable -y
ENV PATH=/root/.cargo/bin:$PATH
ENV CARGO_HOME=/app/.cargo
WORKDIR /app
RUN cargo build --release
CMD ["cache_warmer", "--threads 24", "--user-agent 'Richard-Rocket-Refresh'", "/dev/stdin < /home/wikitip/sitemap.txt"]

and the docker-composer up command returns the same output like the previous file, it exits immediately after compilation

I tried then this command

docker run -i cd0d0c6fc3ca cache_warmer /dev/stdin < /home/wikitip/sitemap.txt

and got this error

docker: Error response from daemon: OCI runtime create failed: container_linu o:349: starting container process caused "exec: \"cache_warmer\": executable e not found in $PATH": unknown. ERRO[0000] error waiting for container: context canceled

finally, is it possible to build the native exec into a centos 7 machine then copy just the executable and run in in another centos 7 machine? I do not want to clog the production server with a lot of libraries (rust and all dependencies).

rvencu commented 4 years ago

I was able to finally achieve what I needed.

I copied the sitemap.txt file into the cache_warmer folder on the main OS and it is mapped together with the rest of the files into /app inside the container.

Then this docker-composer.yml file executes the task

version: '3'
services:
  cache_warmer:
    build: ./
    image: chr4/rust
    volumes:
      - ./:/app
    command:
      ['/app/target/release/cache_warmer','--threads', '24', '--user-agent', 'Richard-Rocket-Refresh', '/app/sitemap.txt']
chr4 commented 4 years ago

Perfect! Good to hear. I'd close this ticket then!

For the record: For the/dev/stdin approach, the data piped in via < would need to come from the host system.

It might work if you put this in your Dockerfile (also, as you've found out with docker-compose, all args need to be split):

CMD ["cache_warmer", "--threads", "24", "--user-agent", "Richard-Rocket-Refresh", "/dev/stdin"]

And then run the command like this:

docker run -i cache_warmer < /home/wikitip/sitemap.txt

But this wouldn't be possible within docker-compose I don't think.