当前位置:   首页安装配置

容器成功:CentOS7 Docker Nginx部署及运行详解

发布日期:2022-02-02 11:03 | 文章来源:gibhub

网上找了一些资料部署,出现不一样的问题,现在总结一下自己的部署流程。

1、资源准备

Dockerfile文件

# "ported" by Adam Miller <maxamillion@fedoraproject.org> from 
#  https://github.com/fedora-cloud/Fedora-Dockerfiles 
# 
# Originally written for Fedora-Dockerfiles by 
#  scollier <scollier@redhat.com> 
 
FROM centos:centos7 
MAINTAINER The CentOS Project <cloud-ops@centos.org> 
 
RUN yum -y update; yum clean all 
RUN yum -y install epel-release tar ; yum clean all 
RUN yum -y install nginx ; yum clean all 
ADD nginx.conf /opt/deploy/nginx/nginx.conf 
RUN echo "daemon off;" >> /opt/deploy/nginx/nginx.conf 
#RUN curl https://git.centos.org/sources/httpd/c7/acf5cccf4afaecf3afeb18c50ae59fd5c6504910 \ 
#  | tar -xz -C /usr/local/nginx/html \ 
#  --strip-components=1 
#RUN sed -i -e 's/Apache/nginx/g' -e '/apache_pb.gif/d' \  
#  /usr/local/nginx/html/index.html 
 
EXPOSE 80 
 
#CMD [ "/usr/local/nginx/sbin" ] 

注意:路径需要在系统上面存在以及对应

nginx.conf文件

# For more information on configuration, see: 
#  * Official English Documentation: http://nginx.org/en/docs/ 
#  * Official Russian Documentation: http://nginx.org/ru/docs/ 
 
user nginx; 
worker_processes 1; 
 
error_log /usr/logs/nginx/error.log; 
#error_log /var/log/nginx/error.log notice; 
#error_log /var/log/nginx/error.log info; 
 
pid    /run/nginx.pid; 
 
 
events { 
  worker_connections 1024; 
} 
 
 
http { 
  include    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_age

注意:路径需要在系统上面存在以及对应

2、执行构建镜像命令

复制代码 代码如下:

[root@localhost nginx]# sudo docker build --rm --tag os7/nginx:centos7 .

执行结果截图:

3、查看镜像是否安装构建成功 Docker images

4、创建容器 docker run -i -t -d -p 192.168.32.129:81:80 os7/nginx /bin/bash

注意:192.168.32.129这个IP的话,则需要在/etc/hosts中添加

192.168.32.129 localhost

5、查看容器是否创建成功并启动 docker ps

6、测试是否成功访问 curl http://192.168.32.129:81

会出现这个拒绝连接,那怎么办呢?有办法解决的,我们先进入该容器里面

7、进入容器 docker exec -i -t small_hodgkin /bin/sh

8、接着在容器里面执行(直接输入即可)

nginx

9、在容器外面执行 curl http://192.168.32.129:81

成功了。

10、再到虚拟机外面通过浏览器访问

到此为止就成功了。

参考资料:https://github.com/CentOS/CentOS-Dockerfiles

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

联系我们
关于使用场景和技术架构的更多咨询,请联系我们的销售和技术支持团队。
Yingsoo Host

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部