Skip to content

Commit 57c8f28

Browse files
committed
Revamp dockerfiles to use an entrypoint script to both run apps as a non-root user and chown the mounted host dir.
Reduce code duplication in dockerfiles. Minor improvements.
1 parent 4d6168e commit 57c8f28

24 files changed

+279
-203
lines changed

.dockerignore

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
1-
target
2-
.git
1+
# Ignore some files that are not needed inside docker images, so that editing them doesn't cause
2+
# docker to rebuild everything.
3+
.dockerignore
4+
.gitignore
5+
6+
######################################
7+
# Below goes the contents of gitignore
8+
9+
# Generated by Cargo
10+
# will have compiled files and executables
11+
**/target/
12+
# These are backup files generated by rustfmt
13+
**/*.rs.bk
14+
15+
.DS_Store
16+
17+
# Intellij IDEA
18+
.idea/
19+
*.iml
20+
/customSpec.json
21+
22+
# VSCode
23+
.vscode/
24+
25+
#exclude python env
26+
env/
27+
28+
# Test Python cache
29+
test/**/__pycache__
30+
31+
# Files generated for the testing system
32+
test/config.ini
33+
34+
# The cache for docker container dependency
35+
.cargo
36+
37+
# The cache for chain data in container
38+
.local
39+
40+
# direnv cache
41+
.direnv
42+
43+
# Python compiled files
44+
*.pyc
45+
46+
# wasm
47+
wasm-wrappers/pkg/
48+
49+
# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
50+
# inside it will be generated by the containers.
51+
build-tools/docker/example-mainnet/mintlayer-data/*
52+
# Same for example-mainnet-dns-server.
53+
build-tools/docker/example-mainnet-dns-server/mintlayer-data/*

.gitignore

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ test/config.ini
3838
# wasm
3939
wasm-wrappers/pkg/
4040

41-
# 'container_home' will be mapped to a home directory of a docker container, so everything
42-
# except for README.md will be generated by the container.
43-
build-tools/docker/example-mainnet/container_home/*
44-
!build-tools/docker/example-mainnet/container_home/README.md
45-
41+
# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
42+
# inside it will be generated by the containers.
43+
build-tools/docker/example-mainnet/mintlayer-data/*
4644
# Same for example-mainnet-dns-server.
47-
build-tools/docker/example-mainnet-dns-server/container_home/*
48-
!build-tools/docker/example-mainnet-dns-server/container_home/README.md
45+
build-tools/docker/example-mainnet-dns-server/mintlayer-data/*

api-server/scanner-daemon/src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ pub struct ApiServerScannerArgs {
2828

2929
/// Optional RPC address
3030
#[clap(long)]
31-
pub rpc_address: Option<NetworkAddressWithPort>,
31+
pub node_rpc_address: Option<NetworkAddressWithPort>,
3232

3333
/// Path to the RPC cookie file. If not set, the value is read from the default cookie file location.
3434
#[clap(long)]
35-
pub rpc_cookie_file: Option<String>,
35+
pub node_rpc_cookie_file: Option<String>,
3636

3737
/// RPC username (either provide a username and password, or use a cookie file. You cannot use both)
3838
#[clap(long)]
39-
pub rpc_username: Option<String>,
39+
pub node_rpc_username: Option<String>,
4040

4141
/// RPC password (either provide a username and password, or use a cookie file. You cannot use both)
4242
#[clap(long)]
43-
pub rpc_password: Option<String>,
43+
pub node_rpc_password: Option<String>,
4444

4545
/// Postgres config values
4646
#[clap(flatten)]

api-server/scanner-daemon/src/main.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use clap::Parser;
2727
use common::chain::{config::ChainType, ChainConfig};
2828
use config::ApiServerScannerArgs;
2929
use node_comm::{make_rpc_client, rpc_client::NodeRpcClient};
30-
use node_lib::default_rpc_config;
3130
use rpc::RpcAuthData;
3231
use utils::{cookie::COOKIE_FILENAME, default_data_dir::default_data_dir_for_chain};
3332
mod config;
@@ -146,17 +145,17 @@ async fn main() -> Result<(), ApiServerScannerError> {
146145

147146
let ApiServerScannerArgs {
148147
network,
149-
rpc_address,
150-
rpc_cookie_file,
151-
rpc_username,
152-
rpc_password,
148+
node_rpc_address,
149+
node_rpc_cookie_file,
150+
node_rpc_username,
151+
node_rpc_password,
153152
postgres_config,
154153
} = args;
155154

156155
let chain_type: ChainType = network.into();
157156
let chain_config = Arc::new(common::chain::config::Builder::new(chain_type).build());
158157

159-
let rpc_auth = match (rpc_cookie_file, rpc_username, rpc_password) {
158+
let node_rpc_auth = match (node_rpc_cookie_file, node_rpc_username, node_rpc_password) {
160159
(None, None, None) => {
161160
let cookie_file_path =
162161
default_data_dir_for_chain(chain_type.name()).join(COOKIE_FILENAME);
@@ -173,14 +172,22 @@ async fn main() -> Result<(), ApiServerScannerError> {
173172
}
174173
};
175174

176-
let default_rpc_bind_address =
177-
|| default_rpc_config(&chain_config).bind_address.expect("Can't fail").into();
175+
let default_node_rpc_bind_address = || {
176+
node_lib::default_rpc_config(&chain_config)
177+
.bind_address
178+
.expect("Can't fail")
179+
.into()
180+
};
178181

179-
let rpc_address = rpc_address.unwrap_or_else(default_rpc_bind_address);
182+
let node_rpc_address = node_rpc_address.unwrap_or_else(default_node_rpc_bind_address);
180183

181-
let rpc_client = make_rpc_client(chain_config.clone(), rpc_address.to_string(), rpc_auth)
182-
.await
183-
.map_err(ApiServerScannerError::RpcError)?;
184+
let rpc_client = make_rpc_client(
185+
chain_config.clone(),
186+
node_rpc_address.to_string(),
187+
node_rpc_auth,
188+
)
189+
.await
190+
.map_err(ApiServerScannerError::RpcError)?;
184191

185192
let storage = make_postgres_storage(
186193
postgres_config.postgres_host,

build-tools/codecheck/codecheck.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
r'|//'
3030
]
3131

32+
COMMON_EXCLUDE_DIRS = [
33+
'target',
34+
'.git',
35+
'build-tools/docker/example-mainnet/mintlayer-data',
36+
'build-tools/docker/example-mainnet-dns-server/mintlayer-data'
37+
]
38+
3239

3340
# List Rust source files
3441
def rs_sources(exclude = []):
@@ -47,7 +54,7 @@ def py_sources(exclude = []):
4754

4855
# Cargo.toml files
4956
def cargo_toml_files(exclude = []):
50-
exclude = [ os.path.normpath(dir) for dir in ['target', '.git', '.github'] + exclude ]
57+
exclude = [ os.path.normpath(dir) for dir in COMMON_EXCLUDE_DIRS + ['.github'] + exclude ]
5158
is_excluded = lambda top, d: os.path.normpath(os.path.join(top, d).lower()) in exclude
5259

5360
for top, dirs, files in os.walk('.', topdown=True):
@@ -57,7 +64,7 @@ def cargo_toml_files(exclude = []):
5764
yield os.path.join(top, file)
5865

5966
def _sources_with_extension(ext: str, exclude = []):
60-
exclude = [ os.path.normpath(dir) for dir in ['target', '.git', '.github'] + exclude ]
67+
exclude = [ os.path.normpath(dir) for dir in COMMON_EXCLUDE_DIRS + ['.github'] + exclude ]
6168
is_excluded = lambda top, d: os.path.normpath(os.path.join(top, d).lower()) in exclude
6269

6370
for top, dirs, files in os.walk('.', topdown=True):
@@ -73,7 +80,7 @@ def sources_with_extensions(exts: list[str], exclude = []):
7380

7481
# All files
7582
def all_files(exclude = []):
76-
exclude_full_paths = [ os.path.normpath(dir) for dir in ['target', '.git'] + exclude ]
83+
exclude_full_paths = [ os.path.normpath(dir) for dir in COMMON_EXCLUDE_DIRS + exclude ]
7784
exclude_dir_names = ['__pycache__']
7885

7986
def is_excluded(top, d):

build-tools/docker/Dockerfile.api-blockchain-scanner-daemon

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@
22
FROM mintlayer-builder:latest AS builder
33

44
# Runtime Stage
5-
FROM debian:bookworm-slim
6-
7-
# Create a new user and group
8-
RUN groupadd -g 1000 mintlayer && \
9-
useradd -u 1000 -g mintlayer -d /home/mintlayer -s /bin/bash -c "Mintlayer User" mintlayer
10-
11-
# Create and set the home directory for the new user
12-
RUN mkdir /home/mintlayer && \
13-
chown mintlayer:mintlayer /home/mintlayer
5+
FROM mintlayer-runner-base
146

157
COPY --from=builder /usr/src/target/release/api-blockchain-scanner-daemon /usr/bin
168

17-
# Define mintlayer directory as a volume
18-
VOLUME ["/home/mintlayer"]
19-
20-
# Switch to the non-root user
21-
USER mintlayer
22-
23-
# Set the working directory
24-
WORKDIR /home/mintlayer
25-
269
CMD ["api-blockchain-scanner-daemon"]

build-tools/docker/Dockerfile.api-web-server

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@
22
FROM mintlayer-builder:latest AS builder
33

44
# Runtime Stage
5-
FROM debian:bookworm-slim
6-
7-
# Create a new user and group
8-
RUN groupadd -g 1000 mintlayer && \
9-
useradd -u 1000 -g mintlayer -d /home/mintlayer -s /bin/bash -c "Mintlayer User" mintlayer
10-
11-
# Create and set the home directory for the new user
12-
RUN mkdir /home/mintlayer && \
13-
chown mintlayer:mintlayer /home/mintlayer
5+
FROM mintlayer-runner-base
146

157
COPY --from=builder /usr/src/target/release/api-web-server /usr/bin
168

17-
# Define mintlayer directory as a volume
18-
VOLUME ["/home/mintlayer"]
19-
20-
# Switch to the non-root user
21-
USER mintlayer
22-
23-
# Set the working directory
24-
WORKDIR /home/mintlayer
25-
269
CMD ["api-web-server"]

build-tools/docker/Dockerfile.builder

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
# Note: this "source" stage only exists so that we could copy the source tree into
2+
# the "builder" stage excluding the "build-tools" directory. This is to avoid rebuilding
3+
# all the images every time when a file in "build-tools" is modified. Note that for most
4+
# of the contents of "build-tools" this could be solved by adding them to .dockerignore,
5+
# but there are files (e.g. entrypoint.sh) that are needed inside images, so they can't
6+
# be ignored.
7+
#
8+
# TODO: dockerfile 1.7 syntax allows specifying --exclude for COPY, so the same can be done
9+
# without an additional stage. But at the moment of writing this it's still experimental.
10+
# Switch to using it when it becomes stable.
11+
FROM rust as source
12+
COPY . /src
13+
RUN rm -r /src/build-tools
14+
115
FROM rust AS builder
216

317
WORKDIR /usr/src/
418

519
# Install necessary build dependencies for the GUI (such as X11, etc.)
6-
RUN apt-get update && apt-get install -y ca-certificate && rm -rf /var/lib/apt/lists/*
20+
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
721

8-
COPY . .
22+
COPY --from=source /src/ /usr/src/
923

1024
ARG NUM_JOBS=1
1125
RUN cargo build --release -j${NUM_JOBS}

build-tools/docker/Dockerfile.dns-server

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@
22
FROM mintlayer-builder:latest AS builder
33

44
# Runtime Stage
5-
FROM debian:bookworm-slim
6-
7-
# Create a new user and group
8-
RUN groupadd -g 1000 mintlayer && \
9-
useradd -u 1000 -g mintlayer -d /home/mintlayer -s /bin/bash -c "Mintlayer User" mintlayer
10-
11-
# Create and set the home directory for the new user
12-
RUN mkdir /home/mintlayer && \
13-
chown mintlayer:mintlayer /home/mintlayer
5+
FROM mintlayer-runner-base
146

157
COPY --from=builder /usr/src/target/release/dns-server /usr/bin
168

17-
# Define mintlayer directory as a volume
18-
VOLUME ["/home/mintlayer"]
19-
20-
# Switch to the non-root user
21-
USER mintlayer
22-
23-
# Set the working directory
24-
WORKDIR /home/mintlayer
25-
269
CMD ["dns-server"]

build-tools/docker/Dockerfile.node-daemon

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@
22
FROM mintlayer-builder:latest AS builder
33

44
# Runtime Stage
5-
FROM debian:bookworm-slim
5+
FROM mintlayer-runner-base
66

7-
# Create a new user and group
8-
RUN groupadd -g 1000 mintlayer && \
9-
useradd -u 1000 -g mintlayer -d /home/mintlayer -s /bin/bash -c "Mintlayer User" mintlayer
10-
11-
# Create and set the home directory for the new user
12-
RUN mkdir /home/mintlayer && \
13-
chown mintlayer:mintlayer /home/mintlayer
14-
15-
COPY --from=builder /usr/src/target/release/node-daemon /usr/src/target/release/wallet-cli /usr/bin/
16-
17-
# Define mintlayer directory as a volume
18-
VOLUME ["/home/mintlayer"]
19-
20-
# Switch to the non-root user
21-
USER mintlayer
22-
23-
# Set the working directory
24-
WORKDIR /home/mintlayer
7+
COPY --from=builder /usr/src/target/release/node-daemon /usr/bin/
258

269
CMD ["node-daemon"]

0 commit comments

Comments
 (0)