本篇文章给大家谈谈Docker安装Nginx环境,以及docker安装nginx的知识点,同时本文还将给你拓展Centos7安装nginx(二)之docker中安装nginx、dockerDocker
本篇文章给大家谈谈Docker安装Nginx环境,以及docker 安装 nginx的知识点,同时本文还将给你拓展Centos7安装nginx(二)之docker中安装nginx、docker Dockerfile学习---构建nginx环境、docker下如何安装Nginx环境的详细过程、Docker使用Centos76镜像安装Nginx环境等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- Docker安装Nginx环境(docker 安装 nginx)
- Centos7安装nginx(二)之docker中安装nginx
- docker Dockerfile学习---构建nginx环境
- docker下如何安装Nginx环境的详细过程
- Docker使用Centos76镜像安装Nginx环境
Docker安装Nginx环境(docker 安装 nginx)
Centos7安装nginx(二)之docker中安装nginx
一、简介
本人最近对k8s+docker容器云、微服务进行一些调研,尝试着在docker中安装Nginx。先来安利docker一波,真是神器,但是对于docker的有点本人将在docker的相关文章中介绍。
- 需求:做自动化运维的同学可能需要测试自己写完的shell脚本,docker是一个比虚拟机快照更方便的选择。只需要启动一个docker容器,将写好的脚本以挂载文件的方式,在docker容器中运行即可(copy进去再执行也可以)。
- 环境: 虚拟机+docker
- 优点:当shell脚本有问题的时候,直接kill掉docker容器,修改完shell后重启一个容器再运行即可。
- 目的:为了练习Nginx的安装配置。由于docker使用Linux内核namespace\cgroup等隔离技术,近似等同于虚拟机(有些隔离做的不彻底,不能完全等同于),在docker中练习安装Nginx比跟在虚拟机上安装相差无异,但开销更少。如果仅仅是为了在docker中使用Nginx,只需要直接拉取Nginx镜像再开启容器即可。
二、安裝过程
在docker中安装Nginx的步骤跟上一篇文章Centos7安装nginx(一)大致相同。需要注意的是在docker中使用systemctl必须以特殊的方式启动容器,否则会报错Docker: Failed to get D-Bus connection: Operation not permitted
安装过程主要分为二部分:
- 启动容器
- 安装Nginx
安装部分参考文章Centos7安装nginx(一),本文重点讲解如何启动docker容器,避免在配置systemctl来管理Nginx时出现报错信息。
1. 启动容器
- 容器的正确启动姿势
docker pull centos:7
(拉取镜像)
docker images
(查看镜像具体信息)
步骤3中的镜像版本要与拉去下来的版本信息对应docker run -d --name Nginx --privileged=true docker.io/centos:7 /usr/sbin/init
(以后台运行的方式启动容器)
docker exec -it Nginx /bin/bash
(进入容器)
- docker容器中使用systemctl报错总结
-
Docker: Failed to get D-Bus connection: Operation not permitted
- 原因
- 这个的原因是因为dbus-daemon没能启动。启动容器时,带上参数
--privileged=true
、将你的CMD或者entrypoint设置为/usr/sbin/init
即可(两者缺一不可)。会自动将dbus等服务启动起来。
- 这个的原因是因为dbus-daemon没能启动。启动容器时,带上参数
- 解决办法:
- 在启动容器时添加参数
- docker run -d --name centos7 --privileged=true centos:7 /usr/sbin/init
- docker exec -it centos7 /bin/bash
- 在启动容器时添加参数
- 原因
2. 安装Nginx
参考文章Centos7安装nginx(一)
三、总结
本文主要记录了docker中使用systemctl的报错问题,为下一篇文章Centos7安装nginx(三)之shell脚本自动化安装nginx,通过shell自动化安装Nginx做准备。文章可能在编辑过程中,由于个人疏忽、不同版本markdown解析器不兼容等原因导致字符书写错误,导致安装失败。本文属于原创,若有引用请注明出处。若有疑问或错误,欢迎各位指出,可以评论或者跟本人联系。
docker Dockerfile学习---构建nginx环境
1、创建项目目录并上传包
$ mkdir docker_Nginx
$ cd docker_Nginx
下载Nginx包
$ wget http://Nginx.org/download/Nginx-1.8.0.tar.gz
2、编辑Dockerfile
# From表示使用centos:latest这个镜像为基础构建我们的镜像
FROM centos:latest
# 创建者的基本信息
MAINTAINER xiaozhou (xiaozhou@docker.com)
LABEL discription="基于centos的Nginx镜像" version="1.0"
# put Nginx-1.8.0.tar.gz into /usr/local/src and unpack Nginx
ADD Nginx-1.8.0.tar.gz /usr/local/src
#安装Nginx所依赖的包
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel \
&& yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel \
&& useradd -M -s /sbin/nologin Nginx
# change dir to /usr/local/src/Nginx-1.8.0
workdir /usr/local/src/Nginx-1.8.0
# execute command to compile Nginx
RUN ./configure --user=Nginx --group=Nginx --prefix=/usr/local/Nginx \
--with-file-aio --with-http_ssl_module --with-http_realip_module \
--with-http_addition_module --with-http_xslt_module \
--with-http_image_filter_module --with-http_geoip_module \
--with-http_sub_module --with-http_dav_module --with-http_flv_module \
--with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module \
--with-http_auth_request_module --with-http_random_index_module \
--with-http_secure_link_module --with-http_degradation_module \
--with-http_stub_status_module \
&& make && make install
# Nginx_config
RUN mkdir /usr/local/Nginx/run \
&& sed -e '3i\user Nginx;' -i /usr/local/Nginx/conf/Nginx.conf \
&& sed -e "9i\error_log /usr/local/Nginx/logs/error.log;" -i /usr/local/Nginx/conf/Nginx.conf \
&& sed -e "12i\pid /usr/local/Nginx/run/Nginx.pid;" -i /usr/local/Nginx/conf/Nginx.conf \
&& sed -e "29i\ \taccess_log /usr/local/Nginx/logs/access.log;" -i /usr/local/Nginx/conf/Nginx.conf
# logrotate 日志切割
RUN touch /etc/logrotate.d/Nginx \
&& echo -e "$LOGS_DIR/*.log {" >> /etc/logrotate.d/Nginx \
&& echo -e "\tdaily" >> /etc/logrotate.d/Nginx \
&& echo -e "\trotate" >> /etc/logrotate.d/Nginx \
&& echo -e "\tmissingok" >> /etc/logrotate.d/Nginx \
&& echo -e "\tdateext" >> /etc/logrotate.d/Nginx \
&& echo -e "\tcompress" >> /etc/logrotate.d/Nginx \
&& echo -e "\tdelaycompress" >> /etc/logrotate.d/Nginx \
&& echo -e "\tnotifempty" >> /etc/logrotate.d/Nginx \
&& echo -e "\tsharedscripts" >> /etc/logrotate.d/Nginx \
&& echo -e "\tpostrotate" >> /etc/logrotate.d/Nginx \
&& echo -e "\t/usr/bin/kill -USR1 \`cat $LOGS_DIR/Nginx.pid\`" >> /etc/logrotate.d/Nginx \
&& echo -e "\tendscript" >> /etc/logrotate.d/Nginx \
&& echo -e "\t}" >> /etc/logrotate.d/Nginx
ENV PATH /usr/local/Nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
CMD /bin/sh -c 'Nginx -g "daemon off;"'
3、构建镜像
$ docker build -t centos:Nginx .
4、启动容器
$ docker run -it -d -P 80 centos:Nginx
查看启动的容器
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d350b2c8a3d5 centos:Nginx "/bin/sh -c '/bin/sh…" 3 seconds ago Up 2 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp wonderful_elion
浏览器输入宿主机地址:
http://192.168.121.121:32769
可以看到 Welcome to Nginx! 的页面,说明环境构建没问题。
docker下如何安装Nginx环境的详细过程
本篇文章给大家分享的内容是关于
一、 环境说明
docker: 18.03.1-ce
nginx: 1.15.1
二、 拉取最新的 Nginx 镜像
拉取镜像
$ docker pull nginx
查看当前镜像
$ docker images # 查询结果: REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8b89e48b5f15 7 hours ago 109MB
三、 准备工作
本次将web服务部署在 /srv/web 目录下:
3.1 创建 /srv/web 目录 并进入该目录
$ cd /srv && mkdir web && cd web
3.2 随便创建一个 Nginx 容器,并拷贝 Nginx 的默认配置:
创建容器:
$ docker run -d --name nginx nginx
从容器中拷贝配置文件至本地:
# 查看 ==> 获取容器ID $ docker container ls # 在当前目录下创建目录:conf $ mkdir conf # 拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录 $ docker cp a89b2c5f3dd1:/etc/nginx/nginx.conf $PWD/conf
删除容器:
# 停止容器 $ docker container stop a89b2c5f3dd1 # 删除容器 $ docker container rm a89b2c5f3dd1
四、 开始正式部署
部署命令:
$ docker run -d -p 8081:80 --name nginx-web-6666 -v $PWD/html:/usr/share/nginx/html -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx nginx
命令详细说明:
-d # 表示在一直在后台运行容器 -p 8081:80 # 对端口进行映射,将本地8081端口映射到容器内部的80端口 --name # 设置创建的容器名称 -v # 将本地目录(文件)挂载到容器指定目录;
五、 测试
5.1 测试
如果是本地测试部署则打开:localhost:8081 即可访问到 web 服务器;
5.2 补充:
因为是将容器内的 nginx 的根目录给挂载到本地指定目录,所以上面访问到的页面应该会报 403 错误;接下来可以在 /srv/web/html/ 开始我们的项目;
5.3 进入本地目录:/srv/web/html/ 创建测试文件 index.html
$ cd /srv/web/html # 创建并随便编写内容 重新刷新页面 $ vim index.html
相关推荐:
如何通过Docker搭建一个swoft开发环境
用Docker创建nginx反向代理以上就是
Docker使用Centos76镜像安装Nginx环境
1. 创建文件夹
docker容器运行期间的文件在容器终止时会被清空,所以需要对一些目录进行映射到宿主机,以此保留运行期间的数据到宿主机,也方便根据产生的数据进行环境还原。
需要映射的文件夹如下:
1. nginx文件配置目录:E:\docker\www\centos\nginx\conf:/usr/local/nginx/conf
2. nginx日志目录:E:\docker\www\centos\nginx\logs:/usr/local/nginx/logs
2. 创建centos7.6容器
docker run -itd -v E:\docker\www\centos\nginx\conf:/usr/local/nginx/conf -v E:\docker\www\centos\nginx\logs:/usr/local/nginx/logs --name centos7_6 --network php_env --network-alias centos7_6 -p 80:80 centos:centos7.6.1810 /bin/bash
3. 进入centos7.6容器安装nginx
1. 进入容器:docker exec -it centos7_6 /bin/bash
2. 进入用户目录:cd /usr/local
3. 安装nginx依赖
yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
4. 安装wget
yum install -y wget
5. 下载nginx1.9.9版本的压缩包
wget http://nginx.org/download/nginx-1.9.9.tar.gz
6. 解压nginx1.9.9的压缩包
tar -zxvf nginx-1.9.9.tar.gz
7. 进入解压文件夹
cd nginx-1.9.9
8. 检查依赖并且编译安装
./configure && make && make install
4. 搭建多项目环境
1. 创建/usr/local/nginx/conf/vhosts文件夹
mkdir /usr/local/nginx/conf/vhosts
2. 修改nginx.cong配置文件,增加多项目配置读取,编辑/usr/local/nginx/conf/nginx.conf文件,在http块的底部增加语句:
include /usr/local/nginx/conf/vhosts/*.conf;
5. 启动ngnix
1. 启动nginx
/usr/local/nginx/sbin/nginx
2. 浏览器访问http://127.0.0.1
显示nginx页面表示启动成功
关于Docker安装Nginx环境和docker 安装 nginx的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Centos7安装nginx(二)之docker中安装nginx、docker Dockerfile学习---构建nginx环境、docker下如何安装Nginx环境的详细过程、Docker使用Centos76镜像安装Nginx环境的相关信息,请在本站寻找。
本文标签: