Docker的Ansible自动化应用
解决国内无法下载镜像问题
国内的服务器无法正常下载仓库的镜像了,只能到其他地区下载,再推送到阿里的镜像,再下载
原来阿里的镜像构建是可以用的,现在也不能下载了
docker pull busybox
docker tag docker.io/library/busybox:latest registry.cn-hangzhou.aliyuncs.com/k8s-a/ingress-nginx:v1.3.0
docker push registry.cn-hangzhou.aliyuncs.com/k8s-a/ingress-nginx:v1.3.0
Dockerfile 文件
# 创建Docke容器模板
FROM registry.cn-hangzhou.aliyuncs.com/k8s-a/ingress-nginx:v1.3.0
MAINTAINER Stanley <anyux3306@163.com>
# 容器启动时运行的命令
CMD ["/bin/ping","www.baidu.com"]
构建镜像
docker build -t test .
运行容器
docker run -it --name=test test
64 bytes from 110.242.68.3: seq=432 ttl=127 time=33.401 ms
64 bytes from 110.242.68.3: seq=433 ttl=127 time=46.890 ms
64 bytes from 110.242.68.3: seq=434 ttl=127 time=27.122 ms
64 bytes from 110.242.68.3: seq=435 ttl=127 time=15.560 ms
^C
--- www.baidu.com ping statistics ---
436 packets transmitted, 435 packets received, 0% packet loss
round-trip min/avg/max = 13.509/23.774/573.077 ms
使用Ansible创建和管理容器
Ansible 提供的 Docker 相关模块已经集成在 community.docker 集合中,而不是直接通过单个 docker 模块
默认情况下,Ansible 会将集合安装在用户的本地目录 ~/.ansible/collections/ 下。如果你需要安装到特定路径,可以使用 -p 参数指定路径
ansible-galaxy collection install community.docker
#查看docker模块
ansible-doc -l | grep docker
查看安装模块位置
ansible-galaxy collection list
---
- hosts: localhost
connection: local
tasks:
- name: Build Docker image from Dockerfiles.
community.docker.docker_image:
name: test:v1.13
build:
path: /root/
source: build
---
- hosts: localhost
connection: local
tasks:
- name: Run a Docker container using nginx image
community.docker.docker_container:
name: test2
image: test:v1.13
state: started