Use docker and cargo-deb to package .deb packages for different linux versions

problem reproduction

My Linux system version is ubuntu 20.04. After writing a rust project, use cargo build --release to package and generate a deb package (if cargo-deb is not installed, the build will produce a binary file), which can be decompressed and installed normally on this machine, but I sent the deb package to a friend, but he couldn’t decompress it. I found out that his linux version is ubuntu 16.04, and I couldn’t decompress the deb file packaged under 20.04.

Solution 1

Use docker to pull a 16.04 version of the image, and pay attention to using the parameter to mount the corresponding folder to the docker container when docker is running -v, and then docker exec enters the container to complete the packaging, so that the packaged deb can run normally in the 16.04 system version

Solution 2

Solution 1 directly using root privileges actually has security risks in multi-person collaborative development. The best way is to add a user with the same name as the host in the docker container. The shell script for adding a user is roughly as follows

# useradd.sh
#! /bin/bash
USER_NAME=$1
USER_ID=$2
GRP_NAME=$3
GRP_ID=$4

# check if the group name exits, if not , add it
getent group | grep "${USER_NAME}:x" > /dev/null 2>&1 || addgroup  --quiet --gid "$GRP_ID" "$GRP_NAME"
# add user to the group
adduser  --quiet --disabled-password --force-badname --gecos '' "$USER_NAME" \
    --uid "$USER_ID" --gid "$GRP_ID" 2>/dev/null
# give user the sudo privilege
usermod -aG sudo "$USER_NAME"
sudo echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
# copy user setup
cat /etc/skel/.bashrc >> "/home/${USER_NAME}/.bashrc"


# Set user files ownership to current user, such as .bashrc, .profile, etc.
sudo chown -R "${USER_NAME}:${GRP_NAME}" "/home/${USER_NAME}"
source "/home/${USER_NAME}/.bashrc"

The useradd.sh here can be copied to the image during docker build, or can be used in the host docker cp [from_path] [to_path], and finally specify the command and parameter list when docker run or docker exec, such asdocker exec -it -u $username bash -c useradd.sh $username $userid $groupname $groupid

Follow up 报错“Rustlang “no override and no default toolchain set””

Cause of error: After adding a new user, there is no rust-related user configuration in the docker container.
Solution 1: Use the new user to reinstall the rust toolchain. You can find the installation method on the rust official website . Note that reinstalling rust is time-consuming. If the proxy is not set properly, the installation may not be successful for a long time.

Solution 2: Use the rust configuration of other users. After rust is installed, the corresponding configurations are placed in the ~/.cargofolder. That is to say, as long as another user has installed rust and its tool chain, we can directly copy a copy to the current user directory. After copying, activate the environment cp -r /home/${other_user}/.cargo /home/${curr_user}/and source ~/.cargo/enventer cargo -V on the command line to view the version. If you still report override and not default toolchain set , consider copying the ~/.rustup folder. settings.tomlFiles are stored in .rustup, and you can view configurations related to rust.

Solution 3: Directly mount the host-related folders to the corresponding container container, use -vparameters, in this example need to mount ~/.cargoand ~/.rustuptwo folders, the corresponding docker command is as follows

docker run -itd \ # 运行docker
		--name test_rust \ # 设置container名称
		-v ${
    
    HOME}/.cargo:/home/${USER_NAME}/.cargo \ # 将host的.cargo文件夹挂载到container中$USER_NAME对应配置文件夹下
		-v ${
    
    HOME}/.rustup:/home/${USER_NAME}/.cargo \ # 将host的.rustup文件夹挂载到container中$USER_NAME对应配置文件夹下
		${DOCKER_IMAGE} \ # 指定${DOCKER_IMAGE}镜像来创建container
		/bin/bash > /dev/null # 运行container的指令

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/125198895