从零开始Ubuntu24.04上Docker构建自动化部署(三)Docker安装Nginx
安装nginx
sudo docker pull nginx
启动nginx
宿主机创建目录
sudo mkdir -p /home/nginx/{conf,conf.d,html,logs}
先启动nginx
sudo docker run -d --name mynginx -p 80:80 nginx
宿主机上拷贝docker上nginx服务上文件到本地目录
sudo docker cp mynginx:/etc/nginx/nginx.conf /home/nginx/conf/
sudo docker cp mynginx:/etc/nginx/conf.d /home/nginx/
sudo docker cp mynginx:/usr/share/nginx/html /home/nginx/
sudo docker cp mynginx:/var/log/nginx /home/nginx/logs
停止docker 上nignx
sudo docker stop mynginx
删除docker上nginx容器
sudo docker rm mynginx
重启启动nginx容器
sudo docker run -d --name mynginx -p 80:80 --restart=always --volume /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf --volume /home/nginx/conf.d:/etc/nginx/conf.d --volume /home/nginx/html:/usr/share/nginx/html --volume /home/nginx/logs:/var/log/nginx --privileged=true nginx
还有种方式,如果不创建目录,将原始文件拷贝过来,也可在启动后,手动创建文件
创建nginx.conf文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf; }
创建default.conf文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /home/nginx/html;
index index.html index.htm;
}
}
修改完成后,重启nginx
当访问浏览器时,会找不到页面存在,不过不影响部署成功
如果实在想要,那就自己写个html页面放到/home/nginx/html下即可