docker启动一个helloworld(公司内网服务器)

这里写目录标题

我来介绍几种启动 Docker Hello World 的方法:

  1. 最简单的方式
bash 复制代码
docker run hello-world

这会自动下载并运行官方的 hello-world 镜像。

  1. 使用 Nginx 作为 Hello World
bash 复制代码
docker run -d -p 80:80 nginx

-d: (detach) 在后台运行容器,不会占用当前终端

-p 80:80: 端口映射

第一个 80 是主机端口(外部访问端口)

第二个 80 是容器内部端口

格式为 主机端口:容器端口

nginx: 使用的镜像名称

然后访问 http://localhost 就能看到 Nginx 欢迎页面。

可以通过curl命令看一下Localhost

  1. 自己创建一个 Hello World 容器

首先创建一个 Dockerfile

dockerfile 复制代码
FROM ubuntu
CMD echo "Hello World from Docker!"

然后构建和运行:

bash 复制代码
# 构建镜像
docker build -t my-hello-world .

# 运行容器
docker run my-hello-world

直接下了一个20G的ubuntu镜像在本地

  1. 使用 Python 创建 Hello World

创建 Dockerfile

dockerfile 复制代码
FROM python:3.8-slim
WORKDIR /app
COPY . .
CMD ["python", "-c", "print('Hello World from Python!')"]

构建和运行:

bash 复制代码
docker build -t python-hello .
docker run python-hello

常用的 Docker 命令:

  • docker ps - 查看运行中的容器
  • docker images - 查看本地镜像
  • docker stop <容器ID> - 停止容器
  • docker rm <容器ID> - 删除容器
  • docker rmi <镜像ID> - 删除镜像

这些是最基础的 Docker Hello World 示例,你可以根据需要选择合适的方式。

容易遇到的问题:

1、docker连接问题

docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).

See 'docker run --help'.

需要改用国内镜像源

编辑或创建 /etc/docker/daemon.json 文件(Windows 通常在 %programdata%\docker\config\daemon.json),添加以下内容:

bash 复制代码
{
    "registry-mirrors": [
        "https://mirror.ccs.tencentyun.com",
        "https://registry.docker-cn.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://hub-mirror.c.163.com"
    ]
}

注意如果原先json中有文件,键之间需要加逗号

bash 复制代码
{
    "registry-mirrors": [
        "https://mirror.ccs.tencentyun.com",
        "https://registry.docker-cn.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://hub-mirror.c.163.com"
    ],   // 这里需要加逗号
    "dns": ["192.10.0.2", "8.8.8.8"]
}

然后重启服务

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl restart docker
相关推荐
海阔天空任鸟飞~5 小时前
Linux 权限 777
linux·运维·服务器
阿标在干嘛5 小时前
从物理机到K8s:政策快报平台的容器化部署实践
云原生·容器·kubernetes
IT瑞先生7 小时前
docker-compose下快速部署实操——持续更新...
运维·docker·容器
张青贤7 小时前
Centos7离线部署K8s集群V1.28.8
云原生·容器·kubernetes·containerd·kubekey·离线部署
Kina_C8 小时前
Linux iptables 防火墙原理与实操——从四表五链到 NAT 配置
linux·运维·服务器·iptables
灵机一物12 小时前
企业选型参考:2026 CXL 内存扩展方案六强测评与落地指南
服务器·网络·数据库·人工智能
Web极客码12 小时前
如何用三段式确定性剪枝,为 LLM Agent 砍掉 35% 的 Token 成本?
服务器·人工智能·算法·机器学习
yeflx12 小时前
速腾Airy雷达使用记录
java·服务器·网络
爱码少年13 小时前
Docker遍历镜像生成导出镜像命令六法
docker·shell
公众号:fuwuqiBMC13 小时前
(转自“服务器BMC”)服务器BMC芯片——多Die(芯片)封装
运维·服务器·人工智能