【问题标题】:screen command doesn't work in a containerscreen 命令在容器中不起作用
【发布时间】:2021-03-16 20:42:12
【问题描述】:

我最近在玩 Docker,我决定开始一个有趣的项目,所以我有了这个 Dockerfile:

# Base Image
FROM debian:buster

# Making steam directory
RUN mkdir /home/cs &&\
    mkdir /home/Resources &&\
    mkdir /home/Resources/steamcmd &&\
    mkdir /home/Resources/rehlds &&\
    mkdir /home/Resources/metamod &&\
    mkdir /home/Resources/amxmodx &&\
    mkdir /home/Resources/reunion &&\
    mkdir /home/Resources/rechecker &&\
    mkdir /home/Resources/reaimdetector &&\
    mkdir /home/Resources/cstrikefix &&\
    mkdir /home/Resources/regamedll &&\
    mkdir /home/Resources/reapi &&\
    mkdir /root/.steam &&\
    mkdir /root/.steam/sdk32 &&\
# Installing required packages
    dpkg --add-architecture i386 &&\
    apt-get update -y &&\
    apt-get install -y libsdl2-2.0-0 libsdl2-2.0-0:i386 lib32gcc1 curl tmux screen xz-utils unzip unrar-free nano &&\
# Installing the server
    cd /home/Resources/steamcmd &&\
    curl -sqL "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz" | tar zxvf - &&\
    until ./steamcmd.sh +login anonymous +force_install_dir /home/cs +app_update 90 -beta beta validate +quit; do sleep 1; done &&\
# Fix steamclient.so issue
    cp /home/cs/steamclient.so /root/.steam/sdk32/ &&\
# reHLDS patch
    cd /home/Resources/rehlds &&\
    curl -sqL "http://nexus.rehlds.org/nexus/content/repositories/rehlds-dev/rehlds/rehlds/3.7.0.693-dev/rehlds-3.7.0.693-dev.zip" >> rehlds-3.7.0.693-dev.zip &&\
    unzip rehlds-3.7.0.693-dev.zip &&\
    cp -r bin/linux32/* /home/cs/ &&\
# Metamod
    cd /home/Resources/metamod &&\
    curl -sqL "http://teamcity.rehlds.org/guestAuth/downloadArtifacts.html?buildTypeId=Metamod_Publish&buildId=lastSuccessful" >> Metamod_publish_1.3.0.128_artifacts.zip &&\
    unzip -q Metamod_publish_1.3.0.128_artifacts.zip &&\
    unzip -q metamod_1.3.0.128.zip &&\
    cp -r addons /home/cs/cstrike &&\
    sed -i 's+gamedll_linux "dlls/cs.so"+gamedll_linux "addons/metamod/metamod_i386.so"+g' /home/cs/cstrike/liblist.gam &&\
# AmxModX
    cd /home/Resources/amxmodx &&\
    curl -sqL "https://www.amxmodx.org/release/amxmodx-1.8.2-base-linux.tar.gz" | tar zxvf - &&\
    curl -sqL "https://www.amxmodx.org/release/amxmodx-1.8.2-cstrike-linux.tar.gz" | tar zxvf - &&\
    cp -r addons /home/cs/cstrike/ &&\
    echo "linux addons/amxmodx/dlls/amxmodx_mm_i386.so" >> /home/cs/cstrike/addons/metamod/plugins.ini &&\
# Reunion
    cd /home/Resources/reunion &&\
    curl -sqL "http://repository.btstelecom.ro/resources/reunion_0.1.92.zip" >> reunion_0.1.92.zip &&\
    unzip reunion_0.1.92.zip &&\
    mkdir /home/cs/cstrike/addons/reunion &&\
    mkdir /home/cs/cstrike/addons/reunion/dlls &&\
    cp bin/Linux/reunion_mm_i386.so /home/cs/cstrike/addons/reunion/dlls/ &&\
    cp reunion.cfg /home/cs/cstrike/ &&\
    echo "linux addons/reunion/dlls/reunion_mm_i386.so" >> /home/cs/cstrike/addons/metamod/plugins.ini &&\
# ReChecker
    cd /home/Resources/rechecker &&\
    curl -sqL "https://amxx-bg.info/download/file.php?id=1444&sid=1811ca41ee4e4a17ea7110eecd7954a6" >> rechecker_2_5.zip &&\
    unzip -q rechecker_2_5.zip &&\
    mkdir /home/cs/cstrike/addons/rechecker &&\
    mkdir /home/cs/cstrike/addons/rechecker/dlls &&\
    cp bin/addons/rechecker/rechecker_mm_i386.so /home/cs/cstrike/addons/rechecker/dlls/ &&\
    cp bin/addons/rechecker/resources.ini /home/cs/cstrike/addons/rechecker/dlls/ &&\
    echo "linux addons/rechecker/dlls/rechecker_mm_i386.so" >> /home/cs/cstrike/addons/metamod/plugins.ini &&\
# cstrike fix
    cd /home/Resources/cstrikefix &&\
    mkdir /home/cs/cstrike/addons/cstrike &&\
    mkdir /home/cs/cstrike/addons/cstrike/dlls &&\
    curl --header "Host: c-s.net.ua" --header "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36" --header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" --header "Accept-Language: ro-RO,ro;q=0.9,en;q=0.8" --header "Referer: https://c-s.net.ua/forum/topic81470.html" --header "Cookie: _ga=GA1.3.1603347949.1602960053; __gads=ID=f426b07af5c143a1-22526794ffb800af:T=1603190043:RT=1603190043:S=ALNI_MaFPky-pOiwPIbZanyNiU0Ksdqm0Q; session_id=9fb1924d44b7d5b77af02e9680400c4f; topicsread=a%3A4%3A%7Bi%3A17628%3Bi%3A1603190043%3Bi%3A84615%3Bi%3A1604348502%3Bi%3A81470%3Bi%3A1604348492%3Bi%3A74614%3Bi%3A1603821602%3B%7D; _gid=GA1.3.999712779.1604595471" --header "Connection: keep-alive" "https://c-s.net.ua/forum/ipb.html?act=attach&type=post&id=40051" -L -o 'cstrike_amxx_1_8_2_fix.zip' &&\
    unzip -q cstrike_amxx_1_8_2_fix.zip &&\
    cd cstrike_amxx_1_8_2_fix &&\
    cp cstrike_amxx_i386.so /home/cs/cstrike/addons/cstrike/dlls &&\
    echo "linux addons/cstrike/dlls/cstrike_amxx_i386.so" >> /home/cs/cstrike/addons/metamod/plugins.ini &&\
# ReGameDLL
    cd /home/Resources/regamedll &&\
    mkdir /home/cs/cstrike/addons/regamedll &&\
    curl --header "Host: c-s.net.ua" --header "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36" --header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" --header "Accept-Language: ro-RO,ro;q=0.9,en;q=0.8" --header "Referer: https://c-s.net.ua/forum/topic81470.html" --header "Cookie: _ga=GA1.3.1603347949.1602960053; __gads=ID=f426b07af5c143a1-22526794ffb800af:T=1603190043:RT=1603190043:S=ALNI_MaFPky-pOiwPIbZanyNiU0Ksdqm0Q; _gid=GA1.3.999712779.1604595471; session_id=1176d37d74d1a5cf21577ccba8e1f55d; topicsread=a%3A4%3A%7Bi%3A17628%3Bi%3A1603190043%3Bi%3A84615%3Bi%3A1604596752%3Bi%3A81470%3Bi%3A1604348492%3Bi%3A74614%3Bi%3A1603821602%3B%7D" --header "Connection: keep-alive" "https://c-s.net.ua/forum/ipb.html?act=attach&type=post&id=45156" -L -o 'ReGameDLL_CS_publish_5.3.0.217_artifacts.zip' &&\
    unzip -q ReGameDLL_CS_publish_5.3.0.217_artifacts.zip &&\
    unzip -q regamedll-dist-5.3.0.217-dev.zip &&\
    cp bin/bugfixed/cs.so /home/cs/cstrike/dlls &&\
    cp game.cfg /home/cs/cstrike &&\
# ReAimDetector
    cd /home/Resources/reaimdetector &&\
    curl --header "Host: c-s.net.ua" --header "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36" --header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" --header "Accept-Language: ro-RO,ro;q=0.9,en;q=0.8" --header "Referer: https://c-s.net.ua/forum/topic84615.html" --header "Cookie: _ga=GA1.3.1603347949.1602960053;__gads=ID=f426b07af5c143a1-22526794ffb800af:T=1603190043:RT=1603190043:S=ALNI_MaFPky-pOiwPIbZanyNiU0Ksdqm0Q; session_id=b83b284a0cf31d151d82a2dd7600f65d; topicsread=a%3A4%3A%7Bi%3A17628%3Bi%3A1603190043%3Bi%3A84615%3Bi%3A1603916003%3Bi%3A81470%3Bi%3A1603477078%3Bi%3A74614%3Bi%3A1603821602%3B%7D; _gid=GA1.3.252331836.1603916004; _gat=1" --header "Connection: keep-alive" "https://c-s.net.ua/forum/ipb.html?act=attach&type=post&id=47508" -L -o 'reaimdetector_0.2.2.rar' &&\
    unrar x reaimdetector_0.2.2.rar &&\
    cp -r linux/addons /home/cs/cstrike/ &&\
    echo "reaimdetector" >> /home/cs/cstrike/addons/amxmodx/configs/modules.ini &&\
    cd /home/cs/cstrike/addons/amxmodx/scripting &&\
    ./amxxpc reaimdetector.sma &&\
    cp reaimdetector.amxx /home/cs/cstrike/addons/amxmodx/plugins/ &&\
    echo "reaimdetector.amxx" >> /home/cs/cstrike/addons/amxmodx/configs/plugins.ini &&\
# Cleaning
    rm -rf /home/Resources &&\
# Run server
    cd /home/cs/ &&\
    screen -A -m -d -S cs /home/cs/hlds_run -game cstrike +maxplayers 25 +map de_dust2 -autoupdate -pingboost 3" >> /home/cs/start.sh 

当我启动容器时最后一个屏幕命令没有运行,但是当我进入容器并运行它运行的完全相同的命令时。 dockerfile 使用 reHLDS 构建 Counter-Strike 1.6 服务器。

编辑:在我运行容器时尝试了 ti7 方法后,它给了我这个错误:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"start.sh\": executable file not found in $PATH": unknown.

这是我尝试过的代码:

echo "#!/bin/sh" >> /home/cs/start.sh &&\
    echo "# start.sh" &&\
    echo "screen -A -m -d -S cs /home/cs/hlds_run -game cstrike +maxplayers 25 +map de_dust2 -autoupdate -pingboost 3" >> /home/cs/start.sh &&\
    chmod +x /home/cs/start.sh &&\
    /home/cs/start.sh
WORKDIR /home/cs/
ENTRYPOINT ["/home/cs/start.sh"]

这是我运行图像的命令:

sudo docker run -itd -p 27015:27015/udp --name=cs cstrike /bin/bash

【问题讨论】:

    标签: linux docker dockerfile


    【解决方案1】:

    屏幕是在容器完成之前创建的

    ENTRYPOINT或它调用的脚本中直接设置容器开始时应该以容器开头的命令

    #!/bin/bash
    # start.sh
    screen screen_command
    
    # exec the final command replacing this script with the new process
    # this makes the new process the forefront in Docker, keeping the container
    # running until it exits and allowing signals to be passed (such as ^C)
    exec sleep infinity
    
    ...
    RUN generate_script.sh
    WORKDIR /home/cs/
    ENTRYPOINT ["/home/cs/start.sh"]
    

    使用ENTRYPOINT,运行容器时无需指定任何shell
    名称后传递的参数将被发送到容器中,并且可以被脚本more进行解释

    docker run --detach --init --name foo-name container
    

    【讨论】:

    • CMD 可能更好。
    • 我的经历参差不齐,我记得ENTRYPOINT 更方便将参数传递给(尽管可能不是或可能不再是这种情况)
    • @Zeth 啊,你应该用完整或相对路径来调用它 - 我也更新了我的答案
    • 有效吗?图层应该被缓存
    • @ti7 现在容器在 1 秒后存在
    猜你喜欢
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多