Docker学习之安装nginx

首先docker pull nginx下载镜像 

[root@iZbp17hqnide6jadyyzp0oZ server]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
6ec8c9369e08: Pull complete 
d3cb09a117e5: Pull complete 
7ef2f1459687: Pull complete 
e4d1bf8c9482: Pull complete 
795301d236d7: Pull complete 
Digest: sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c
Status: Downloaded newer image for nginx:latest

使用docker images 查看镜像

[root@iZbp17hqnide6jadyyzp0oZ server]# docker images
REPOSITORY                                                  TAG                 IMAGE ID            CREATED             SIZE
nginx                                                       latest              8cf1bfb43ff5        9 days ago          132MB

使用docker run -d --name nginx -p 9999:80 -v /usr/local/server/nginx/:/etc/nginx nginx启动nginx并将/etc/nginx下面目录挂载出来

[root@iZbp17hqnide6jadyyzp0oZ nginx]# docker run -d --name nginx -p 9999:80 -v /usr/local/server/nginx/:/etc/nginx nginx
9901ba698570e56c260a46bfb25d9bf94e4481b0a40928729d190e51acb9a389

使用docker logs -f nginx查看日志

[root@iZbp17hqnide6jadyyzp0oZ nginx]# docker logs -f nginx 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2020/07/31 06:25:49 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

发现启动失败了,原因open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)。应该是nginx.conf在本地没有匹配到,不知道生成文件夹还是文件(映射到我本地生成了一个nginx.conf的文件夹,所以这么猜测,有大佬会可以教下我)。

解决办法:一个文件一个文件映射或者创建出这些文件在进行挂载

我这里用了个笨处理办法

先docker run -d --name nginx -p 9999:80 nginx。将nginx镜像启动起来,完后执行docker cp nginx:/etc/nginx /usr/local/server/将目录拷贝出来。

[root@iZbp17hqnide6jadyyzp0oZ server]# docker run -d --name nginx -p 9999:80 nginx
566743191cb818a5c05d02540d5778476693e62991e9d635a345dbcb6e56c49d
[root@iZbp17hqnide6jadyyzp0oZ server]# docker cp nginx:/etc/nginx /usr/local/server/
[root@iZbp17hqnide6jadyyzp0oZ server]# cd nginx/
[root@iZbp17hqnide6jadyyzp0oZ nginx]# ls
conf.d  fastcgi_params  koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params  uwsgi_params  win-utf

完后删除容器重新执行docker run 挂载目录,执行成功(不知道还有没有直接一点的方法)

[root@iZbp17hqnide6jadyyzp0oZ nginx]# docker rm -f nginx 
nginx
[root@iZbp17hqnide6jadyyzp0oZ nginx]# docker run -d --name nginx -p 9999:80 -v /usr/local/server/nginx/:/etc/nginx nginx
5a01ec72db7a1d9ad48a4c18fbec973fa289563203394dee21dd0c39d1b6b010
[root@iZbp17hqnide6jadyyzp0oZ nginx]# docker logs -f nginx 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: error: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

访问nginx获得下面页面,代表安装成功

Docker学习之安装nginx_第1张图片

 

你可能感兴趣的:(nginx,docker,docker,nginx)