Skip to content

Commit ae64fb0

Browse files
committed
Added Docker support.
Added a Dockerfile to allow building Shrek into an image. It uses Alpine Linux to keep the image size small. Shrek doesn't use any networking so there shouldn't be any CGO issues. I also created a Docker Hub repository for Shrek so users have the convenience of running Shrek inside Docker without having to build the image themselves. The README has been updated to include Docker instructions. refs #3
1 parent 0db27a0 commit ae64fb0

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

.dockerignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**/.dockerignore
2+
**/.env
3+
**/.git
4+
**/.gitattributes
5+
**/.gitignore
6+
**/.idea
7+
**/.vscode
8+
**/bin
9+
**/build
10+
**/deploy
11+
**/docker-compose*
12+
**/Dockerfile*
13+
**/releases
14+
assets
15+
examples

Dockerfile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM golang:1.17-alpine AS builder
2+
3+
WORKDIR /usr/src/shrek
4+
5+
# Pre-copy/cache go.mod for pre-downloading dependencies and only re-downloading
6+
# them in subsequent builds if they change.
7+
COPY go.mod go.sum ./
8+
RUN go mod download && go mod verify
9+
10+
# Copy all other project files.
11+
COPY . .
12+
13+
# Build app.
14+
RUN go build -v -o /usr/local/bin/shrek ./cmd/shrek
15+
16+
FROM alpine:latest AS final
17+
18+
WORKDIR /app
19+
20+
# Copy compiled binary into final image.
21+
COPY --from=builder /usr/local/bin/shrek .
22+
23+
# Define entry point.
24+
ENTRYPOINT ["/app/shrek", "-d", "/app/generated/"]

README.md

+69
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,58 @@ To see full usage, use the help flag `-h`:
5757
shrek -h
5858
```
5959

60+
# Running in Docker
61+
62+
[![Docker Hub][dkhub-badge]][dkhub-page]
63+
64+
You can run Shrek in Docker by pulling a pre-built image from [Docker Hub][dkhub-page]
65+
or by building an image locally.
66+
67+
## Option 1: Pull image from Docker Hub
68+
69+
There's no need to run `docker pull` directly, because `docker run` will automatically
70+
download the image from Docker Hub if it isn't found locally. So you can save a step
71+
and run Shrek with a one-liner:
72+
73+
```bash
74+
docker run --rm -it \
75+
-v "$PWD/generated":/app/generated \
76+
innix/shrek:latest \
77+
-n 3 food:ad barn:yd
78+
```
79+
80+
## Option 2: Build image locally
81+
82+
Build the Docker image locally using the [`Dockerfile`](Dockerfile):
83+
84+
```bash
85+
docker build -t shrek:latest .
86+
```
87+
88+
Then run a new container using the locally built image:
89+
90+
```bash
91+
docker run --rm -it \
92+
-v "$PWD/generated":/app/generated \
93+
shrek:latest \
94+
-n 3 food:ad barn:yd
95+
```
96+
97+
## How does the `docker run` command work with Shrek?
98+
99+
When running Shrek in a container, you pass arguments to it by placing them at the
100+
very end of the `docker run` command. In the example commands above, the arguments
101+
`-n 3 food:ad barn:yd` are passed to the Shrek binary.
102+
103+
The Docker image configures Shrek to save found `.onion` addresses to the directory
104+
`/app/generated/` in the container's file system. To access that directory from the
105+
host, you need to create a shared volume using Docker's `-v` argument. In the above
106+
examples, the `-v` argument will allow the host to access the container's directory
107+
by browsing to `$PWD/generated`.
108+
109+
If you're getting a permission error when trying to access the `generated` directory
110+
on the host, take a look at [this FAQ][docker-access-dir-faq].
111+
60112
# Using Shrek as a library
61113

62114
You can use Shrek as a library in your Go code. Add it to your `go.mod` file by running
@@ -181,6 +233,17 @@ func main() {
181233
```
182234
</details>
183235

236+
## Why can't I access the `generated` directory created by the Docker container?
237+
238+
If the directory (or any of the sub-directories) were created by the Shrek process
239+
running in the Docker container, they will be owned by the `root` user that is running
240+
the Shrek process. You need to change ownership of the directory back to your user,
241+
which can be done by running the [`chown`][chown.1-page] command:
242+
243+
```bash
244+
sudo chown -R $USER "$PWD/generated"
245+
```
246+
184247
## Why "Shrek"?
185248

186249
Onions have layers, ogres have layers.
@@ -209,5 +272,11 @@ Shrek is distributed under the terms of the MIT License (see [LICENSE](LICENSE))
209272
[licen-badge]: <https://img.shields.io/github/license/innix/shrek?style=for-the-badge>
210273
[licen-page]: <LICENSE> "Project License"
211274

275+
[dkhub-badge]: <https://img.shields.io/docker/v/innix/shrek?color=2496ed&label=Docker%20Hub&logo=docker&style=for-the-badge>
276+
[dkhub-page]: <https://hub.docker.com/r/innix/shrek> "innix/shrek - Docker Hub page"
277+
212278
[mkp224o-page]: <https://github.com/cathugger/mkp224o> "cathugger/mkp224o - GitHub page"
213279
[ghbine-page]: <https://github.com/cretz/bine/> "cretz/bine - GitHub page"
280+
[chown.1-page]: <https://linux.die.net/man/1/chown> "chown(1) - Linux man page"
281+
282+
[docker-access-dir-faq]: <#why-cant-i-access-the-generated-directory-created-by-the-docker-container> "Why can't I access the generated directory created by the Docker container?"

0 commit comments

Comments
 (0)