昨天在dockers安装了nginx,安装倒是很简单docker pull nginx:版本号
,不写版本号默认是pull最新版。运行就是docker run --name nginx -p 8080:80 -d nginx
然后访问·localhost:8080
就可以看到nginx的欢迎界面了。
虽然docker安装很方便,但是对于刚入门的要想修改里面的配置文件就不简单了,
网上有很多讲解如何在本地目录上挂载配置文件的文章。但是对于我这种新手来说看得晕头转向。
希望我这篇文章可以给被dockers墙上的程序员一点帮助-_-。
我们安装完nginx 后会发现配置文件在哪,日志文件目录在哪,web项目放到那个目录下,等等。
关于docker的简单的原理解释可以参考docker
实际上docker run一个镜像会生成一个容器,而我们需要修改的配置文件日志文件等等都在这个容器里,但是要想进入容器修改配置却不容易,下面介绍二种方法。
docker run --name docker_nginx -d -p 8080:80 nginxId
[root@localhost nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34d0daa6b54c nginx "nginx -g 'daemon ..." 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp docker_nginx
a2926eeff28f mysql:5.7 "docker-entrypoint..." 2 weeks ago Up 22 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp hardcore_perlman
[root@localhost nginx]# docker exec -it 34d0 bash
root@34d0daa6b54c:/#
注意(docker 的Id可以简写为完整id的前几个字符)
cat /etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
修改会提示vim: command not foundroot@34d0daa6b54c:/etc/nginx/conf.d# vim /etc/nginx/nginx.conf
bash: vim: command not found
因为容器是与本地几乎隔绝的,所以我们要先安装vim
1. 先更新包管理
apt-get update
2. 安装vim
apt-get install vim
时间比较慢,如果有找到如何更新安装源的小伙伴贴到评论区分享下
安装完成后就可以修改配置文件了
// 退出容器
root@34d0daa6b54c:/etc/nginx/conf.d# exit
exit
[root@localhost nginx]#
日志目录 : /var/log/nginx
配置目录 : /etc/nginx/conf.d
主配置文 : 件/etc/nginx/nginx.conf
项目目录 : /usr/share/nginx/html nginx
这个操作对于以后修改很方便,推荐这一种
[root@localhost nginx]# cd /home/hewcen/mynginx/nginx/
[root@localhost nginx]# ls
conf conf.d html log
[root@localhost nginx]#
vim /home/hewcen/mynginx/nginx/conf/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;
}
vim /home/hewcen/mynginx/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#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;
#}
}
配置文件创建完成后我们在新建一个index.html
/home/hewcen/mynginx/nginx/html index.html
<html>
<head>
<title>Welcome to nginx!title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
style>
head>
<body>
<h1>hello docker!h1>
body>
html>
docker run --name docker_nginx -d -p 8080:80 \
-v /home/hewcen/mynginx/nginx/log:/var/log/nginx \
-v /home/hewcen/mynginx/nginx/conf.d:/etc/nginx/conf.d \
-v /home/hewcen/mynginx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/hewcen/mynginx/nginx/html:/usr/share/nginx/html nginx
挂载日志目录
挂载配置目录
挂载主配置文件
挂载项目目录
docker logs 运行容器的Id
docker ps -a
--privileged=true \