Skip to content

Commit 92f12b0

Browse files
committed
refactor: use root user by default
it makes docker volumes accessible because non-root users can't create docker volumes. this is also less problematic on ci-cd systems like drone ci when it uses shared volumes on pipelines.
1 parent 064a8a0 commit 92f12b0

File tree

6 files changed

+32
-80
lines changed

6 files changed

+32
-80
lines changed

.drone.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ steps:
5656
auto_tag: true
5757
build_args:
5858
- DOCKER_IMAGE_VERSION=${DRONE_TAG}
59+
tags:
60+
- "latest"
5961

6062
trigger:
6163
target:

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ test:
1313
.PHONY: test
1414

1515
ci-test:
16-
@sudo chown -R rust:rust ./
1716
@echo "Testing application..."
1817
@rustc -vV
1918
@echo

README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ This is a __Linux Docker image__ based on [ekidd/rust-musl-builder](https://hub.
1010

1111
### Compiling an application inside a Docker container
1212

13-
__x86_64-unknown-linux-musl__
13+
By default the working directory is `/root/src`.
14+
15+
#### x86_64-unknown-linux-musl
1416

1517
```sh
1618
docker run --rm \
17-
--user rust:rust \
18-
--volume "${PWD}/sample":/home/rust/sample \
19-
--workdir /home/rust/sample \
19+
--volume "${PWD}/sample":/root/src \
20+
--workdir /root/src \
2021
joseluisq/rust-linux-darwin-builder:1.42.0 \
2122
sh -c "cargo build --release"
2223
```
2324

24-
__x86_64-apple-darwin__
25+
#### x86_64-apple-darwin
2526

2627
```sh
2728
docker run --rm \
28-
--user rust:rust \
29-
--volume "${PWD}/sample":/home/rust/sample \
30-
--workdir /home/rust/sample \
29+
--volume "${PWD}/sample":/root/src \
30+
--workdir /root/src \
3131
joseluisq/rust-linux-darwin-builder:1.42.0 \
3232
sh -c "cargo build --release --target x86_64-apple-darwin"
3333
```
@@ -40,24 +40,14 @@ You can also use the image as a base for your own Dockerfile:
4040
FROM joseluisq/rust-linux-darwin-builder:1.42.0
4141
```
4242

43-
### Custom directories
44-
45-
By default this image uses a `rust` user and the default working directory is `/home/rust`.
46-
If you want to use a different directory, change the corresponding owner and group as well.
47-
48-
```sh
49-
sudo chown -R rust:rust /custom/directory
50-
```
51-
52-
#### Cross-compilation example
43+
### Cross-compilation example
5344

5445
Below a simple example using a `Makefile` for cross-compiling a Rust app.
5546

5647
Notes:
5748

5849
- A [hello world](./tests/hello-world) app is used.
59-
- A custom directory is used below as working directory (instead of `/home/rust`).
60-
- If you want to use the default `/home/rust` as working directory, owner and group change is not necessary.
50+
- A custom directory is used below as working directory instead of `/root/src`.
6151

6252
Create a Makefile:
6353

@@ -71,7 +61,6 @@ compile:
7161
.PHONY: compile
7262

7363
cross-compile:
74-
@sudo chown -R rust:rust ./
7564
@echo
7665
@echo "1. Cross compiling example..."
7766
@rustc -vV

docker/Dockerfile

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ARG OPENSSL_VERSION=1.0.2
1717
# here is to support the musl-libc builds and Cargo builds needed for a
1818
# large selection of the most popular crates.
1919
RUN set -eux \
20-
&& DEBIAN_FRONTEND=noninteractive apt-get update -qq \
20+
&& DEBIAN_FRONTEND=noninteractive apt-get update -qq \
2121
&& DEBIAN_FRONTEND=noninteractive apt-get install -qq -y --no-install-recommends --no-install-suggests \
2222
build-essential \
2323
ca-certificates \
@@ -45,36 +45,19 @@ RUN set -eux \
4545
sudo \
4646
xutils-dev \
4747
zlib1g-dev \
48-
# We also set up a `rust` user by default, in whose account we'll install
49-
# the Rust toolchain. This user has sudo privileges if you need to install
50-
# any more software.
51-
&& useradd rust --user-group --create-home --shell /bin/bash --groups sudo \
52-
# `mdbook` is the standard Rust tool for making searchable HTML manuals.
53-
&& MDBOOK_VERSION=0.2.1 && \
54-
curl -LO https://github.com/rust-lang-nursery/mdBook/releases/download/v$MDBOOK_VERSION/mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
55-
tar xf mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
56-
mv mdbook /usr/local/bin/ && \
57-
rm -f mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz \
5848
# Clean up local repository of retrieved packages and remove the package lists
5949
&& apt-get clean \
6050
&& rm -rf /var/lib/apt/lists/*
6151

6252
# Static linking for C++ code
6353
RUN set -eux \
64-
&& sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
65-
66-
# Allow sudo without a password.
67-
ADD docker/sudoers /etc/sudoers.d/nopasswd
68-
69-
# Run all further code as user `rust`, and create our working directories
70-
# as the appropriate user.
71-
USER rust
72-
RUN set -eux \
73-
&& mkdir -p /home/rust/libs /home/rust/src
54+
&& ln -s "/usr/bin/g++" "/usr/bin/musl-g++" \
55+
# Create appropriate directories for current user
56+
&& mkdir -p /root/libs /root/src
7457

7558
# Set up our path with all our binary directories, including those for the
7659
# musl-gcc toolchain and for our Rust toolchain.
77-
ENV PATH=/home/rust/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
60+
ENV PATH=/root/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
7861

7962
# Install our Rust toolchain and the `musl` target. We patch the
8063
# command-line we pass to the installer so that it won't attempt to
@@ -86,7 +69,7 @@ RUN set -eux \
8669
&& rustup target add x86_64-unknown-linux-musl \
8770
&& rustup target add armv7-unknown-linux-musleabihf \
8871
&& rustup target add x86_64-apple-darwin
89-
ADD docker/cargo-config.toml /home/rust/.cargo/config
72+
ADD docker/cargo-config.toml /root/.cargo/config
9073

9174
# Set up a `git credentials` helper for using GH_USER and GH_TOKEN to access
9275
# private repositories if desired.
@@ -166,7 +149,7 @@ ENV OSXCROSS_SDK_VERSION 10.11
166149

167150
RUN set -eux \
168151
&& echo "Building osxcross..." \
169-
&& cd /home/rust \
152+
&& cd /usr/local/ \
170153
&& git clone --depth 1 https://github.com/tpoechtrager/osxcross \
171154
&& cd osxcross \
172155
&& curl -L -o ./tarballs/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz \
@@ -175,11 +158,10 @@ RUN set -eux \
175158
&& rm -rf *~ taballs *.tar.xz \
176159
&& rm -rf /tmp/*
177160

178-
ENV PATH $PATH:/home/rust/osxcross/target/bin
161+
ENV PATH $PATH:/usr/local/osxcross/target/bin
179162

180-
# Expect our source code to live in /home/rust/src. We'll run the build as
181-
# user `rust`, which will be uid 1000, gid 1000 outside the container.
182-
WORKDIR /home/rust/src
163+
# Expect our source code to live in /root/src
164+
WORKDIR /root/src
183165

184166
CMD ["bash"]
185167

docker/sudoers

Lines changed: 0 additions & 2 deletions
This file was deleted.

docker/tmpl.Dockerfile

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ARG OPENSSL_VERSION=1.0.2
1717
# here is to support the musl-libc builds and Cargo builds needed for a
1818
# large selection of the most popular crates.
1919
RUN set -eux \
20-
&& DEBIAN_FRONTEND=noninteractive apt-get update -qq \
20+
&& DEBIAN_FRONTEND=noninteractive apt-get update -qq \
2121
&& DEBIAN_FRONTEND=noninteractive apt-get install -qq -y --no-install-recommends --no-install-suggests \
2222
build-essential \
2323
ca-certificates \
@@ -45,36 +45,19 @@ RUN set -eux \
4545
sudo \
4646
xutils-dev \
4747
zlib1g-dev \
48-
# We also set up a `rust` user by default, in whose account we'll install
49-
# the Rust toolchain. This user has sudo privileges if you need to install
50-
# any more software.
51-
&& useradd rust --user-group --create-home --shell /bin/bash --groups sudo \
52-
# `mdbook` is the standard Rust tool for making searchable HTML manuals.
53-
&& MDBOOK_VERSION=0.2.1 && \
54-
curl -LO https://github.com/rust-lang-nursery/mdBook/releases/download/v$MDBOOK_VERSION/mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
55-
tar xf mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
56-
mv mdbook /usr/local/bin/ && \
57-
rm -f mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz \
5848
# Clean up local repository of retrieved packages and remove the package lists
5949
&& apt-get clean \
6050
&& rm -rf /var/lib/apt/lists/*
6151

6252
# Static linking for C++ code
6353
RUN set -eux \
64-
&& sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
65-
66-
# Allow sudo without a password.
67-
ADD docker/sudoers /etc/sudoers.d/nopasswd
68-
69-
# Run all further code as user `rust`, and create our working directories
70-
# as the appropriate user.
71-
USER rust
72-
RUN set -eux \
73-
&& mkdir -p /home/rust/libs /home/rust/src
54+
&& ln -s "/usr/bin/g++" "/usr/bin/musl-g++" \
55+
# Create appropriate directories for current user
56+
&& mkdir -p /root/libs /root/src
7457

7558
# Set up our path with all our binary directories, including those for the
7659
# musl-gcc toolchain and for our Rust toolchain.
77-
ENV PATH=/home/rust/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
60+
ENV PATH=/root/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
7861

7962
# Install our Rust toolchain and the `musl` target. We patch the
8063
# command-line we pass to the installer so that it won't attempt to
@@ -86,7 +69,7 @@ RUN set -eux \
8669
&& rustup target add x86_64-unknown-linux-musl \
8770
&& rustup target add armv7-unknown-linux-musleabihf \
8871
&& rustup target add x86_64-apple-darwin
89-
ADD docker/cargo-config.toml /home/rust/.cargo/config
72+
ADD docker/cargo-config.toml /root/.cargo/config
9073

9174
# Set up a `git credentials` helper for using GH_USER and GH_TOKEN to access
9275
# private repositories if desired.
@@ -166,7 +149,7 @@ ENV OSXCROSS_SDK_VERSION 10.11
166149

167150
RUN set -eux \
168151
&& echo "Building osxcross..." \
169-
&& cd /home/rust \
152+
&& cd /usr/local/ \
170153
&& git clone --depth 1 https://github.com/tpoechtrager/osxcross \
171154
&& cd osxcross \
172155
&& curl -L -o ./tarballs/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz \
@@ -175,11 +158,10 @@ RUN set -eux \
175158
&& rm -rf *~ taballs *.tar.xz \
176159
&& rm -rf /tmp/*
177160

178-
ENV PATH $PATH:/home/rust/osxcross/target/bin
161+
ENV PATH $PATH:/usr/local/osxcross/target/bin
179162

180-
# Expect our source code to live in /home/rust/src. We'll run the build as
181-
# user `rust`, which will be uid 1000, gid 1000 outside the container.
182-
WORKDIR /home/rust/src
163+
# Expect our source code to live in /root/src
164+
WORKDIR /root/src
183165

184166
CMD ["bash"]
185167

0 commit comments

Comments
 (0)