Skip to content

Commit

Permalink
Rework docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Jul 24, 2024
1 parent ce24ab8 commit 0cfa68c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
24 changes: 21 additions & 3 deletions platform/linux/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ RUN apt-get update \
apt-transport-https \
curl \
gnupg \
sudo `# allows dev to install more packages without switching to root or rebuilding container` \
build-essential \
libsqlite3-dev \
libcurl4-openssl-dev \
Expand All @@ -29,7 +30,24 @@ RUN curl -fsSL https://github.com/bazelbuild/bazelisk/releases/download/v1.20.0/
&& chmod +x /usr/local/bin/bazel \
&& :

# When running docker with -u flag, the user should exist in the container to avoid various warnings
RUN groupadd -f user && useradd -m -g user user

WORKDIR /app

ARG USERNAME=user
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Create docker user as passed in by the build command
# This was modeled on https://code.visualstudio.com/remote/advancedcontainers/add-nonroot-user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME

COPY startup.sh /usr/local/bin/startup.sh
RUN chmod +x /usr/local/bin/startup.sh

# This allows users to `docker run` without specifying -u and -g
USER $USERNAME

ENTRYPOINT ["/usr/local/bin/startup.sh"]
CMD ["bash"]
1 change: 1 addition & 0 deletions platform/linux/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Exclude everything because we expect users to mount this dir as a volume,
# so none of this repos' content should be part of the docker build context.
*
!startup.sh
24 changes: 9 additions & 15 deletions platform/linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,18 @@ You can use a Docker container to build MapLibre Native. Note that you cannot bu

```bash
# Build docker image from the repo root
# Specifying USER_ID and GROUP_ID allows container to create files with the same owner as the host user,
# and avoids having to pass -u $(id -u):$(id -g) to docker run.
docker build \
-t maplibre-native-image \
-f platform/linux/Dockerfile .
--build-arg USER_ID=$(id -u) \
--build-arg GROUP_ID=$(id -g) \
-f platform/linux/Dockerfile \
platform/linux

# Run docker image as the current user.
# This ensures that the files created in the container are owned by the current user.
docker run --rm -it -v "$PWD:/app/" -v "$PWD/.docker_cache:/home/user/.cache" -u $(id -u):$(id -g) maplibre-native-image ___any_build_command___

# You can also execute build commands from inside the docker container by starting it without parameters:
docker run --rm -it -v "$PWD:/app/" -v "$PWD/.docker_cache:/home/user/.cache" -u $(id -u):$(id -g) maplibre-native-image
```

You can safely ignore this type of message and a missing username. It happens if your linux user was not the first one created on the system.

```
groups: cannot find name for group ID ....
I have no name!@...:/root$ `
# Run all build commands using the docker container.
# You can also execute build commands from inside the docker container by starting it without the build command.
docker run --rm -it -v "$PWD:/app/" -v "$PWD/.docker_cache:/home/user/.cache" maplibre-native-image ___any_build_command___
```

## Build
Expand All @@ -59,7 +54,6 @@ cmake -B build -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DC
cmake --build build --target mbgl-render -j $(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null)
```


## Running `mbgl-render`
Running `mbgl-render --style https://raw.githubusercontent.com/maplibre/demotiles/gh-pages/style.json` should produce a map tile image with the default MapLibre styling from [the MapLibre demo](https://maplibre.org/).

Expand Down
5 changes: 5 additions & 0 deletions platform/linux/startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

# This file will have any docker pre-installations

exec "$@"

0 comments on commit 0cfa68c

Please sign in to comment.