Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: support dockerized builds of current source-code #2030

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
34 changes: 21 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
##### docker buildx create --use --name multi-builder --platform linux/arm64,linux/amd64
# https://github.com/docker/buildx/issues/318#issuecomment-1023226339
#FROM --platform=$BUILDPLATFORM rustlang/rust:nightly-buster-slim as builder
FROM rustlang/rust:nightly-buster-slim as builder
ARG BASE_IMAGE
FROM ${BASE_IMAGE} AS builder

# Installing git because of the workaround for the config
# https://github.com/rust-lang/cargo/issues/10781#issuecomment-1441071052
Expand All @@ -14,23 +15,30 @@ WORKDIR /usr/src/github.com/rust-lang
# Create blank project
RUN USER=root cargo new mdBook

# We want dependencies cached, so copy those first.
COPY Cargo.toml Cargo.lock /usr/src/github.com/rust-lang/mdBook
# examples is referenced in Cargo.toml
COPY examples /usr/src/github.com/rust-lang/mdBook/examples

WORKDIR /usr/src/github.com/rust-lang/mdBook

## Install target platform (Cross-Compilation) --> Needed for Alpine
#RUN rustup install nightly
# Use stable rather than nightly for the build target
# Got errors while building after 6 months
# https://substrate.stackexchange.com/questions/5379/how-do-i-fix-a-failed-build-error-e0635-unknown-feature-proc-macro-span-shri/9312#9312
RUN rustup default stable

# note: the `x86_64-unknown-linux-musl` target may not be installed
RUN rustup target add x86_64-unknown-linux-musl
marcellodesales marked this conversation as resolved.
Show resolved Hide resolved

# This is a dummy build to get the dependencies cached.
# We want dependencies cached, so copy those first.
COPY Cargo.toml Cargo.lock /usr/src/github.com/rust-lang/mdBook
marcellodesales marked this conversation as resolved.
Show resolved Hide resolved
# examples is referenced in Cargo.toml
COPY examples /usr/src/github.com/rust-lang/mdBook/examples

# This is a dummy build to pull dependencies and have them cached
# https://github.com/rust-lang/cargo/issues/8172#issuecomment-659056517
# Very slow builds: https://github.com/rust-lang/cargo/issues/9167#issuecomment-1219251978
# Logs verbose: https://github.com/rust-lang/cargo/issues/1106#issuecomment-141555744
RUN cargo build -vv --config "net.git-fetch-with-cli=true" --target x86_64-unknown-linux-musl --release

WORKDIR /usr/src/github.com/rust-lang/mdBook

# Now copy in the rest of the sources
COPY src /usr/src/github.com/rust-lang/mdBook/src

Expand All @@ -39,14 +47,14 @@ RUN cargo build --locked --bin mdbook --release --target x86_64-unknown-linux-mu

################
##### Runtime
FROM --platform=$BUILDPLATFORM alpine:3.16.0 AS runtime
FROM alpine:3.18.4 AS runtime

# Copy application binary from builder image
COPY --from=builder /usr/src/github.com/rust-lang/mdBook/target/x86_64-unknown-linux-musl/release/mdbook /usr/local/bin/mdbook

ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /etc/build.log

# Just to document which port a container from this image exposes
EXPOSE 3000

# The command to serve the books
ENTRYPOINT ["mdbook", "serve", "--hostname", "0.0.0.0"]

48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,57 @@ If you are interested in contributing to the development of mdBook, check out th
## Container

> **NOTE**: You need to have docker installed
> https://docs.docker.com/language/golang/run-containers/
> https://docs.docker.com/engine/install/

1. Locate a local directory with md files
2. Quickly run a docker container with the current version:

```console
docker run -ti -v $(pwd)/src:/src -p 3000:3000 rust-lang/mdbook
docker run -ti -v $(pwd):/mdbook -p 3000:3000 rust-lang/mdbook
```

* Using docker-compose.yaml, you can run to load the sources from the `test-book`.

```console
$ docker compose up --build
[+] Building 0.0s (0/0) docker-container:mac-linux-builder
[+] Running 1/1
βœ” Container rustlang-mdbook Recreated 0.1s
Attaching to rustlang-mdbook
rustlang-mdbook | 2023-11-23 16:52:59 [INFO] (mdbook::book): Book building has started
rustlang-mdbook | 2023-11-23 16:52:59 [INFO] (mdbook::book): Running the html backend
rustlang-mdbook | 2023-11-23 16:53:00 [INFO] (mdbook::cmd::serve): Serving on: http://0.0.0.0:3000
rustlang-mdbook | 2023-11-23 16:53:00 [INFO] (warp::server): Server::run; addr=0.0.0.0:3000
rustlang-mdbook | 2023-11-23 16:53:00 [INFO] (warp::server): listening on http://0.0.0.0:3000
rustlang-mdbook | 2023-11-23 16:53:00 [INFO] (mdbook::cmd::watch): Listening for changes...
```

* Test it in a container as well

> **NOTE**: docker compose creates a default network with the name of the `dir_default`

```console
docker run -ti --network mdbook_default alpine/lynx mdbook:3000

Introduction (p1 of 3)
1. Prefix Chapter
2.
3. 1. Introduction
4. 2. Draft Chapter
5.
6. Actual Markdown Tag Examples
7. 3. Markdown Individual tags
8.
1. 3.1. Heading
2. 3.2. Paragraphs
3. 3.3. Line Break
4. 3.4. Emphasis
5. 3.5. Blockquote
6. 3.6. List
7. 3.7. Code
8. 3.8. Image
9. 3.9. Links and Horizontal Rule
10. 3.10. Tables
```

## License
Expand Down
22 changes: 22 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3.9"

services:

####
#### 1. Place this file in a folder where the **/*.md files exist
#### 2. Adjust the port number to one available locally
#### 3. Adjust the volume path to point to the container's /src dir
####
mdbook:
image: rust-lang/mdbook
container_name: rustlang-mdbook
platform: linux/amd64
build:
context: .
args:
BASE_IMAGE: rustlang/rust:nightly-buster-slim
ports:
- 3000:3000
volumes:
- ./test_book/src:/src