在docker中运行nginx

1.导入nginx镜像
docker image load -i nginx.tar
docker image inspect nginx #查看镜像信息

2.运行镜像

#使用nginx镜像创建容器,容器名称为nginxtest
docker container run -d --name nginxtest nginx
   -d 表示后台运行
docker container ls   #查看运作中的容器,STATUS列是容器状态
docker container inspect nginxtest #查看容器详细信息
docker container exec -it nginxtest bash  #连接到容器内的命令行(容器内可以执行的shell命令很少),可以查看nginx配置文件/etc/nginx/nginx.conf
docker container --help  #查看命令帮助
docker container stop nginxtest
docker container ls -a  #查看所有容器,包括停止的容器
docker container rm nginxtest #彻底删除容器

对于初学者来说可以将容器理解为进程,而镜像就是程序的二进制文件

3.容器配置
docker run --help 可以查看运行容器的参数配置,主要的配置有端口映射,存储映射,cpu和内存配置
3.1创建nginx配置文件

[root@testdsq nginxconf]# pwd
/root/test/nginxconf
[root@testdsq nginxconf]# ls
hello.conf
[root@testdsq nginxconf]# cat hello.conf 
server {
        listen  8000;
        location / {
           return 200 "hello";
        }
}

3.2创建容器

docker container run -d --name nginxmap -v /root/test/nginxconf/:/etc/nginx/conf.d -p 8000:8000 --cpus 2 --memory 500M nginx
   #-p 8000:8000  第一个是主机端口,第二个是容器端口
   #-v 主机文件路径:容器内路径  

3.3测试

#查看容器ip
docker container inspect nginxmap  #"IPAddress": "172.17.0.3"
#访问容器8000端口
curl http://172.17.0.2:8000
#访问主机的8000端口
curl http://192.168.1.77:8000
#查看容器资源消耗情况
docker container stats nginxmap
posted @ 2024-04-19 16:51  董少奇  阅读(33)  评论(0)    收藏  举报