v2admin@ubuntu:~$ docker pull hello-world v2admin@ubuntu:~$ docker images hello-world REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 5 months ago 1.84kB
hello-world的Dockerfile只有三条(Dockerfile 定义了如何构建 Docker 镜像,是镜像的描述文件。)mysql
FROM scratch //从 0 开始构建。 COPY hello / // 复制文件“hello”到镜像的根目录。 CMD ["/hello"] // 容器启动时,执行 /hello
一般指linux的各大发行版本的Doctor镜像linux
FROM scratch //从0构建 ADD centos-7-docker.tar.xz / //用户空间,解压后,就生成/bin、/dev等目录 CMD [“/bin/bash”]
Docker支持扩展示有镜像,生成新的镜像,大多数镜像都是在base镜像基础上构建出来的。
示例:sql
FROM ubuntu RUN apt install apache2 RUN apt install mysql CMD ["/bin/bash"]
两种方法:docker
docker commit
1)交互模式运行容器 docker run -it image-name
2)修改容器(安装部署应用等)
3)将容器保存为新的镜像 docker commit
示例:apache
v2admin@ubuntu:~$ docker run -it centos //-it参数能够以交互模式进入容器,这样就能够对容器进行操做了 [root@f3439e5b0092 /]# yum install httpd //安装httpd服务 //exit退出 v2admin@ubuntu:~$ docker commit -m="install httpd" f3439e5b0092 centos-httpd //生成新的镜像 sha256:105e2960a30af973884e762f701fad52a608e9ce96246212ff6518ed220f5509 v2admin@ubuntu:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-httpd latest 105e2960a30a 21 seconds ago 339MB //新生成的镜像 ubuntu latest 4c108a37151f 8 days ago 64.2MB httpd latest e77c77f17b46 2 weeks ago 140MB centos latest 9f38484d220f 3 months ago 202MB
Dcokerfile:文本文件,记录镜像的构建步骤
编写一个Dockerfileubuntu
FROM ubuntu RUN apt update -y && apt install apache2 -y v2admin@ubuntu:~$ docker build -t ubuntu-httpd . //构建镜像 v2admin@ubuntu:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-httpd latest 315b71d16eb4 About an hour ago 187MB centos-httpd latest 105e2960a30a 4 hours ago 339MB ubuntu latest 4c108a37151f 8 days ago 64.2MB
新生成的镜像与base镜像的差别centos
v2admin@ubuntu:~$ docker history ubuntu //查看镜像的构建历史 IMAGE CREATED CREATED BY SIZE COMMENT 4c108a37151f 8 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 8 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 8 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B <missing> 8 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 987kB <missing> 8 days ago /bin/sh -c #(nop) ADD file:4e6b5d9ca371eb881… 63.2MB v2admin@ubuntu:~$ docker history ubuntu-httpd IMAGE CREATED CREATED BY SIZE COMMENT 315b71d16eb4 About an hour ago /bin/sh -c apt update -y && apt install apac… 123MB // 惟一的一条差别,在base镜像上多了一层 4c108a37151f 8 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 8 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 8 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B <missing> 8 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 987kB <missing> 8 days ago /bin/sh -c #(nop) ADD file:4e6b5d9ca371eb881… 63.2MB