1、什么情况下需要自己构建镜像?
a、找不到现成的镜像,比如自己开发的应用程序。
b、需要在镜像中加入特定的功能,比如官方镜像几乎都不提供 ssh。
2、docker中构建镜像的方法?
a、docker commit 命令
b、Dockerfile 构建文件
3、docker commit构建镜像的过程?
a、运行容器
b、修改容器
c、将容器保存为新的镜像
4、如何运行容器?
docker run -it image-name eg:ubuntu、centos
-it 参数的作用是以交互模式进入容器,并打开终端。412b30588f4a 是容器的内部 ID
5、docker如何用命令构建镜像?
docker commit old-image-name new-image-name
6、为什么不建议用docker commit命令构建镜像?
a、这是一种手工创建镜像的方式,容易出错,效率低且可重复性弱。比如要在 debian base 镜像中也加入 vi,还得重复前面的所有步骤。
b、更重要的:使用者并不知道镜像是如何创建出来的,里面是否有恶意程序。也就是说无法对镜像进行审计,存在安全隐患。
ps:Dockerfile(推荐方法)构建镜像,底层也 docker commit 一层一层构建新镜像的
7、为啥启动一个centos的容器,在容器内装了nginx,然后生成一个新的镜像,再用这个镜像启动一个容器,按理说这个容器不就直接可以访问nginx了吗,但是nginx的服务都没有启动,我登录进入这个容器,手动/etc/rc.d/init.d/nginx start 才启动?
nginx会不会自动启动在于有没有诸如systemd的服务管理器自动启动它。在常规linux中systemd会做这件事,但容器中可能没有systemd,或者有systemd但没有针对nginx的启动配置,所以一般在容器中会通过ENTRYPOINT来启动容器中的服务,比如nginx
8、使用dockerfile创建镜像有什么注意事项?
Dockerfile 中的 ADD、COPY 等命令可以将 build context 中的文件添加到镜像。此例中,build context 为当前目录 /root,该目录下的所有文件和子目录都会被发送给 Docker daemon。
所以,使用 build context 就得小心了,不要将多余文件放到 build context,特别不要把 /、/usr 作为 build context,否则构建过程会相当缓慢甚至失败。build context中只放dockerfile中会用到的文件和目录
9、使用dockerfile创建镜像的过程?
https://mp.weixin.qq.com/s/zhQlw2_GWs_DvyXFoDCRog
root@richardo-docker01:~# vim Dockerfile
root@richardo-docker01:~#
root@richardo-docker01:~#
root@richardo-docker01:~# docker build -t centos-with-vim-dockerfile .
Sending build context to Docker daemon 11.26kB
Step 1/2 : FROM centos
---> 0d120b6ccaa8
Step 2/2 : RUN yum makecache && yum install -y vim
---> Running in 1866d7739877
CentOS-8 - AppStream 929 kB/s | 5.8 MB 00:06
CentOS-8 - Base 2.5 MB/s | 2.2 MB 00:00
CentOS-8 - Extras 11 kB/s | 8.6 kB 00:00
Last metadata expiration check: 0:00:01 ago on Fri Nov 13 08:54:24 2020.
Metadata cache created.
Last metadata expiration check: 0:00:02 ago on Fri Nov 13 08:54:24 2020.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k
vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M
vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k
which x86_64 2.21-12.el8 BaseOS 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 31 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 236 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 631 kB/s | 48 kB 00:00
(3/5): which-2.21-12.el8.x86_64.rpm 935 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 1.0 MB/s | 1.4 MB 00:01
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 1.4 MB/s | 6.3 MB 00:04
--------------------------------------------------------------------------------
Total 1.4 MB/s | 7.8 MB 00:05
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream 73 kB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : which-2.21-12.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/5
Verifying : which-2.21-12.el8.x86_64 5/5
Installed:
gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-13.el8.x86_64
vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch
which-2.21-12.el8.x86_64
Complete!
Removing intermediate container 1866d7739877
---> e99684eeca8a
Successfully built e99684eeca8a
Successfully tagged centos-with-vim-dockerfile:latest
root@richardo-docker01:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-with-vim-dockerfile latest e99684eeca8a 37 seconds ago 272MB
centos-with-vim latest 0ec55bc57a3d 47 minutes ago 272MB
httpd latest 3dd970e6b110 4 weeks ago 138MB
centos latest 0d120b6ccaa8 3 months ago 215MB
hello-world latest bf756fb1ae65 10 months ago 13.3kB