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
文件
cat <<EOF > sources.list
deb http://mirrors.ustc.edu.cn/debian/ buster main
deb-src http://mirrors.ustc.edu.cn/debian/ buster main
deb http://mirrors.ustc.edu.cn/debian-security buster/updates main
deb-src http://mirrors.ustc.edu.cn/debian-security buster/updates main
# buster-updates, previously known as 'volatile'
deb http://mirrors.ustc.edu.cn/debian/ buster-updates main
deb-src http://mirrors.ustc.edu.cn/debian/ buster-updates main
deb http://mirrors.ustc.edu.cn/debian/ buster-backports main non-free contrib
deb-src http://mirrors.ustc.edu.cn/debian/ buster-backports main non-free contrib
EOF
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:/#