dockerfile案例
dockerfile构建nginx
FROM centos:7
RUN rm -rf /etc/yum.repos.d/*
ADD Centos-7.repo /etc/yum.repo.d/
ADD epel-7.repo /etc/yum.repo.d/
RUN yum -y install nginx
EXPOSE 80
CMD ["/usr/sbin/nginx","-g","daemon off;"]
dockerfile构建nginx镜像
[root@node1 ~]# docker build -t cznginx-test .
[+] Building 11.3s (8/9) docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 231B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/5] FROM docker.io/library/centos:7 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 66B 0.0s
=> CACHED [2/5] RUN rm -rf /etc/yum.repos.d/* 0.0s
=> [3/5] ADD Centos-7.repo /etc/yum.repos.d/ 0.0s
=> [4/5] ADD epel-7.repo /etc/yum.repos.d/ 0.0s
=> [5/5] RUN yum -y install nginx 11.3s
查看
[root@node1 ~]# docker images | grep nginx
cznginx-test latest ab2af01390e1 About a minute ago 536MB
使用这个镜像来运行容器
[root@node1 ~]# docker run -itd --name nginx-cz -p 80:80 cznginx-test:latest
149d7e61d541bec5a3baccc87977ae6b64bd331222ef061cea5edf592c9281e1
[root@node1 ~]# docker ps |grep nginx-cz
149d7e61d541 cznginx-test:latest "/usr/sbin/nginx -g …" 24 seconds ago Up 23 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-cz
[root@node1 ~]#
进入此容器添加一个网页
[root@node1 ~]# docker run -itd --name nginx-cz -p 80:80 cznginx-test:latest
149d7e61d541bec5a3baccc87977ae6b64bd331222ef061cea5edf592c9281e1
[root@node1 ~]# docker ps |grep nginx-cz
149d7e61d541 cznginx-test:latest "/usr/sbin/nginx -g …" 24 seconds ago Up 23 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-cz
[root@node1 ~]# docker exec -it nginx-cz /bin/bash
[root@149d7e61d541 /]# cd /usr/share/nginx/
[root@149d7e61d541 nginx]# cd html/
[root@149d7e61d541 html]# ls
404.html 50x.html en-US icons img index.html nginx-logo.png poweredby.png
[root@149d7e61d541 html]# rm -rf index.html
[root@149d7e61d541 html]# echo hello > index.html
访问
扫雷案例
1、在docker主机中部署apache服务,并将saolei.zip放置到该站点中,使其通过http://docker主机ip/saolei.zip可以下载
[root@docker]# yum -y install apache2
2、将Centos-7.repo和epel-7.repo文件上传至/root目录
[root@docker]# ls /root/
Centos-7.repo epel-7.repo
3、# 将saolei.zip包放置/var/www/html目录下
[root@docker]# ls /var/www/html/
index.html saolei.zip
4、# 重启apache服务并设置下次启动生效
[root@docker]# systemctl restart apache2
[root@docker]# systemctl enable apache2
新建Dockerfile
FROM centos:7
RUN rm -rf /etc/yum.repos.d/*
ADD Centos-7.repo /etc/yum.repos.d/
ADD epel-7.repo /etc/yum.repos.d/
RUN yum -y install tomcat unzip curl
WORKDIR /var/lib/tomcat/webapps
RUN curl -O http://192.168.100.10/saolei.zip && \
unzip saolei.zip && \
mv saolei ROOT
EXPOSE 8080
ADD init.sh /init.sh
CMD ["/bin/bash","/init.sh"]
写一个shell脚本
[root@node1 ~]# cat init.sh
#!/bin/bash
/usr/libexec/tomcat/server star
[root@node1 ~]#
构建镜像
[root@node1 ~]# docker build -t saolei-123 .
[+] Building 0.1s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 371B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/8] FROM docker.io/library/centos:7 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 147B 0.0s
=> CACHED [2/8] RUN rm -rf /etc/yum.repos.d/* 0.0s
=> CACHED [3/8] ADD Centos-7.repo /etc/yum.repos.d/ 0.0s
=> CACHED [4/8] ADD epel-7.repo /etc/yum.repos.d/ 0.0s
=> CACHED [5/8] RUN yum -y install tomcat unzip curl 0.0s
=> CACHED [6/8] WORKDIR /var/lib/tomcat/webapps 0.0s
=> CACHED [7/8] RUN curl -O http://192.168.100.10/saolei.zip && unzip saolei.zip && mv saolei ROOT 0.0s
=> [8/8] ADD init.sh /init.sh 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:e37e772c1581ca10d27dc86e37a592779a8c5dcdc721ef7d313dd9bc251d5fb0 0.0s
=> => naming to docker.io/library/saolei-123 0.0s
查看构建的镜像
[root@node1 ~]# docker images |grep saolei-123
saolei-123 latest e37e772c1581 34 seconds ago 717MB
运行saolei容器
[root@node1 ~]# docker run -itd --name saolei-max -p 8888:8080 saolei-123:latest
c3e373ca9d0864752a414bc3a4177f56e4a7cd1c2a494df32fdd82394b495dac
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3e373ca9d08 saolei-123:latest "/bin/bash /init.sh" 4 seconds ago Up 3 seconds 0.0.0.0:8888->8080/tcp, [::]:8888->8080/tcp saolei-max
访问 192.168.100.10:8888/saolei.jsp
可道云案例
[root@docker opt]# mkdir dockerfile
[root@docker opt]# cd dockerfile
[root@docker dockerfile]# mkdir kod
[root@docker dockerfile]# cd kod/
上传可道云包和nginx配置文件
[root@docker kod]# ls
dockerfile kodexplorer4.40.zip nginx.con
//nginx配置文件
[root@node1 kod]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /code;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /code$fastcgi_script_name;
include fastcgi_params;
}
}
}
新建dockerfile
[root@node1 kod]# pwd
/opt/dockerfile/kod
[root@node1 kod]# cat dockerfile
FROM centos:7
RUN rm -rf /etc/yum.repos.d/*
ADD /root/Centos-7.repo /etc/yum.repos.d/
ADD /root/epel-7.repo /etc/yum.repos.d/
RUN yum -y install nginx php-fpm php-gd php-mbstring unzip
RUN sed -i '/^user/c user=nginx' /etc/php-fpm.d/www.conf
RUN sed -i '/^group/c group=nginx' /etc/php-fpm.d/www.conf
COPY nginx.conf /etc/nginx/nginx.conf
RUN mkdir /code
WORKDIR /code
COPY kodexplorer4.40.zip .
RUN unzip kodexplorer4.40.zip
RUN chown -R nginx.nginx .
ADD init.sh /init.sh
EXPOSE 80
ENTRYPOINT ["/bin/bash","/init.sh"]
编写脚本
[root@node1 kod]# pwd
/opt/dockerfile/kod
[root@node1 kod]# cat init.sh
#!/bin/bash
php-fpm -D
echo "$1" >> /etc/nginx/nginx.conf
nginx -g 'daemon off;
构建镜像
[root@node1 kod]# docker build -t kod:v123 .
[+] Building 0.1s (19/19) FINISHED docker:default
=> [internal] load build definition from dockerfile 0.0s
=> => transferring dockerfile: 547B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [ 1/14] FROM docker.io/library/centos:7 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 254B 0.0s
=> CACHED [ 2/14] RUN rm -rf /etc/yum.repos.d/* 0.0s
=> CACHED [ 3/14] ADD Centos-7.repo /etc/yum.repos.d/ 0.0s
=> CACHED [ 4/14] ADD epel-7.repo /etc/yum.repos.d/ 0.0s
=> CACHED [ 5/14] RUN yum -y install nginx php-fpm php-gd php-mbstring unzi 0.0s
=> CACHED [ 6/14] RUN sed -i '/^user/c user=nginx' /etc/php-fpm.d/www.conf 0.0s
=> CACHED [ 7/14] RUN sed -i '/^group/c group=nginx' /etc/php-fpm.d/www.con 0.0s
=> CACHED [ 8/14] COPY nginx.conf /etc/nginx/nginx.conf 0.0s
=> CACHED [ 9/14] RUN mkdir /code 0.0s
=> CACHED [10/14] WORKDIR /code 0.0s
=> CACHED [11/14] COPY kodexplorer4.40.zip . 0.0s
=> CACHED [12/14] RUN unzip kodexplorer4.40.zip 0.0s
=> CACHED [13/14] RUN chown -R nginx.nginx . 0.0s
=> [14/14] ADD init.sh /init.sh 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:f4fe4f58c05cf304152d3b162311247a023a38cf08c79e84 0.0s
=> => naming to docker.io/library/kod:v123 0.0s
0.0s
查看镜像
[root@node1 kod]# docker images | grep kod
kod 123 0d1bf90d5eb3 4 minutes ago 676MB
运行容器
[root@node1 kod]# docker run -itd --name kod-test -p 9999:80 kod:v123
db7688b167d3c42b4557fcd191983c2e356b1f1ca41c138e626c3ac1ebc29ef2
[root@node1 kod]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db7688b167d3 kod:v123 "/bin/bash /init.sh" 3 seconds ago Up 3 seconds 0.0.0.0:9999->80/tcp, [::]:9999->80/tcp kod-test
[root@node1 kod]#
访问 192.168.100.10:9999