环境设置
[root@localhost ~]# systemctl start docker
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# swapoff -a
[root@localhost ~]# vim /etc/fstab
下载编排工具
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose ##给执行权限
[root@localhost ~]# mv docker-compose /usr/local/bin/ ##放到/usr/local/bin/下便于调用
清空所有镜像和容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
创建目录编写Dockerfile文件
[root@localhost bin]# mkdir -p /root/compose_nginx/nginx ##创建工作目录
[root@localhost ~]# cp -p docker-compose /usr/local/bin/
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# mkdir /root/compose_nginx
[root@localhost ~]# ls
anaconda-ks.cfg compose_nginx docker-compose Documents index.html
Music original-ks.cfg Public Templatesapache Desktop Dockerfile Downloads initial-setup-ks.cfg mysql Pictures run.sh Videos
[root@localhost compose_nginx]# mkdir nginx
[root@localhost compose_nginx]# cd nginx/
[root@localhost nginx]# vim Dockerfile # 编写Nginx Dockerfile
FROM centos:7
MAINTAINER ghw
RUN yum -y update
RUN yum -y install make gcc gcc-gcc pcre-devel zlib-devel
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.12.2
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
添加源码包
[root@localhost nginx]# rz -E
rz waiting to receive.
[root@localhost nginx]# ls
Dockerfile nginx-1.12.2.tar.gz
创建启动文件
[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
[root@localhost nginx]# ls
Dockerfile nginx-1.12.2.tar.gz run.sh
编写yaml文件
[root@localhost nginx]# cd ..
[root@localhost compose_nginx]# vim docker-compose.yaml
version: '3' # 版本号为 3
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80 #
- 1217:443
networks:
- abc
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
abc:
[root@localhost nginx]# docker-compose -f /root/compose_nginx/docker-compose.yaml up -d
Successfully built 38eb34df7256
Successfully tagged compose_nginx_nginx:latest
WARNING: Image for service nginx was built because it did not already exist. To rebuild this image
you must use `docker-compose build` or `docker-compose up --build`.
Creating compose_nginx_nginx_1 ... done
[root@localhost nginx]# cd /root/compose_nginx/
[root@localhost compose_nginx]# ls
docker-compose.yaml nginx wwwroot
[root@localhost wwwroot]# vim index.html # 往主页添加内容
<h1>hello world this is ky06 !</h1>
然后查看IP检验结果