使用Dockerfile构建一个Ansible镜像

Docker生态虽然已经很强大,大部分场景都有对应的镜像,但是有时候自己会有一些特殊需求,而且不想每次都要去配置环境,这时候使用Dockerfile构建自己的镜像就很有必要了。

什么是Dockerfile

Dockerfile类似于一个脚本,能够按照你写的程序去自动生成一个新的镜像文件,然后再利用这个镜像文件创建一个容器,就能得到你需要的开发/部署环境了,而且一次构建,随处可用。

PS: Dockerfile是一个固定的文件名

如何构建属于自己的镜像

编写Dockerfile

输入下面的内容

FROM debian:10
COPY sources.list /etc/apt/sources.list
RUN apt update;apt install -y ansible sshpass vim net-tools
RUN sed -i 's/^#host_key_checking.*/host_key_checking = False/g' /etc/ansible/ansible.cfg
CMD ["/bin/bash"]

效果如下

[root@bxy1 docker]# cat Dockerfile 
FROM debian:10
COPY sources.list /etc/apt/sources.list
RUN apt update;apt install -y ansible sshpass vim net-tools
RUN sed -i 's/^#host_key_checking.*/host_key_checking = False/g' /etc/ansible/ansible.cfg
CMD ["/bin/bash"]
[root@bxy1 docker]# 

sources.list是一个源配置文件,和Dockerfile文件在同一级

  • FROM 意思就是使用哪个基础镜像
  • COPY 就是复制本地的哪个文件到容器镜像
  • RUN 就是执行一个Linux命令
  • CMD 就是设置容器启动的时候需要运行什么程序

开始构建

使用下面的语法进行镜像构建

 docker build -t ${镜像名称:版本} .

例如

 docker build -t debian10/ansible .

当不设置版本的时候,默认会给予latest的一个版本标签

构建效果

[root@bxy1 docker]# docker build -t debian10/ansible .
Sending build context to Docker daemon  3.584kB
Step 1/5 : FROM debian:10
 ---> d5af59919bab
Step 2/5 : COPY sources.list /etc/apt/sources.list
 ---> Using cache
 ---> e2c792b6cee8
Step 3/5 : RUN apt update;apt install -y ansible sshpass vim net-tools
 ---> Using cache
 ---> a7e1c4bfeb45
Step 4/5 : RUN sed -i 's/^#host_key_checking.*/host_key_checking = False/g' /etc/ansible/ansible.cfg
 ---> Using cache
 ---> ea055fb4b227
Step 5/5 : CMD ["/bin/bash"]
 ---> Running in a8577ce3fd41
Removing intermediate container a8577ce3fd41
 ---> 25c0393c9b27
Successfully built 25c0393c9b27
Successfully tagged debian10/ansible:latest

因为我之前执行过,所以某些步骤的执行过程就不会再显示了

验证

镜像验证

执行

docker images
[root@bxy1 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
debian10/ansible    latest              25c0393c9b27        About a minute ago   339MB

debian              10                  d5af59919bab        6 weeks ago          108MB

然后就看到已经生成了一个debian10/ansible镜像了

容器创建验证

镜像生成之后,我们使用这个新的镜像创建一个容器看看

执行命令

docker run -tid --name ansible-liumou debian10/ansible:latest
docker exec -ti ansible-liumou /bin/bash

效果

[root@bxy1 docker]# docker run -tid --name ansible-liumou debian10/ansible:latest
881b1656d6704fad415adbe180aae1596a2160956c1b0f31d1ee3a0e916a6013
[root@bxy1 docker]# docker exec -ti ansible-liumou /bin/bash
root@881b1656d670:/# ansible
Usage: ansible <host-pattern> [options]

Define and run a single task 'playbook' against a set of hosts

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
  --ask-vault-pass      ask for vault password
  -B SECONDS, --background=SECONDS
                        run asynchronously, failing after X seconds
                        (default=N/A)
  -C, --check           don't make any changes; instead, try to predict some
                        of the changes that may occur
  -D, --diff            when changing (small) files and templates, show the
                        differences in those files; works great with --check
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS
                        set additional variables as key=value or YAML/JSON, if
                        filename prepend with @
  -f FORKS, --forks=FORKS
                        specify number of parallel processes to use
                        (default=5)
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
                        specify inventory host path or comma separated host
                        list. --inventory-file is deprecated
  -l SUBSET, --limit=SUBSET
                        further limit selected hosts to an additional pattern
  --list-hosts          outputs a list of matching hosts; does not execute
                        anything else
  -m MODULE_NAME, --module-name=MODULE_NAME
                        module name to execute (default=command)
  -M MODULE_PATH, --module-path=MODULE_PATH
                        prepend colon-separated path(s) to module library
                        (default=['/root/.ansible/plugins/modules',
                        '/usr/share/ansible/plugins/modules'])
  -o, --one-line        condense output
  --playbook-dir=BASEDIR
                        Since this tool does not use playbooks, use this as a
                        subsitute playbook directory.This sets the relative
                        path for many features including roles/ group_vars/
                        etc.
  -P POLL_INTERVAL, --poll=POLL_INTERVAL
                        set the poll interval if using -B (default=15)
  --syntax-check        perform a syntax check on the playbook, but do not
                        execute it
  -t TREE, --tree=TREE  log output to this directory
  --vault-id=VAULT_IDS  the vault identity to use
  --vault-password-file=VAULT_PASSWORD_FILES
                        vault password file
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)
  --version             show program's version number and exit

  Connection Options:
    control as whom and how to connect to hosts

    -k, --ask-pass      ask for connection password
    --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
                        use this file to authenticate the connection
    -u REMOTE_USER, --user=REMOTE_USER
                        connect as this user (default=None)
    -c CONNECTION, --connection=CONNECTION
                        connection type to use (default=smart)
    -T TIMEOUT, --timeout=TIMEOUT
                        override the connection timeout in seconds
                        (default=10)
    --ssh-common-args=SSH_COMMON_ARGS
                        specify common arguments to pass to sftp/scp/ssh (e.g.
                        ProxyCommand)
    --sftp-extra-args=SFTP_EXTRA_ARGS
                        specify extra arguments to pass to sftp only (e.g. -f,
                        -l)
    --scp-extra-args=SCP_EXTRA_ARGS
                        specify extra arguments to pass to scp only (e.g. -l)
    --ssh-extra-args=SSH_EXTRA_ARGS
                        specify extra arguments to pass to ssh only (e.g. -R)

  Privilege Escalation Options:
    control how and which user you become as on target hosts

    -s, --sudo          run operations with sudo (nopasswd) (deprecated, use
                        become)
    -U SUDO_USER, --sudo-user=SUDO_USER
                        desired sudo user (default=root) (deprecated, use
                        become)
    -S, --su            run operations with su (deprecated, use become)
    -R SU_USER, --su-user=SU_USER
                        run operations with su as this user (default=None)
                        (deprecated, use become)
    -b, --become        run operations with become (does not imply password
                        prompting)
    --become-method=BECOME_METHOD
                        privilege escalation method to use (default=sudo),
                        valid choices: [ sudo | su | pbrun | pfexec | doas |
                        dzdo | ksu | runas | pmrun | enable | machinectl ]
    --become-user=BECOME_USER
                        run operations as this user (default=root)
    --ask-sudo-pass     ask for sudo password (deprecated, use become)
    --ask-su-pass       ask for su password (deprecated, use become)
    -K, --ask-become-pass
                        ask for privilege escalation password

Some modules do not make sense in Ad-Hoc (include, meta, etc)
ERROR! Missing target hosts
root@881b1656d670:/# 
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坐公交也用券

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值