00000212
在 Docker 中,容器的运行状态(State)可以分为多种,每种状态反映了容器在其生命周期中的不同阶段。
你可以通过 docker ps(运行中容器)和 docker ps -a(所有容器)查看状态,也可以通过 docker inspect <容器名或ID>查看更详细的 State字段。
| 状态 | 含义 | 说明 |
|---|---|---|
created |
已创建 |
容器已创建但未启动,文件系统已准备好,但进程未运行。 |
running |
运行中 |
容器正在运行,主进程处于活动状态。 |
paused |
已暂停 |
容器内的所有进程被暂停(SIGSTOP),内存状态保留,可恢复。 |
exited |
已退出 |
容器主进程已终止(正常退出或异常退出),退出码非 0 表示异常。 |
dead |
死亡 |
容器无法被正常停止或移除,通常是 Docker 引擎内部错误状态。 |
restarting |
重启中 |
容器正在重启(例如设置了 |
removing |
移除中 |
容器正在被 |
docker commit
docker commit命令用于将容器的当前状态提交为一个新的镜像。
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
参数列表
| 参数 | 简写 | 类型 | 默认值 | 描述 |
|---|---|---|---|---|
—author |
-a |
string |
作者信息 |
|
—change |
-c |
list |
应用 Dockerfile 指令 |
|
—message |
-m |
string |
提交信息 |
|
—pause |
-p |
bool |
true |
提交时暂停容器 |
docker run -it --name centos-nginx-build centos:7.9.2009 /bin/bash
做你想要的功能都可以,也可以安装别的应用
# 在容器内执行
# 1. 优化yum源并更新系统
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum update -y
# 2. 安装 Nginx 和相关工具
yum install -y nginx curl wget vim net-tools
# 在容器内执行优化清理
yum clean all
rm -rf /var/cache/yum/*
rm -rf /tmp/* /var/tmp/*
rm -rf /var/log/yum.log
# 3. 创建必要的目录
mkdir -p /var/www/html
mkdir -p /app/logs
mkdir -p /app/conf
# 4. 创建简单的测试页面
cat > /var/www/html/index.html << 'EOF'
dsf centos7-nginx-test
EOF
# 5. 创建自定义 Nginx 配置 注意:使用默认配置也可以
cat > /etc/nginx/conf.d/default.conf << 'EOF'
server {
listen 80;
server_name loaclhost;
# 根目录
root /var/www/html;
index index.html index.htm;
# 访问日志
access_log /app/logs/access.log;
error_log /app/logs/error.log;
location / {
try_files $uri $uri/ =404;
}
}
EOF
# 6. 设置权限
chown -R nginx:nginx /var/www/html
chown -R nginx:nginx /app/logs
chmod -R 755 /var/www/html
# 7. 测试 Nginx 配置
nginx -t
# 8. 启动 Nginx
nginx
# 9. 验证 Nginx 是否运行
ps aux | grep nginx
netstat -tlnp | grep 80
# 10. 测试本地访问
curl 127.0.0.1
在容器内按 Ctrl+P然后 Ctrl+Q退出容器(或者输入exit 回车退出容器,保持容器运行即可),或者在另一个终端操作。
# 1. 提交容器为镜像 docker commit \ --author "Your Name <your.email@example.com>" \ --message "Initial CentOS with Nginx installation" \ --change='CMD ["nginx", "-g", "daemon off;"]'\ centos-nginx-build \ centos-nginx # --change='CMD ["nginx", "-g", "daemon off;"]' 表示强制nginx在前台运行 # 2. 查看新镜像 docker images | grep centos-nginx # 3. 为镜像添加标签 docker tag centos-nginx centos-nginx:v1.0
docker run -d --name nginx-01 -p 80:80 centos-nginx:v1.0 # 本地测试访问 curl 127.0.0.1
docker run -it --name ubuntu-nginx-build ubuntu:22.04 /bin/bash
做你想要的功能都可以,也可以安装别的应用
# 在容器内执行 # 1. 更新系统 apt update -y # 2. 安装 Nginx和curl apt install -y nginx curl # 3. 创建简单的测试页面 cat > /var/www/html/index.html << 'EOF' dsf ubuntu-nginx-test EOF # 4. 使用默认的nginx配置 # 5. 测试 Nginx 配置 nginx -t # 6. 启动 Nginx nginx # 7. 验证 Nginx 是否运行 ps aux | grep nginx # 8. 测试本地访问 curl 127.0.0.1
在容器内按 Ctrl+P然后 Ctrl+Q退出容器(或者输入exit 回车退出容器,保持容器运行即可),或者在另一个终端操作。
# 1. 提交容器为镜像 docker commit \ --author "Your Name <your.email@example.com>" \ --message "Initial CentOS with Nginx installation" \ --change='CMD ["nginx", "-g", "daemon off;"]'\ ubuntu-nginx-build \ ubuntu-nginx # --change='CMD ["nginx", "-g", "daemon off;"]' 表示强制nginx在前台运行 # 2. 查看新镜像 docker images | grep ubuntu-nginx # 3. 为镜像添加标签 docker tag ubuntu-nginx ubuntu-nginx:v1.0
docker run -d --name ubuntu-nginx-01 -p 81:80 ubuntu-nginx:v1.0 # 本地测试访问 curl 127.0.0.1:81
Alpine 以其小巧的体积著称,推荐使用。
docker run -it --name alpine-nginx-build alpine:3.20.2
做你想要的功能都可以,也可以安装别的应用
# 在容器内执行
# 1. 更换为阿里源,并更新系统
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
apk update
# 2. 安装 Nginx和curl
apk add nginx curl
# 3. 删除缓存
rm -rf /var/cache/*
# 4. 使用默认的nginx配置即可
mkdir -p /var/www/html
cat > /etc/nginx/http.d/default.conf << 'EOF'
server {
listen 80;
server_name loaclhost;
# 根目录
root /var/www/html;
index index.html index.htm;
# 访问日志
access_log /var/log/access.log;
error_log /var/log/error.log;
location / {
try_files $uri $uri/ =404;
}
}
EOF
# 5. 创建简单的测试页面
cat > /var/www/html/index.html << 'EOF'
dsf alpine-nginx-test
EOF
# 6. 测试 Nginx 配置
nginx -t
# 7. 启动 Nginx
nginx
# 8. 验证 Nginx 是否运行
ps aux | grep nginx
# 9. 测试本地访问
curl 127.0.0.1
在容器内按 Ctrl+P然后 Ctrl+Q退出容器(或者输入exit 回车退出容器,保持容器运行即可),或者在另一个终端操作。
# 1. 提交容器为镜像 docker commit \ --author "Your Name <your.email@example.com>" \ --message "Initial CentOS with Nginx installation" \ --change='CMD ["nginx", "-g", "daemon off;"]'\ alpine-nginx-build \ alpine-nginx # 2. 查看新镜像 docker images | grep alpine-nginx # 3. 为镜像添加标签 docker tag alpine-nginx alpine-nginx:v1.0
docker run -d --name alpine-nginx-01 -p 82:80 alpine-nginx:v1.0 # 本地测试访问 curl 127.0.0.1:82
| 备注 | 修改日期 | 修改人 |
| 内容更新 | 2025-12-22 00:07:44[当前版本] | 文艺范儿 |
| 内容更新 | 2025-12-22 00:06:32 | 文艺范儿 |
| 内容更新 | 2025-12-20 00:59:53 | 文艺范儿 |
| 创建版本 | 2025-12-20 00:58:55 | 文艺范儿 |