Docker启动Nginx

搜索镜像

docker search nginx

image

拉取镜像

这里拉取的官方镜像

docker pull nginx

image

创建挂载目录

将nginx的文件都放在/opt/nginx/下,这里手动创建下

mkdir -p /opt/nginx/conf
mkdir -p /opt/nginx/logs
mkdir -p /opt/nginx/html

image

复制容器conf.d文件夹和nginx.conf文件到宿主机中

由于要挂载nginx的配置、日志、页面,以方便修改配置,查看日志等;所以我们先启动一个容器,然后将容器中的配置、日志、页面的文件复制到宿主机,以便后面挂载用;

docker run -d -p 8001:80 --name nginx nginx

image

将容器nginx.conf文件复制到宿主机

docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/conf/nginx.conf

将容器conf.d文件夹下内容复制到宿主机

docker cp nginx:/etc/nginx/conf.d /opt/nginx/conf/conf.d

将容器中的html文件夹复制到宿主机

docker cp nginx:/usr/share/nginx/html /opt/nginx

image-20230223150946585

最后将容器删除

m -f 容器Id

运行容器

docker run -d --name nginx -p 8801:80 nginx

运行容器映射数据卷

docker run -d --name nginx -p 8801:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d nginx