# 1.构建上下文和所需的文件
[root@localhost ~]# mkdir dockerfile-test
[root@localhost ~]# cd dockerfile-test/
[root@localhost dockerfile-test]# touch nginx.repo
[root@localhost dockerfile-test]# touch Dockerfile
# 2.编辑Dockerfile
[root@localhost dockerfile-test]# cat Dockerfile
FROM centos
LABEL maintainer='xjt'
RUN rm -rf /etc/yum.repos.d/*
COPY ./nginx.repo /etc/yum.repos.d
RUN yum makecache
RUN yum install -y nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
[root@localhost dockerfile-test]# cat nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# 4.镜像构建时网络问题处理(非必须)
# 报错:WARNING: IPv4 forwarding is disabled. Networking will not work.
解决办法:
vi /etc/sysctl.conf
net.ipv4.ip_forward=1 #添加这段代码
# 重启network服务
systemctl restart network
# 查看是否修改成功 (备注:返回1,就是成功)
[root@studyCMachine aaaaaaa]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
# 5.构建镜像
[root@localhost dockerfile-test]# docker build -t centos-nginx-xjt:1.0 .
# 6.启动容器
[root@localhost dockerfile-test]# docker run -d -p 8000:80 --name my-nginx centos-nginx-hqs:1.0
e5be77c444922817b69acc15810d186634765d27ec0d8ea8e7b345acffc610fa
# 7.访问验证 curl
[root@localhost dockerfile-test]# curl 127.0.0.1:8000
标签:nginx,Dockerfile,案例,构建,test,root,dockerfile,localhost
From: https://www.cnblogs.com/caifa/p/17466246.html