Skip to content

Instantly share code, notes, and snippets.

@gkhays
Last active November 16, 2019 00:21
Show Gist options
  • Save gkhays/2e02eb5be40ff28ef8836b18a7c76a6a to your computer and use it in GitHub Desktop.
Save gkhays/2e02eb5be40ff28ef8836b18a7c76a6a to your computer and use it in GitHub Desktop.
Isolate building and executing a Rust application in a Docker container

Isolating a Rust Application in a Docker Container

HN user flatroze posted a CLI tool for saving web pages as a single file. A comment posted by mike-cardwell demonstrated how to compile and run the utility within a Docker container.

Compile and install:

$ git clone https://github.com/Y2Z/monolith.git
$ cd monolith
$ docker run --rm -w "$(pwd)" -v "$(pwd):$(pwd)" -u "$(id -u):$(id -g)" rust cargo install --path .

Now run:

$ cd target/release
$ docker run --rm -w "$(pwd)" -v "$(pwd):$(pwd)" -u "$(id -u):$(id -g)" rust ./monolith https://www.grepular.com > grepular.html

A quick look at target/release

src/monolith/target/release$ ls -la
total 13764
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:33 .
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:22 ..
-rwxrwxrwx 1 ghays ghays        0 Aug 23 11:17 .cargo-lock
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:17 .fingerprint
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:17 build
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:22 deps
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:17 examples
-rw-rw-rw- 1 ghays ghays  1843261 Aug 23 11:34 grepular.html
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:17 incremental
-rwxrwxrwx 1 ghays ghays      221 Aug 23 11:22 libmonolith.d
-rwxrwxrwx 2 ghays ghays  2074980 Aug 23 11:22 libmonolith.rlib
-rwxrwxrwx 2 ghays ghays 10169352 Aug 23 11:22 monolith
-rwxrwxrwx 1 ghays ghays      253 Aug 23 11:22 monolith.d
drwxrwxrwx 1 ghays ghays     4096 Aug 23 11:17 native

Links

  1. Show HN: CLI tool for saving web pages as a single file
  2. monolith - Save HTML pages with ease

The same comment poster referenced a project to isolate node.js

SaferNode - A safer way to run node

Running Docker Under WSL

At a quick glance I wasn't sure how to map the volume mounts under Windows and didn't want to take the time, so I set up Docker under WSL. Note: I altered the way the file system mounts -- I changed it from /mnt/c to /c, e.g.

ghays@DESKTOP-AQA8MFG:/d/Users/ghays/src/monolith$ df
Filesystem      1K-blocks      Used  Available Use% Mounted on
rootfs          487822332  92280716  395541616  19% /
none            487822332  92280716  395541616  19% /dev
none            487822332  92280716  395541616  19% /run
none            487822332  92280716  395541616  19% /run/lock
none            487822332  92280716  395541616  19% /run/shm
none            487822332  92280716  395541616  19% /run/user
cgroup          487822332  92280716  395541616  19% /sys/fs/cgroup
C:\             487822332  92280716  395541616  19% /c
D:\            1953512444 326810100 1626702344  17% /d

To do this, I edited wsl.conf

sudo vim/etc/wsl.conf

# Now make it look like this and save the file when you're done:
[automount]
root = /
options = "metadata"

Setting Up Docker for Windows and WSL to Work Flawlessly

@gkhays
Copy link
Author

gkhays commented Aug 25, 2019

I also got it working with docker-compose. It's not intuitive, but works.

docker-compose.yml

version: '3'
services:  
  monolith:
    image: rust
    command: cargo install --path .
    volumes:
      - '$PWD:$PWD'
    working_dir: '$PWD'
    user: '$UID:1000'
    #uid: '$UID' # $(id -u)
    #gid: '1000' # $(id -g)

Then, to install:

docker-compose up

Now, since the monolith "service" is "up" you can run it against a web site:

docker-compose run monolith ./target/release/monolith https://www.grepular.com

Finally, clean up:

docker-compose down

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment