iN2gN3jT

导航

Docker 的安装和使用 (Rocky Linux 9.4 虚拟机)

参见: Install Docker Engine on RHEL | Docker Docs

添加仓库

dnf -y install dnf-plugins-core
dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo

安装 Docker 引擎

  • 安装相关组件
dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • 启动服务
systemctl enable --now docker

配置 docker 服务的代理

mkdir /usr/lib/systemd/system/docker.service.d
PROXY_URL=http://192.168.250.1:10090
NO_PROXY_URL=localhost,127.0.0.1,::1,192.168.0.0/16,172.16.0.0/12,10.0.0.0/8,.svc,.cluster.local,.localdomain,.example.com
cat > /usr/lib/systemd/system/docker.service.d/http-proxy.conf << EOF
[Service]
Environment="NO_PROXY=$NO_PROXY_URL"
Environment="HTTPS_PROXY=$PROXY_URL"
Environment="HTTP_PROXY=$PROXY_URL"
Environment="ALL_PROXY=$PROXY_URL"
Environment="no_proxy=$NO_PROXY_URL"
Environment="https_proxy=$PROXY_URL"
Environment="http_proxy=$PROXY_URL"
Environment="all_proxy=$PROXY_URL"
EOF

systemctl daemon-reload
systemctl restart docker

验证安装效果

docker 的 hello world

docker run --rm hello-world

脚本语言/解释型语言的 hello world

  • bash, python, javascript, typescript, ruby, perl, php, hylang, powershell
docker run --rm bash bash -c 'echo "hello world"'
docker run --rm busybox echo hello world
docker run --rm python python -c 'print("hello world")'
docker run --rm node node -e 'console.log("hello world")'
docker run --rm node sh -c 'npx --yes tsx -e '\''console.log("hello world")'\'''
docker run --rm ruby ruby -e 'puts "hello world"'
docker run --rm perl perl -e 'print "hello world\n"'
docker run --rm php php -r 'echo "hello world\n";'
docker run --rm hylang hy -c '(print "hello world")'
docker run --rm mcr.microsoft.com/powershell pwsh -c 'Write-Host "hello world"'

编译型语言的 hello world

Go

cat > hello-world.go << EOF
package main
func main(){
    println("hello world")
}
EOF
docker run --rm -v "$PWD":/app -w /app golang \
    go run hello-world.go

Rust

cat > hello-world.rs << EOF
fn main(){
    println!("hello world")
}
EOF
docker run --rm -v "$PWD":/app -w /app rust 、
    sh -c 'rustc hello-world.rs -o /tmp/hello-world  && /tmp/hello-world'

Java

cat > HelloWorld.java << EOF
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}
EOF
docker run --rm -v "$PWD":/app -w /app eclipse-temurin \
    sh -c 'javac HelloWorld.java -d /tmp  && cd /tmp && java HelloWorld'

C

cat > hello-world.c << EOF
#include <stdio.h>
int main(){
    printf("hello world\n");
    return 0;
}
EOF
docker run --rm -v "$PWD":/app -w /app gcc \
    sh -c 'gcc hello-world.c -o /tmp/hello-world && /tmp/hello-world'

C++

cat > hello-world.cpp << EOF
#include <iostream>
int main(){
    std::cout<<"hello world"<<std::endl;
}
EOF
docker run --rm -v "$PWD":/app -w /app gcc \
    sh -c 'g++ hello-world.cpp -o /tmp/hello-world && /tmp/hello-world'

Haskell

cat > hello-world.hs << EOF
main = putStrLn "hello world"
EOF
docker run --rm -v "$PWD":/app -w /app haskell \
    sh -c 'runghc hello-world.hs'

posted on 2026-10-27 12:47  笔记和文档  阅读(15)  评论(0)    收藏  举报