Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

fix: Fixes substrate-node-template docker issues #13039

Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/node-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ First, install [Docker](https://docs.docker.com/get-docker/) and [Docker Compose

Then run the following command to start a single node development chain.

> If you get an error that tcp port address is already in use then find an available port to use for the host port in docker-compose.yml (i.e. "<HOST_PORT>:9944").

```sh
./scripts/docker_run.sh
```
Expand Down
15 changes: 9 additions & 6 deletions bin/node-template/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
version: "3.2"

services:
# first line of this file must be reserved for use by ./scripts/docker_run.sh
dev:
container_name: node-template
image: paritytech/ci-linux:production
working_dir: /var/www/node-template
working_dir: /var/www/node-template/node
ports:
- "9944:9944"
environment:
- CARGO_HOME=/var/www/node-template/.cargo
volumes:
- .:/var/www/node-template
- type: bind
source: ./.local
target: /root/.local
- ./.cargo/registry:/root/.cargo/registry
- ./.local:/root/.local
- ../../.git:/var/www/node-template/.git
- ../../frame:/var/frame
- ../../utils:/var/utils
- ../../client:/var/client
- ../../primitives:/var/primitives
command: bash -c "cargo build --release && ./target/release/node-template --dev --ws-external"
71 changes: 67 additions & 4 deletions bin/node-template/scripts/docker_run.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,72 @@
# This script is meant to be run on Unix/Linux based systems
set -e

echo "*** Start Substrate node template ***"

printf "*** Start Substrate node template ***\n"
SCRIPT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
PARENT_DIR=$(dirname "$SCRIPT_DIR")
mkdir -p "$PARENT_DIR/.local"
mkdir -p "$PARENT_DIR/.cargo/registry"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe put them into the target/ dir, so I wont forget deleting them eventually?
Also do you know if there is a way to not make docker chown these folders with root?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cheers, i'm working on fixing all that, i'll push some commits and tell you when its done

Copy link
Contributor Author

@ltfschoen ltfschoen Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've pushed changes that address your comments.

in the changes when i added a workspace bin/node-template/Cargo.toml file instead of only building the bin/node-template/node directory that fixed the location of the /target directory, and adding to docker-compose.yml CARGO_HOME=/var/www/node-template/target/.cargo allows .cargo files to be kept in the /target dir.
in the entrypoint.sh script i use usermod to load the existing non-root user called nonroot that has already been added to the prebuilt docker image 'paritytech/ci-linux:production', then i use chown -R nonroot:nonroot ... to change ownership of the relevant folders like the chain directory and other hidden folders that get used like:
/var/www/node-template/target/debug/.cargo-lock
/var/www/node-template/target/.cargo/registry/index/github.mirror.nvdadr.com-x/.git/config.lock
/var/www/node-template/target/debug/build/anyhow-x/invoked.timestamp

i also create a symlink from the chain data folder to /data, so we can have a single static dev chain folder, which you can specify with --base-path=/data, which makes it easier to access to prune since otherwise the directory might change each time and clutters up storage.

i also run arguments that the user passes into the script with su -c "$args" nonroot, where args=$@.

cd $(dirname ${BASH_SOURCE[0]})/..

docker-compose down --remove-orphans
docker-compose run --rm --service-ports dev $@
modify_first_line_of_docker_compose() {
local first_line=$1
sed -i "1s/.*/$first_line/" $PARENT_DIR/docker-compose.yml
}

printf "Searching for docker-compose executable...\n"

if ! [ -x "$(command -v docker-compose)" ]; then
printf "Skipping docker-compose since executable is not installed.\n"
else
printf "Detected docker-compose executable.\n"
exit_code=$(docker-compose version; status=$?; echo $status)
# get the last character captured by $? since it may also include command output first
last_char=${exit_code: -1}

# prefix last char with random character incase last char is 0 but not an exit code
if [[ "x${last_char}" == "x0" && "$(docker-compose version)" =~ " 1." ]]; then
printf "Detected legacy docker-compose version 1.x. Using Compose File Format 1.\n"
# temporarily comment out first line `services:` of ../docker-compose.yml
modify_first_line_of_docker_compose "#services:"
docker-compose down --remove-orphans
docker-compose run --rm --service-ports dev $@
# uncomment again the first line `services:` of ../docker-compose.yml
modify_first_line_of_docker_compose "services:"
exit
fi

exit_code=$(docker-compose compose version; status=$?; echo $status)
last_char=${exit_code: -1}

if [[ "x${last_char}" == "x0" && "$(docker-compose compose version)" =~ " v2." ]]; then
printf "Detected legacy docker-compose version 2.x. Using Compose File Format 2+.\n"
# switch back to default `services:` incase was temporarily commented out
modify_first_line_of_docker_compose "services:"
docker-compose compose down --remove-orphans
docker-compose compose run --rm --service-ports dev $@
exit
fi

printf "Unknown or unsupported version of docker-compose. Skipping...\n"
fi

if ! [ -x "$(command -v docker)" ]; then
printf "Skipping docker since docker executable is not installed.\n"
else
printf "Detected docker executable.\n"
exit_code=$(docker compose version; status=$?; echo $status)
last_char=${exit_code: -1}

if [[ "x${last_char}" == "x0" ]]; then
printf "Detected docker compose subcommand.\n"
# switch back to default `services:` incase was temporarily commented out
modify_first_line_of_docker_compose "services:"
docker compose down --remove-orphans
docker compose run --rm --service-ports dev $@
else
printf "Skipping docker since docker executable subcommand not supported.\n"
fi
exit
fi

printf "Error: Unable to detect any docker compose installation.\n"
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<C: SubstrateCli> Runner<C> {
pub fn print_node_infos<C: SubstrateCli>(config: &Configuration) {
info!("{}", C::impl_name());
info!("✌️ version {}", C::impl_version());
info!("❤️ by {}, {}-{}", C::author(), C::copyright_start_year(), Local::today().year());
info!("❤️ by {}, {}-{}", C::author(), C::copyright_start_year(), Local::now().year());
info!("📋 Chain specification: {}", config.chain_spec.name());
info!("🏷 Node name: {}", config.network.node_name);
info!("👤 Role: {}", config.display_role());
Expand Down