Docker容器的高级操作(常用操作)

本文详细介绍了如何使用Docker进行端口映射(-p)、数据卷挂载(-v)以及设置环境变量(-e)。通过实例展示如何运行Nginx容器并配置其对外服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

端口映射

docker run -p 容器外端口:容器内端口

[root@ip-10-0-3-12 ec2-user]# docker image ls
REPOSITORY       TAG       IMAGE ID       CREATED       SIZE
58440236/nginx   v1.12.2   4037a5562b03   3 years ago   108MB
[root@ip-10-0-3-12 ec2-user]# docker run --rm --name mynginx -d -p81:80 58440236/nginx:v1.12.2
8075e0efdb70ef0d80d5fb44238fae9220ea6c02911ea9a12dfaf7ca2ecd6e90
[root@ip-10-0-3-12 ec2-user]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                NAMES
8075e0efdb70   58440236/nginx:v1.12.2   "nginx -g 'daemon of…"   4 seconds ago   Up 4 seconds   0.0.0.0:81->80/tcp   mynginx
[root@ip-10-0-3-12 ec2-user]# netstat -luntp|grep 81
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      1587/docker-proxy   
[root@ip-10-0-3-12 ec2-user]# curl 127.0.0.1:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

挂载数据卷

docker run -v 容器外目录:容器内目录

传递环境变量

docker run -e 环境变量key=环境变量value

容器内安装软件(工具)

yum/apt-get/apt等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值