一、 Dockerfie
1、构建Apache容器服务
[root@docker docker]# mkdir apache # 创建一个新的目录 apache
[root@docker docker]# cd apache/
[root@docker apache]# vim Dockerfile #编辑生成一个Dockerfile文件
FROM centos:7 # 指定新镜像基于centos:7镜像
MAINTAINER this is apache #镜像的维护者
RUN yum -y update #在基于镜像centos:7上执行更新yum仓库
RUN yum -y install httpd #在基于镜像上yum安装http服务
EXPOSE 80 # 指定端口80
ADD index.html /var/www/html/index.html # 不指定则复制Dockerfile所在目录下的index.html文件到容器中的/var/www/html目录下
ADD run.sh /run.sh # 复制Dockerfile文件所在目录下的run.sh文件到容器中的目录下
RUN chmod +x /run.sh #在基于镜像上为根目录下的run.sh文件赋予可执行权限
CMD ["/run.sh"] # 启动容器时运行容器根目录下的run.sh文件
[root@docker apache]# echo 'this is apache' > index.html # 生成并默认首页文件中注入内容
[root@docker apache]# ls
Dockerfile index.html
[root@docker apache]# vim run.sh #编辑httpd启动脚本
#!/bin/bash
rm -rf /run/httpd
宿主机访问
2、 构建ssh镜像
[root@docker docker]# mkdir sshd
[root@docker docker]# cd sshd
[root@docker sshd]# vim Dockerfile
FROM centos:7
MAINTAINER this is sshd
RUN yum -y update #更新镜像yum源
RUN yum -y install openssh* net-tools lsof telnet passwd
#在基于镜像上安装软件
#openssh是ssh协议的免费开源实现
#net-tools组件可支持使用ifconfig命令查看网卡信息
#losf查看进程打开的文件、打开文件的进程、进程打开的端口
#telnet支持远程登录
#passwd用于设置密码,更新用户身份令牌
RUN echo '123456' | passwd --stdin root #更改root用户密码为123456
将ssh服务端配置文件中所有的UsePAM yes替换为UsePAM no,关闭第三方认证登录,别人都无法远程登录该主机
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
#创建非密钥对文件,生成到/etc/ssh/目录下为ssh_host_rsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd
#嵌套创建/root/.ssh /目录并且将/root目录属主和属组改为root并且为/root/.ssh目录赋予权限700,只有root用户可读写执行
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22 #指定端口
CMD ["/usr/sbin/sshd","-D"] #在运行容器时启动ssh服务,并开启守护进程
[root@docker sshd]# docker build -t mysshd:1 .
3、Nginx 升级优化
3.1 :未优化版
[root@docker nginx0]# ls
Dockerfile nginx-1.12.2.tar.gz
[root@docker nginx0]# vim Dockerfile
FROM centos:7 #基于centos:7镜像
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make #安装依赖环境
ADD nginx-1.12.2.tar.gz /mnt #将源码复制到指定目录,并解压
WORKDIR /mnt/nginx-1.12.2 # 为下面的指令指定执行目录
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc #关闭Debug调试
RUN ./configure --prefix=/usr/local/nginx # 安装编译
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"] # 指定挂载点
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"] #为了保持nginx的容器不退出,关闭nginx后台运行
[root@docker nginx]# docker build -t nginx:v0 . #使用当前目录下的文件构建标签nginx:v0的镜像
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v0 86be3d064eb0 22 hours ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker nginx]# docker run -d -P --name nginx0 nginx:v0
ee74ba4ae4a08d5de42979cecfc786a00a16ce29ae1035a9748e4ac36cdb118e
[root@docker nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee74ba4ae4a0 nginx:v0 "/usr/local/nginx/sb…" 20 seconds ago Up 20 seconds 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx0
3.2 优化1:不需要输出的丢入/dev/null
[root@docker nginx]# cat Dockerfile
FROM centos:7
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && yum clean all
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx &> /dev/null
RUN make &> /dev/null
RUN make install &> /dev/null
RUN rm -rf /mnt/nginx-1.12.2 #删除不需要的文件
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx""-g","daemon off;"]
[root@docker nginx]# docker build -t nginx:v1 .
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 e50592ec82ab 22 hours ago 310MB
nginx v0 86be3d064eb0 22 hours ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker nginx]# docker run -d -P --name nginx1 nginx:v1
024023f4d31563633dd2f46cf3fe066a9d355cf0b959843a2181e42fc0fd73c9
[root@docker nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
024023f4d315 nginx:v1 "/usr/local/nginx/sb…" 4 seconds ago Up 4 seconds 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginx1
ee74ba4ae4a0 nginx:v0 "/usr/local/nginx/sb…" 2 minutes ago Up 2 minutes 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx0
3.3 :优化2 减少RUN构建
[root@docker nginx]# cat Dockerfile #因为RUN较少。所以效果不明显
FROM centos:7
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \
yum clean all && \
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
./configure --prefix=/usr/local/nginx &> /dev/null && \
make &> /dev/null && make install &> /dev/null &&\
rm -rf /mnt/nginx-1.12.2
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker nginx]# docker build -t nginx:v2 .
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 c3cba2299823 22 hours ago 308MB
nginx v1 e50592ec82ab 22 hours ago 310MB
nginx v0 86be3d064eb0 22 hours ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker nginx]# docker run -d -P --name nginx2 nginx:v2
2eee585cca14b5c0630d8527b37b8d99c911e3d0e5d9a23d5f921564b68922ef
[root@docker nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2eee585cca14 nginx:v2 "/usr/local/nginx/sb…" 4 seconds ago Up 4 seconds 0.0.0.0:49156->80/tcp, :::49156->80/tcp nginx2
024023f4d315 nginx:v1 "/usr/local/nginx/sb…" About a minute ago Up About a minute 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginx1
ee74ba4ae4a0 nginx:v0 "/usr/local/nginx/sb…" 3 minutes ago Up 3 minutes 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx0
3.4 多阶段构建
[root@docker nginx]# ls
Dockerfile nginx-1.12.2.tar.gz
[root@docker nginx]# cat Dockerfile
FROM centos:7 as build
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && yum clean all && \
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
./configure --prefix=/usr/local/nginx &> /dev/null && \
make &> /dev/null && make install &> /dev/null && \
rm -rf /mnt/nginx-1.12.2
FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker nginx]# docker build -t nginx:v2 .
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v3 bc5dc4bc6ba0 22 hours ago 205MB
nginx v2 c3cba2299823 22 hours ago 308MB
nginx v1 e50592ec82ab 22 hours ago 310MB
nginx v0 86be3d064eb0 22 hours ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker nginx]# docker run -d -P --name nginx3 nginx:v3
8e33015056a9ca57c38326830429739132bc2761ab535424212feba67e4f3cf6
[root@docker nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e33015056a9 nginx:v3 "/usr/local/nginx/sb…" 7 seconds ago Up 6 seconds 0.0.0.0:49157->80/tcp, :::49157->80/tcp nginx3
2eee585cca14 nginx:v2 "/usr/local/nginx/sb…" About a minute ago Up About a minute 0.0.0.0:49156->80/tcp, :::49156->80/tcp nginx2
024023f4d315 nginx:v1 "/usr/local/nginx/sb…" 2 minutes ago Up 2 minutes 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginx1
ee74ba4ae4a0 nginx:v0 "/usr/local/nginx/sb…" 4 minutes ago Up 4 minutes 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx0
|