docker制作镜像 监控

一、自动制作dockerfile。启动双服务(FROM+CMD+RUN+ENV+ADD)

RUN yum install httpd openssh-server -y
RUN echo ’ 123456’ |passwd --stdin root
CMD /etc/ init.a/ sshd start; /usr/ sbin/httpd -D FOREGROUND

RUN yum install httpd openssh-server -y
RUN echo ’ 123456’ Ipasswd --stdin root
ADD init.sh / init. sh
CMD [" /bin/bash", “/init. sh”]

ADD拷贝包只能解压tar.gz的包

环境变量设置密码,在设置密码的过程中将echo "$SSH_PASS" | passwd --stdin root写在脚本里,构建镜像,运行镜像时加上--env “SSH_PASS=654321”,进入容器使用该密码

用户忘记docker run时传入环境变量,就给dockerfile中设置默认值
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200226134001592.png)
如果忘记传入密码环境变量,就是上一次设置的密码

[root@docker01 httpd_ssh]# vim dockerfile

FROM centos:6.9

RUN yum install httpd openssh-server -y

ADD docker.sh /docker.sh
CMD ["/bin/bash","/docker.sh"]

[root@docker01 httpd_ssh]# cat docker.sh
#!/bin/bash
echo “$SSH_PASS” | passwd --stdin root
/etc/init.d/sshd start
/usr/sbin/httpd -D FOREGROUND

docker build -t http_ssh:v4 .

[root@docker01 httpd_ssh]# docker run -d -e “SSH_PASS=654321”-p 84:80 -p 1025:22 http_ssh:v4

[root@docker01 httpd_ssh]# ssh root@10.0.0.11 -p 1026
The authenticity of host ‘[10.0.0.11]:1026 ([10.0.0.11]:1026)’ can’t be established.
RSA key fingerprint is SHA256:PXOoflPz0rE8C4wWBe+EU+S2v+laIUApzj8tzn36WAA.
RSA key fingerprint is MD5:4c:4a:fe:95:67:75:bb:b6:da:56:2c:b2:9b:db:41:ea.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘[10.0.0.11]:1026’ (RSA) to the list of known hosts.
root@10.0.0.11’s password:
[root@ab96b440f4e5 ~]#

**CMD---ENTRYPOINT**

[root@docker01 dockerfile]# docker run -d http_ssh:v4 sleep 100
9dee004d9e0069eb28930bba134eee74f1a5b4420f85cff502c2123331da64ff
[root@docker01 dockerfile]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9dee004d9e00 http_ssh:v4 “sleep 100” 18 seconds ago Up 7 seconds 80/tcp vigilant_lovelace

将元dockerfile文件中的CMD替换为ENTRYPOINT

[root@docker01 httpd_ssh]# cat dockerfile
FROM centos:6.9

RUN yum install httpd openssh-server -y

ADD docker.sh /docker.sh
ENTRYPOINT ["/bin/bash","/docker.sh"]

[root@docker01 httpd_ssh]# docker build -t http_ssh:v6 .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM centos:6.9
—> eafa9248528c
Step 2/4 : RUN yum install httpd openssh-server -y
—> Using cache
—> 93ba9239bb32
Step 3/4 : ADD docker.sh /docker.sh
—> Using cache
—> 981bb759fc8f
Step 4/4 : ENTRYPOINT ["/bin/bash","/docker.sh"]
—> Running in a3f0cf025744
Removing intermediate container a3f0cf025744
—> 44f4bcc1f5af
Successfully built 44f4bcc1f5af
Successfully tagged http_ssh:v6
[root@docker01 httpd_ssh]# docker run -d http_ssh:v
v1 v2 v3 v4 v6
[root@docker01 httpd_ssh]# docker run -d http_ssh:v6 sleep 100
3f917917dbb2cf5ca5676ffed95e95382c0741bad40eb8095891d78bc7f607d3
[root@docker01 httpd_ssh]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f917917dbb2 http_ssh:v6 “/bin/bash /docker.s…” 14 seconds ago Up 8 seconds 80/tcp xenodochial_sinoussi

**此时,启动容器的初始命令将不再改变**sleep 100反而成为脚本的参数
### 二、docker的分层技术
**作用:节省空间、复用**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200226140605430.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTU4MTQ4Nw==,size_16,color_FFFFFF,t_70)
容器是和宿主机共用一个内核
- 最底层放入一个tar包就可以做成一个镜像(alpine、centos)
- 只要是镜像就是一个只读层,当基于镜像启动了一个容器之后,就会在镜像的基础上做增量,就是说容器会在此基础上挂载一个可写层。文件变化(装nginx,建文件)保留一在可写层里面
提交镜像其实就是提交了可写层
- 提交镜像完成后又多了一层
**总结**:镜像是一层,启动容器就多了一层可写层,装了软件就多了有软件的镜像,并且又多了一层可写层,总共四层,算上内核共五层

[root@docker01 httpd_ssh]# docker history centos:6.9
IMAGE CREATED CREATED BY SIZE COMMENT
eafa9248528c 5 days ago /bin/bash 257MB
adf829198a7f 18 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
18 months ago /bin/sh -c #(nop) LABEL name=CentOS Base Im… 0B
18 months ago /bin/sh -c #(nop) ADD file:b99efdfca7b4df4ef… 195MB

此镜像只有两层(文件变化的层)

[root@docker01 httpd_ssh]# docker history centos:6.9 |grep -v “0B”
IMAGE CREATED CREATED BY SIZE COMMENT
eafa9248528c 5 days ago /bin/bash 257MB
18 months ago /bin/sh -c #(nop) ADD file:b99efdfca7b4df4ef… 195MB

### 将镜像导出为tar包

[root@docker01 httpd_ssh]# docker save centos:6.9 -o docker_centos6.9.tar.gz

[root@docker01 httpd_ssh]# docker save centos:6.9 > docker_centos6.9.tar.gz

### 将10.0.0.11的镜像传到10.0.0.12

scp -rp docker_centos6.9.tar.gz 10.0.0.12:/root

docker load -i docker_centos6.9.tar.gz

按层导入
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200226142237519.png)
相同的层已经存在就不会再导入
**镜像分层的作用:节省空间、复用、提高了上传下载的速度**

[root@docker01 httpd_ssh]# docker save centos:6.9 -o docker_centos6.9.tar.gz
[root@docker01 httpd_ssh]# scp -rp docker_centos6.9.tar.gz 10.0.0.12:/root
root@10.0.0.12’s password:
docker_centos6.9.tar.gz

[root@docker02 ~]# docker load -i docker_centos6.9.tar.gz
b5e11aae8a8e: Loading layer [>] 202.9MB/202.9MB
264d676c9e96: Loading layer [
>] 259.6MB/259.6MB
Loaded image: centos:6.9

[root@docker02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 6.9 eafa9248528c 5 days ago 451MB
alpine latest e7d92cdc71fe 5 weeks ago 5.59MB
busybox latest d8233ab899d4 12 months ago 1.2MB

### 三、dockerfile优化
不同的linux系统,配置文件的路径不一样
不同的linux系统,包管理工具不一样
```l
centos:  yum自动解决rpm依赖  rpm=redhat package manager
Ubuntu: apt-get                         apt=advance package tools
alpine:      apk add                       apk=alpine package kit

zabbix每一个服务都会做到docker镜像里。

alpine基础镜像

[root@docker01 httpd_ssh]# docker run -it -p 80:80 alpine:latest /bin/sh
/ # hostname 
350b45b30a2d

下载安装nginx服务

  • 替换源
    在这里插入图片描述
/ # sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
/ # cat /etc/apk/repositories 
http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.11/main
http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.11/community
  • 更新
/ # apk update
fetch http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
v3.11.3-86-gc5fc897261 [http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.11/main]
v3.11.3-79-gcdba3c9b8f [http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.11/community]
OK: 11266 distinct packages available
  • apk add 安装
/ # apk add nginx
/ # nginx -v
nginx version: nginx/1.16.1

最新版的
启动时报错

/ # nginx
/ # nginx: [emerg] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)
/ # mkdir /run/nginx
/ # nginx
/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/ # netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      27/nginx: master pr
/ # curl 127.0.0.0
curl: (7) Couldn't connect to server
/ # curl 127.0.0.1
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

404的原因

/ # vi /etc/nginx/conf.d/default.conf
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # Everything is a 404
        location / {
                return 404;
        }

        # You may need this to prevent return 404 recursion.
        location = /404.html {
                internal;
        }
}

更改

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # Everything is a 404
        location / {
           root /html;     
           index index.html;
}                                                           
                                                            
        # You may need this to prevent return 404 recursion.
        location = /404.html {
                internal;
        }
}
/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/ # nginc
/bin/sh: nginc: not found
/ # nginx -s stop
/ # nginx
/ # mkdir  /html
/ # cd /html/

/html # echo "nihao" >index.html
/html # curl 127.0.0.1
nihao

提交为镜像

[root@docker01 httpd_ssh]# docker commit 350b45b30a2d test:v1
sha256:9b9b0f1717c8eb631a1c57a9a5ad6def3de8d9dba90c8a5136224653c3e6e771
[root@docker01 httpd_ssh]# docker images
REPOSITORY                                       TAG                 IMAGE ID            CREATED             SIZE
test                                             v1                  9b9b0f1717c8        55 seconds ago      9.9MB

才9.9M
优化:
在这里插入图片描述
yum会产生缓存,在后面加上命令yum clean all,重新构建,和上次的作比较

[root@docker01 httpd_ssh]# docker images|grep http
http_ssh                                         v7                  367462579ed0        29 seconds ago      479MB
http_ssh                                         v6                  44f4bcc1f5af        About an hour ago   576MB

忽略文件的在文件里直接写文件名
dockerfile中用到的文件不能忽略,不然会报错找不到文件

自动创建镜像跑kod服务

[root@docker01 kod]# grep -Ev "^$" dockerfile 
FROM centos:6.9
ADD nginx.repo /etc/yum.repo.d/nginx.repo
RUN yum install nginx -y && \
 yum install php-fpm php-mbstring php-gd -y && \
 sed -i '/^user/s#apache#nginx#g' /etc/php-fpm.d/www.conf && \
 sed -i '/^group/s#apache#nginx#g' /etc/php-fpm.d/www.conf && \
 
 yum clean all
ADD default.conf /etc/nginx/conf.d/default.conf
RUN mkdir /html
WORKDIR /html
ADD kod.tar.gz /html
RUN chown -R nginx:nginx .
ADD init.sh /init.sh
CMD ["/bin/bash","/init.sh"]

[root@docker01 kod]# docker build -t kod:v3 .
[root@docker01 kod]# docker exec -it 29c78b398cea /bin/bash
[root@29c78b398cea html]#

[root@docker01 kod]# docker ps -a -l
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                  NAMES
29c78b398cea        kod:v3              "/bin/bash /init.sh"   3 minutes ago       Up 3 minutes        0.0.0.0:8081->80/tcp   laughing_fermat

四、容器间的互联

[root@docker01 kod]# docker rm -f `docker ps -a -q` 

清一波容器
同一个宿主机启动两个容器

[root@docker01 kod]# docker run -it alpine
/ # ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
109: eth0@if110: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.088 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.067 ms
^C
--- 172.17.0.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.067/0.077/0.088 ms
[root@docker01 kod]# docker run -it alpine:latest 
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
107: eth0@if108: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=34.204 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.066 ms
^C
--- 172.17.0.3 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.066/17.135/34.204 ms

docker之间默认网络就是通的172.17.0.0/16
当docker容器死掉了,网络ip就释放,如果启动别的容器,刚才释放的ip就会被占用。ip地址对于容器来说不固定,期中架构中服务和服务连接就不固定会出问题。
LB —>WEB01
—>WEB02

如何然容器和容器之间稳定
[root@docker01 kod]# docker run -d --name sun01 kod:v3

其他容器相连接它
[root@docker01 kod]# docker run -it --link sun01:web01 alpine:latest 
/ # ping sun
PING sun01 (120.197.235.6): 56 data bytes

sun01是启动容器设置的名字,web01是别名

link容器间的互联是单方向的

docker使用在期中架构启动顺序
数据库—web—lb

五、部署zabbix

批量导入镜像

docker-mysql-5.7.tar.gz  zabbix-java-gateway.tar.gz  zabbix-server-mysql.tar.gz  zabbix-web-nginx-mysql.tar.gz
[root@docker01 srv]# for n in `ls *`;do docker load -i $n;done

启动容器(来自zabbix官网)

docker run --name mysql-server -it \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="root_pwd" \
      -d mysql:5.7 \
      --character-set-server=utf8 --collation-server=utf8_bin
     
docker run --name zabbix-java-gateway -t \
      -d zabbix/zabbix-java-gateway:latest
    
docker run --name zabbix-server-mysql -t \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="root_pwd" \
      -e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
      --link mysql-server:mysql \
      --link zabbix-java-gateway:zabbix-java-gateway \
      -p 10051:10051 \
      -d zabbix/zabbix-server-mysql:latest
      
docker run --name zabbix-web-nginx-mysql -t \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="root_pwd" \
      --link mysql-server:mysql \
      --link zabbix-server-mysql:zabbix-server \
      -p 80:80 \
      -d zabbix/zabbix-web-nginx-mysql:latest

解释:-e带的是环境变量
–link 连接数据库和其他需要连接
在这里插入图片描述
密码铭文明文显示在这里插入图片描述
初始:Admin—zabbix
在这里插入图片描述

其实就是启动了四个容器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值