什么是 Dockerfile?
Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]# pwd
/root/docker_image_build
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]# ls
Dockerfile
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]# cat Dockerfile
FROM ubuntu:16.04
RUN echo '这是一个本地构建的nginx镜像' > /tmp/rob0625.txt
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4f380adfc10f 2 days ago 133MB
ubuntu 16.04 f6f49faac5cf 3 months ago 132MB
hello-world latest d1165f221234 3 months ago 13.3kB
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]# docker build -t myubuntu:v1 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM ubuntu:16.04
---> f6f49faac5cf
Step 2/2 : RUN echo '这是一个本地构建的nginx镜像' > /tmp/rob0625.txt
---> Running in 50f4de7f3e2d
Removing intermediate container 50f4de7f3e2d
---> 09f033b88faa
Successfully built 09f033b88faa
Successfully tagged myubuntu:v1
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu v1 09f033b88faa 7 seconds ago 132MB
nginx latest 4f380adfc10f 2 days ago 133MB
ubuntu 16.04 f6f49faac5cf 3 months ago 132MB
hello-world latest d1165f221234 3 months ago 13.3kB
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]# docker run -it myubuntu:v1 /bin/bash
root@64be36769af2:/#
root@64be36769af2:/#
root@64be36769af2:/#
root@64be36769af2:/#
root@64be36769af2:/# cat /tmp/rob0625.txt
这是一个本地构建的nginx镜像
root@64be36769af2:/#
root@64be36769af2:/#
root@64be36769af2:/#
root@64be36769af2:/#
root@64be36769af2:/# exit
[root@jdddba docker_image_build]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f1f296c38e6 ubuntu:16.04 "/bin/bash" 2 months ago Up 2 months keen_snyder
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
[root@jdddba docker_image_build]#
Dockerfile demo2:
背景是:在MAC上安装完 Docker desktop 是给的小练习:
ZBMac-KT2HFQD4N:~ chengpengxing$ docker run --name repo alpine/git clone https://github.com/docker/getting-started.git
Unable to find image 'alpine/git:latest' locally
latest: Pulling from alpine/git
f97344484467: Pull complete
ba95c4335eea: Pull complete
ab8690bd2e84: Pull complete
Digest: sha256:760aaf0d59c93f87572ec40dee1efd10a7ea13a78dff1f59a904e908449329ae
Status: Downloaded newer image for alpine/git:latest
Cloning into 'getting-started'...
ZBMac-KT2HFQD4N:~ chengpengxing$
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
79bc4ae2bbf0 alpine/git "git clone https://g…" 3 minutes ago Exited (0) 3 minutes ago repo
➜ ~
ZBMac-KT2HFQD4N:~ chengpengxing$ docker cp repo:/git/getting-started/ . // 拷贝镜像里的内容
分析:docker run --name repo alpine/git clone https://github.com/docker/getting-started.git 命令的格式
--name repo //指定容器的名字
alpine/git //指定容器的镜像,本地如果有的话,就用本地的,没有的话就从线上拉取
clone https://github.com/docker/getting-started.git // 是生成上述镜像的 dockerfile 中 ENTRYPOINT 的参数
生成镜像 alpine/git 的 Dockerfile:
FROM alpine
LABEL maintainer Bill Wang <ozbillwang@gmail.com>
RUN apk --update add git less openssh && \
rm -rf /var/lib/apt/lists/* && \
rm /var/cache/apk/*
VOLUME /git
WORKDIR /git
ENTRYPOINT ["git"]
CMD ["--help"]
CentOs 物理机可以运行 Ubuntu 的容器 ,另外Linux X86架构不可以运行 ARM 架构的容器,但是 MAC 可以运行2种架构
[root@dbs-test-rob ~]# docker run -it ubuntu:14.04 /bin/bash
root@0ad634ee67f6:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.6 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.6 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@0ad634ee67f6:/#
root@0ad634ee67f6:/#
root@0ad634ee67f6:/#
root@0ad634ee67f6:/# exit
exit
[root@dbs-test-rob ~]#
[root@dbs-test-rob ~]#
[root@dbs-test-rob ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
[root@dbs-test-rob ~]#
[root@dbs-test-rob ~]#
Linux X86架构不可以运行 ARM 架构的容器:
[root@dbs-test-rob ~]# docker run -it hub-vpc.jdcloud.com/baseimage/jdk8-tomcat8-openeuler22-arm64:1.0.0 /bin/bash
Unable to find image 'hub-vpc.jdcloud.com/baseimage/jdk8-tomcat8-openeuler22-arm64:1.0.0' locally
1.0.0: Pulling from baseimage/jdk8-tomcat8-openeuler22-arm64
8771742c1353: Pull complete
bac4447ba7d9: Pull complete
f492fb35d743: Pull complete
b1cc3d3f092a: Pull complete
a2015a42151f: Pull complete
64aff98117fd: Pull complete
e03892fa5eda: Pull complete
46b96521725b: Pull complete
cb00aff8c075: Pull complete
Digest: sha256:dfb1cb331245110a405e4520b71e2c179f164f07235fa1d822c35e033d2f6824
Status: Downloaded newer image for hub-vpc.jdcloud.com/baseimage/jdk8-tomcat8-openeuler22-arm64:1.0.0
standard_init_linux.go:228: exec user process caused: exec format error
[root@dbs-test-rob ~]#
[root@dbs-test-rob ~]#
[root@dbs-test-rob ~]# uname -m
x86_64
[root@dbs-test-rob ~]#
Q:那为什么iMac操作系统既可以运行arm64v8的镜像 ,又可以运行amd64的镜像?
A:iMac 操作系统可以运行 arm64v8 和 amd64 镜像是因为它是基于苹果公司的自家处理器架构,称为苹果公司架构(Apple Silicon)。
苹果公司架构支持两种指令集:arm64 和 x86-64,因此 iMac 操作系统可以运行基于这两种指令集的镜像。
不过,iMac 操作系统也可以通过使用虚拟机或者其他方式来运行其他架构的操作系统,从而能够运行其他架构的 Docker 镜像。
docker 镜像导入导出有两种方法:
一种是使用 save 和 load 命令
docker save ubuntu:load>/root/ubuntu.tar
docker load<ubuntu.tar
一种是使用 export 和 import 命令
docker export 98ca36> ubuntu.tar
cat ubuntu.tar | sudo docker import - ubuntu:import // 把快照文件导入为镜像
两者的区别:
export 和 import 导出的是一个容器的快照, 不是镜像本身, 也就是说没有 layer。
你的 dockerfile 里的 workdir, entrypoint 之类的所有东西都会丢失,commit 过的话也会丢失。
快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也更大。
docker save 保存的是镜像(image),docker export 保存的是容器(container);
docker load 用来载入镜像包,docker import 用来载入容器包,但两者都会恢复为镜像;
docker load 不能对载入的镜像重命名,而 docker import 可以为镜像指定新名称。
另外 docker commit: 从容器创建一个新的镜像。
将容器a404c6c174a2 保存为新的镜像,并添加提交人信息和说明信息。
runoob@runoob:~$ docker commit -a "runoob.com" -m "my apache" a404c6c174a2 mymysql:v1
sha256:37af1236adef1544e8886be23010b66577647a40bc02c0885a6600b33ee28057
runoob@runoob:~$ docker images mymysql:v1
REPOSITORY TAG IMAGE ID CREATED SIZE
mymysql v1 37af1236adef 15 seconds ago 329 MB
docker pull ubuntu:latest@sha256:ad18cfdb19dac67bf0072dacea661a817330e5c955d081f4d09914e743ae5d4a