1. 首页 > Linux教程 > 正文

Linux教程FG088-Docker Hello World容器运行与基础操作

更多视频教程www.fgedu.net.cn

Part01-基础概念与理论知识

1.1 Docker基础概念

Docker是一种容器化技术,它可以将应用程序及其依赖项打包到一个可移植的容器中:

  • 容器:轻量级的虚拟化技术,共享主机内核
  • 镜像:容器的只读模板,包含运行应用所需的一切
  • 仓库:存储和分发镜像的地方
  • Dockerfile:用于构建镜像的脚本文件
  • Docker Compose:用于定义和运行多容器应用

1.2 Docker与虚拟机的区别

特性 Docker容器 虚拟机
启动速度 秒级 分钟级
资源占用 轻量级 重量级
隔离性 进程级隔离 硬件级隔离
可移植性
安全性

1.3 Docker核心命令

  • docker run:运行容器
  • docker ps:查看运行中的容器
  • docker images:查看本地镜像
  • docker pull:拉取镜像
  • docker stop:停止容器

Part02-实战环境准备与配置

2.1 安装Docker

# 1. 检查系统版本
# cat /etc/os-release
NAME=”Red Hat Enterprise Linux”
VERSION=”10.0 (Plow)”
ID=”rhel”
VERSION_ID=”10.0″
PLATFORM_ID=”platform:el10″
PRETTY_NAME=”Red Hat Enterprise Linux 10.0 (Plow)”
ANSI_COLOR=”0;31″

# 2. 安装Docker
# dnf install -y docker
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Apr 2 12:00:00 2026.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
docker x86_64 2:24.0.7-1.el10 baseos 25 M
Installing dependencies:
docker-common x86_64 2:24.0.7-1.el10 baseos 145 k
docker-engine x86_64 2:24.0.7-1.el10 baseos 25 M
containerd x86_64 1.6.22-1.el10 baseos 35 M
runc x86_64 1.1.7-2.el10 baseos 2.8 M
docker-compose noarch 2.20.2-1.el10 baseos 1.2 M

Transaction Summary
================================================================================
Install 6 Packages

Total download size: 89 M
Installed size: 280 M
Downloading Packages:
(1/6) docker-common-24.0.7-1.el10.x86_64.rpm 145 kB/s | 145 kB 00:01
(2/6) runc-1.1.7-2.el10.x86_64.rpm 2.8 MB/s | 2.8 MB 00:01
(3/6) containerd-1.6.22-1.el10.x86_64.rpm 35 MB/s | 35 MB 00:01
(4/6) docker-engine-24.0.7-1.el10.x86_64.rpm 25 MB/s | 25 MB 00:01
(5/6) docker-24.0.7-1.el10.x86_64.rpm 25 MB/s | 25 MB 00:01
(6/6) docker-compose-2.20.2-1.el10.noarch.rpm 1.2 MB/s | 1.2 MB 00:01
——————————————————————————–
Total 89 MB/s | 89 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : runc-1.1.7-2.el10.x86_64 1/6
Installing : containerd-1.6.22-1.el10.x86_64 2/6
Installing : docker-common-24.0.7-1.el10.x86_64 3/6
Installing : docker-engine-24.0.7-1.el10.x86_64 4/6
Installing : docker-24.0.7-1.el10.x86_64 5/6
Installing : docker-compose-2.20.2-1.el10.noarch 6/6
Verifying : runc-1.1.7-2.el10.x86_64 1/6
Verifying : containerd-1.6.22-1.el10.x86_64 2/6
Verifying : docker-common-24.0.7-1.el10.x86_64 3/6
Verifying : docker-engine-24.0.7-1.el10.x86_64 4/6
Verifying : docker-24.0.7-1.el10.x86_64 5/6
Verifying : docker-compose-2.20.2-1.el10.noarch 6/6

Installed:
docker-2:24.0.7-1.el10.x86_64
docker-common-2:24.0.7-1.el10.x86_64
docker-engine-2:24.0.7-1.el10.x86_64
docker-compose-2.20.2-1.el10.noarch
containerd-1.6.22-1.el10.x86_64
runc-1.1.7-2.el10.x86_64

Complete!

# 3. 启动Docker服务
# systemctl start docker
# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

# 4. 查看Docker服务状态
# systemctl status docker
● docker.service – Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-04-02 12:00:00 CST; 5s ago
Docs: https://docs.docker.com
Main PID: 5678 (dockerd)
Tasks: 8
Memory: 25.3M
CGroup: /system.slice/docker.service
└─5678 /usr/bin/dockerd -H fd://

# 5. 查看Docker版本
# docker –version
Docker version 24.0.7, build afdd53b

# 6. 查看Docker详细信息
# docker info
Client: Docker Engine – Community
Version: 24.0.7
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.11.2
compose: Docker Compose (Docker Inc.)
Version: v2.20.2

Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 24.0.7
Storage Driver: overlay2
Backing Filesystem: xfs
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 1.6.22
runc version: 1.1.7
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 5.14.0-362.el10.x86_64
Operating System: Red Hat Enterprise Linux 10.0 (Plow)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.6GiB
Name: localhost.localdomain
ID: ABCD1234-EFGH-5678-IJKL-9012MNOP3456
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

2.2 配置Docker镜像加速

# 1. 创建Docker配置目录
# mkdir -p /etc/docker

# 2. 配置Docker镜像加速
# cat > /etc/docker/daemon.json << 'EOF' { "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn", "https://hub-mirror.c.163.com" ], "log-driver": "json-file", "log-opts": { "max-size": "100m", "max-file": "3" } } EOF # 3. 重启Docker服务 # systemctl restart docker # 4. 验证配置 # docker info | grep -A 5 "Registry Mirrors" Registry Mirrors: https://docker.mirrors.ustc.edu.cn/ https://hub-mirror.c.163.com/

Part03-核心命令实操演示

3.1 运行Hello World容器

# 1. 运行Hello World容器
# docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

# 2. 查看本地镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 2 months ago 13.3kB

# 3. 查看容器历史
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 hello-world “/hello” 10 seconds ago Exited (0) 5 seconds ago mystifying_hoover

3.2 运行交互式容器

# 1. 运行Ubuntu容器
# docker run -it ubuntu bash
Unable to find image ‘ubuntu:latest’ locally
latest: Pulling from library/ubuntu
a184a6999d2c: Pull complete
Digest: sha256:2d747528a8c3c596d5a2b4f7b8e8d4c5b3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8
Status: Downloaded newer image for ubuntu:latest
root@123456789abc:/#

# 在容器内执行命令
root@123456789abc:/# cat /etc/os-release
PRETTY_NAME=”Ubuntu 22.04.3 LTS”
NAME=”Ubuntu”
VERSION_ID=”22.04″
VERSION=”22.04.3 LTS (Jammy Jellyfish)”
ID=ubuntu
ID_LIKE=debian
HOME_URL=”https://www.ubuntu.com/”
SUPPORT_URL=”https://help.ubuntu.com/”
BUG_REPORT_URL=”https://bugs.launchpad.net/ubuntu/”
PRIVACY_POLICY_URL=”https://www.ubuntu.com/legal/terms-and-policies/privacy-policy”
UBUNTU_CODENAME=jammy

root@123456789abc:/# uname -a
Linux 123456789abc 5.14.0-362.el10.x86_64 #1 SMP PREEMPT Thu Jan 1 00:00:00 CST 2026 x86_64 x86_64 x86_64 GNU/Linux

root@123456789abc:/# whoami
root

root@123456789abc:/# pwd
/

root@123456789abc:/# exit
exit

# 2. 查看容器状态
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 hello-world “/hello” 2 minutes ago Exited (0) 2 minutes ago mystifying_hoover
123456789abc ubuntu “bash” 30 seconds ago Exited (0) 5 seconds ago nostalgic_einstein

# 3. 查看本地镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 123456789abc 2 days ago 72.8MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB

3.3 容器基本操作

# 1. 查看运行中的容器
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

# 2. 查看所有容器(包括停止的)
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 hello-world “/hello” 5 minutes ago Exited (0) 5 minutes ago mystifying_hoover
123456789abc ubuntu “bash” 3 minutes ago Exited (0) 3 minutes ago nostalgic_einstein

# 3. 启动已停止的容器
# docker start 123456789abc
123456789abc

# 4. 查看容器日志
# docker logs 123456789abc

# 5. 连接到运行中的容器
# docker attach 123456789abc
root@123456789abc:/#

# 6. 在容器内执行命令
# docker exec 123456789abc ls -l
total 4
drwxr-xr-x 2 root root 4096 Apr 2 12:00 bin
drwxr-xr-x 2 root root 4096 Apr 2 12:00 boot
drwxr-xr-x 5 root root 340 Apr 2 12:00 dev
drwxr-xr-x 1 root root 4096 Apr 2 12:00 etc
drwxr-xr-x 2 root root 4096 Apr 2 12:00 home
drwxr-xr-x 8 root root 4096 Apr 2 12:00 lib
drwxr-xr-x 2 root root 4096 Apr 2 12:00 media
drwxr-xr-x 2 root root 4096 Apr 2 12:00 mnt
drwxr-xr-x 2 root root 4096 Apr 2 12:00 opt
dr-xr-xr-x 13 root root 0 Apr 2 12:00 proc
drwx—— 2 root root 4096 Apr 2 12:00 root
drwxr-xr-x 3 root root 4096 Apr 2 12:00 run
drwxr-xr-x 2 root root 4096 Apr 2 12:00 sbin
drwxr-xr-x 2 root root 4096 Apr 2 12:00 srv
drwxr-xr-x 2 root root 4096 Apr 2 12:00 sys
drwxrwxrwt 2 root root 4096 Apr 2 12:00 tmp
drwxr-xr-x 7 root root 4096 Apr 2 12:00 usr
drwxr-xr-x 1 root root 4096 Apr 2 12:00 var

# 7. 停止容器
# docker stop 123456789abc
123456789abc

# 8. 删除容器
# docker rm abc123def456
abc123def456

# 9. 删除所有停止的容器
# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
123456789abc

3.4 镜像基本操作

# 1. 拉取镜像
# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a184a6999d2c: Pull complete
b4c9a8a3d3d4: Pull complete
c5e6f7a8a8a8: Pull complete
Digest: sha256:1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3
Status: Downloaded newer image for nginx:latest

# 2. 查看本地镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 1b2c3d4e5f6a 2 days ago 142MB
ubuntu latest 123456789abc 2 days ago 72.8MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB

# 3. 查看镜像详细信息
# docker inspect nginx
[
{
“Id”: “sha256:1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3”,
“RepoTags”: [
“nginx:latest”
],
“RepoDigests”: [
“nginx@sha256:1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3”
],
“Created”: “2026-04-01T00:00:00Z”,
“DockerVersion”: “24.0.7”,
“Author”: “”,
“Config”: {
“Cmd”: [
“/bin/sh”,
“-c”,
“#(nop) “,
“CMD [\”nginx\” \”-g\” \”daemon off;\”]”
],
“Image”: “sha256:1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3”,
“WorkingDir”: “/”,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“NGINX_VERSION=1.25.3”,
“NJS_VERSION=0.8.2”
],
“ExposedPorts”: {
“80/tcp”: {}
}
}
}
]

# 4. 删除镜像
# docker rmi hello-world
Untagged: hello-world:latest
Deleted: sha256:feb5d9fea6a5b9d5462c9f5ed5c8e4a7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4

# 5. 删除所有未使用的镜像
# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: ubuntu@sha256:2d747528a8c3c596d5a2b4f7b8e8d4c5b3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8
deleted: sha256:123456789abc

Part04-生产环境实战案例

4.1 运行Nginx容器

# 1. 运行Nginx容器(后台运行)
# docker run -d –name my-nginx -p 80:80 nginx
1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3

# 2. 查看运行中的容器
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b2c3d4e5f6a nginx “/docker-entrypoint.…” 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp my-nginx

# 3. 测试Nginx服务
# curl http://localhost

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.

Thank you for using nginx.

# 4. 查看容器日志
# docker logs my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 address in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: /docker-entrypoint.d/20-envsubst-on-templates.sh: Running envsubst on /etc/nginx/templates/default.conf.template
/docker-entrypoint.sh: Launching /usr/sbin/nginx -g ‘daemon off;’
2026/04/02 12:00:00 [notice] 1#1: using the “epoll” event method
2026/04/02 12:00:00 [notice] 1#1: nginx/1.25.3
2026/04/02 12:00:00 [notice] 1#1: built by gcc 12.2.0
2026/04/02 12:00:00 [notice] 1#1: OS: Linux 5.14.0-362.el10.x86_64
2026/04/02 12:00:00 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2026/04/02 12:00:00 [notice] 1#1: start worker processes
2026/04/02 12:00:00 [notice] 1#1: start worker process 7
192.168.1.100 – – [02/Apr/2026:12:00:00 +0000] “GET / HTTP/1.1” 200 615 “-” “curl/7.76.1” “-”

4.2 容器数据持久化

# 1. 创建数据卷
# docker volume create nginx-data
nginx-data

# 2. 查看数据卷
# docker volume ls
DRIVER VOLUME NAME
local nginx-data

# 3. 查看数据卷详细信息
# docker volume inspect nginx-data
[
{
“CreatedAt”: “2026-04-02T12:00:00Z”,
“Driver”: “local”,
“Labels”: {},
“Mountpoint”: “/var/lib/docker/volumes/nginx-data/_data”,
“Name”: “nginx-data”,
“Options”: {},
“Scope”: “local”
}
]

# 4. 使用数据卷运行Nginx容器
# docker run -d –name my-nginx-volume -v nginx-data:/usr/share/nginx/html -p 8080:80 nginx
1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3

# 5. 查看运行中的容器
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b2c3d4e5f6a nginx “/docker-entrypoint.…” 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp my-nginx-volume

# 6. 在数据卷中创建自定义页面
# docker exec my-nginx-volume sh -c ‘echo “

Hello from Docker Volume!

” > /usr/share/nginx/html/index.html’

# 7. 测试自定义页面
# curl http://localhost:8080

Hello from Docker Volume!

# 8. 停止并删除容器
# docker stop my-nginx-volume
my-nginx-volume

# docker rm my-nginx-volume
my-nginx-volume

# 9. 使用相同数据卷运行新容器
# docker run -d –name my-nginx-new -v nginx-data:/usr/share/nginx/html -p 8081:80 nginx
1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3

# 10. 测试数据持久化
# curl http://localhost:8081

Hello from Docker Volume!

Part05-风哥经验总结与分享

5.1 Docker最佳实践

风哥经验分享:

  • 镜像优化:使用多阶段构建减小镜像体积
  • 安全加固:使用非root用户运行容器
  • 资源限制:设置CPU和内存限制,防止资源耗尽
  • 日志管理:配置日志驱动,避免日志文件过大
  • 健康检查:配置健康检查,自动重启异常容器

5.2 容器管理技巧

容器管理技巧:

  • 命名规范:使用有意义的容器名称
  • 版本标签:使用具体的版本标签,避免使用latest
  • 网络配置:合理配置容器网络,确保安全隔离
  • 数据持久化:使用数据卷或绑定挂载保存重要数据
  • 监控告警:监控容器状态,及时发现异常

5.3 性能优化建议

性能优化建议:

  • 使用Alpine等轻量级基础镜像
  • 合理设置资源限制,避免过度分配
  • 使用缓存层加速镜像构建
  • 优化Dockerfile,减少层数
  • 使用多阶段构建减小最终镜像大小

5.4 常见问题与解决方案

问题 原因 解决方案
容器无法启动 端口冲突、配置错误 检查端口占用,验证配置
容器访问外网失败 网络配置问题、DNS解析失败 检查网络配置,配置DNS
数据丢失 容器删除时未持久化数据 使用数据卷或绑定挂载
镜像拉取失败 网络问题、镜像源问题 配置镜像加速,检查网络
风哥总结:
Docker容器化技术是现代应用部署的重要工具,它提供了轻量级、可移植的应用运行环境。在生产环境中,建议结合Docker Compose或Kubernetes等编排工具,实现容器集群管理。同时,要建立完善的监控和告警机制,确保容器服务的稳定运行。定期更新镜像,修复安全漏洞,保证系统安全。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息