基于上次打包grafana服务,在服务的基础上部署postgres、redis、nginx三个服务,在每增加一个服务时将容器重新打包成镜像。
充分体现了docker的优点:1、跨平台可移植性,按统一标准打包;2、面向应用,优化部署应用;3、版本控制;4、组件复用,组件式搭建;5、共享性,公共的注册服务器。
下面展示操作过程:
1、将镜像到处迁移到其他机器上运行
导出镜像:
导入镜像:
运行容器:
[root@docker-slave-79101 ~]# docker run -d -p 3000:3000 -p 3222:22 centos_grafana:v1 /usr/local/promethues/grafana-7.1.5/bin/grafana-server
[root@docker-slave-79101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4665c5ad1a8 centos_grafana:v1 "/usr/local/promet..." 46 minutes ago Up 46 minutes 0.0.0.0:3000->3000/tcp, 0.0.0.0:5432->5432/tcp, 0.0.0.0:3222->22/tcp nostalgic_stall
man
登录容器:
[root@docker-slave-79101 ~]# docker exec -it e4665c5ad1a8 /bin/bash
容器启动ssh登录:
注:若提醒密码不正确,可以登录容器修改root密码:
打包运行的容器成镜像
[root@docker-slave-79101 ~]# docker commit -a="believer" -m="base on grafana then add postgres server" e4665c5ad1a8 centos_grafana_postgres_redis:v1
sha256:430665c43ed40fa073102ee50c7314202d1374522016f134e842543792c31e68
[root@docker-slave-79101 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_grafana_postgres_redis v1 430665c43ed4 About a minute ago 2.32 GB
centos_grafana_postgres v1 e95dd4aaf7a9 6 minutes ago 2.13 GB
centos_grafana v1 d91e67ddd15b 2 days ago 1.84 GB
[root@docker-slave-79101 ~]# docker rmi 430665c43ed4
Untagged: centos_grafana_postgres_redis:v1
Deleted: sha256:430665c43ed40fa073102ee50c7314202d1374522016f134e842543792c31e68
Deleted: sha256:3351c3e77e953bc93b2e454999a085ef9eb92e7fde7d1abeb43180172acf50f5
[root@docker-slave-79101 ~]# docker commit -a="believer" -m="base on grafana then add postgres server and redis server" e4665c5ad1a8 centos_grafana_postgres_redis:v2
sha256:03ca71d3d334217c7361d000bea6b8049dd9097073a609bc6188d261ff42bbf2
[root@docker-slave-79101 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_grafana_postgres_redis v2 03ca71d3d334 6 seconds ago 2.32 GB
centos_grafana_postgres v1 e95dd4aaf7a9 6 minutes ago 2.13 GB
centos_grafana v1 d91e67ddd15b 2 days ago 1.84 GB
[root@docker-slave-79101 ~]# ll
将所有服务写成启动脚本:
[root@9f6b9d859c89 grafana-7.1.5]# cat /opt/start.sh
#!/bin/bash
/usr/sbin/sshd -D &
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/data/redis/bin/redis-server /data/redis/etc/redis.conf
su - postgres -c 'pg_ctl -D /data/12 start'
/usr/local/promethues/grafana-7.1.5/bin/grafana-server
提交运行的容器:
[root@docker-slave-79101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES3be84e9d403f centos_grafana_postgres_redis_nginx:v4 "/usr/local/promet..." 4 minutes ago Exited (0) About a minute ago
flamboyant_bose31c8f09e8b9c centos_grafana_postgres_redis_nginx:v3 "/usr/sbin/sshd -D" 17 minutes ago Up 58 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:3000->3000/tcp, 0.0.0.0:5
432->5432/tcp, 0.0.0.0:6379->6379/tcp, 0.0.0.0:4222->22/tcp happy_winge4665c5ad1a8 centos_grafana:v1 "/usr/local/promet..." About an hour ago Exited (0) 44 minutes ago
nostalgic_stallman
[root@docker-slave-79101 ~]# docker commit -a="believer" -m="base on grafana then add postgres server and redis server and nginx servers" 31c8f09e8b9c centos_grafana_postgres_redis_nginx:v6
sha256:f9deffae25935d5ae473f2b9ca86dbed1cf3c42c16a6799ff82c1bbf1b40390c
[root@docker-slave-79101 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_grafana_postgres_redis_nginx v6 f9deffae2593 6 seconds ago 2.53 GB
centos_grafana_postgres_redis_nginx v4 268762e01870 9 minutes ago 2.49 GB
centos_grafana_postgres_redis_nginx v3 ef25a98a25f3 20 minutes ago 2.53 GB
centos_grafana_postgres_redis v2 03ca71d3d334 About an hour ago 2.32 GB
centos_grafana_postgres v1 e95dd4aaf7a9 About an hour ago 2.13 GB
centos_grafana v1 d91e67ddd15b 2 days ago 1.84 GB
利用已提交的镜像运行新的容器:
docker run -p 4222:22 -p 80:80 -p 6379:6379 -p 5432:5432 -p 3000:3000 centos_grafana_postgres_redis_nginx:v6 /opt/start.sh
登录容器检查服务和端口
[root@docker-slave-79101 ~]# docker exec -it 9f6b9d859c89 /bin/bash
[root@9f6b9d859c89 grafana-7.1.5]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 19:41 ? 00:00:00 /bin/bash /opt/start.sh
root 8 1 0 19:41 ? 00:00:00 /usr/sbin/sshd -D
root 10 1 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 12 10 0 19:41 ? 00:00:00 nginx: worker process
root 13 1 0 19:41 ? 00:00:00 /data/redis/bin/redis-server 127.0.0.1:6379
postgres 32 1 0 19:41 ? 00:00:00 /opt/postgres/bin/postgres -D /data/12
postgres 34 32 0 19:41 ? 00:00:00 postgres: checkpointer process
postgres 35 32 0 19:41 ? 00:00:00 postgres: writer process
postgres 36 32 0 19:41 ? 00:00:00 postgres: wal writer process
postgres 37 32 0 19:41 ? 00:00:00 postgres: autovacuum launcher process
postgres 38 32 0 19:41 ? 00:00:00 postgres: stats collector process
postgres 39 32 0 19:41 ? 00:00:00 postgres: bgworker: logical replication launcher
root 40 1 1 19:41 ? 00:00:00 /usr/local/promethues/grafana-7.1.5/bin/grafana-server
root 52 0 0 19:41 ? 00:00:00 /bin/bash
root 67 52 0 19:42 ? 00:00:00 ps -ef
[root@9f6b9d859c89 grafana-7.1.5]# ss -tlnp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6379 *:*
users:(("redis-server",pid=13,fd=6))LISTEN 0 128 *:80 *:*
users:(("nginx",pid=10,fd=6))LISTEN 0 128 *:22 *:*
users:(("sshd",pid=8,fd=3))LISTEN 0 128 127.0.0.1:5432 *:*
LISTEN 0 128 :::22 :::*
users:(("sshd",pid=8,fd=4))LISTEN 0 128 :::3000 :::*
users:(("grafana-server",pid=40,fd=10))LISTEN 0 128 ::1:5432 :::*
实操记录暂且告一段落,有什么不足之处,欢迎指正和交流~~~