一个小公司也用上了docker,经常需要部署nginx服务,我通常都会把他们上传到一个目录,大概就是下面的样子
就是一个简单的nginx配置(因为经常需要配置代理不想进入容器和拷贝,就挂出来了),
和指定一个静态文件目录,(静态文件需要经常更新也不想进入容器和拷贝,就挂出来了)
文件更新后需要重启一下容器,不然可能出现更新内容不生效
docker run --restart=always --name nginx80 -p 80:80 -v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf -v $(pwd)/dist/:/usr/share/nginx/html/ -d nginx
总的来说,这条命令创建并启动了一个名为“nginx80”的Docker容器,使用Nginx镜像,监听80端口,挂载了两个卷来存储配置和静态文件,并且设置了容器在退出后自动重启。
这条命令是用于在Docker中运行一个名为“nginx”的容器。下面是对这条命令的逐项解释:
docker run
: 这是Docker的一个命令,用于启动一个新的容器实例。--restart=always
: 这个选项意味着容器会在退出后自动重启。这对于需要持续运行的服务(如web服务器)是非常有用的,因为它确保容器在出现问题或被手动停止后仍会自动恢复运行。--name nginx80
: 这为容器指定了一个名称“nginx80”,这样你就可以通过这个名字来引用或管理这个容器,而不是使用默认的随机名称。-p 80:80
: 这是端口映射设置。它将宿主机的80端口映射到容器的80端口。这意味着,如果你有一个web应用程序在容器内运行并监听80端口,你可以通过访问宿主机的80端口来访问它。-v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf
: 这是卷(volume)挂载设置。它将当前目录下的default.conf
文件挂载到容器的/etc/nginx/conf.d/default.conf
路径上。这意味着你可以修改这个default.conf
文件来更改Nginx的配置,并且这些更改将在容器重启后保留。-v $(pwd)/dist/:/usr/share/nginx/html/
: 这是另一个卷挂载设置。它将当前目录下的dist/
目录挂载到容器的/usr/share/nginx/html/
路径上。Nginx默认使用这个目录作为其静态文件服务的根目录,所以你可以将你的HTML、CSS、JavaScript等文件放在这个目录下,并直接通过Nginx来访问它们。-d nginx
: 这告诉Docker使用名为“nginx”的镜像来创建并运行容器。
下面是默认的nginx配置文件default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}