1.拉取映象
docker pull nginx
2.建立容器
docker run --name my-nginx -p 80:80 -d nginx
3.找個資料夾建立以下目錄,mkdir -p {conf,conf.d,html,logs}
4.從建立的nginx容器中複製配置檔案到本地目錄
9a2becc47dfc是你建立的nginx容器id,docker cp 容器ID:源位置 目標位置
docker cp 9a2becc47dfc:/etc/nginx/nginx.conf /Users/zhangcheng/Desktop/project/docker/nginx/conf
docker cp 9a2becc47dfc:/etc/nginx/conf.d/default.conf /Users/zhangcheng/Desktop/project/docker/nginx/conf.d
5.刪除容器
docker rm 9a2becc47dfc
6.建立新容器
建立容器並掛載目錄
docker run --name my_nginx -d \ -p 80:80 \ -v /Users/zhangcheng/Desktop/project/docker/nginx/html:/usr/share/nginx/html \ -v /Users/zhangcheng/Desktop/project/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /Users/zhangcheng/Desktop/project/docker/nginx/conf.d:/etc/nginx/conf.d \ -v /Users/zhangcheng/Desktop/project/docker/nginx/logs:/var/log/nginx \ nginx
7.在html目錄建立index.html
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>
8.訪問 http://localhost/