FROM指定base镜像,本地不存在就会从远程仓库下载(有网就行)
[root@server1 docker]# pwd #在干净的目录下创建dockerfile镜像文件
/mnt/docker #以u免运行时,有影响
[root@server1 docker]# vim dockerfile
[root@server1 docker]# cat dockerfile
FROM busybox
RUN echo testfile >file1
RUN echo testfile >file2
注意:dockerfile中写命令时,不能有多余的空格
创建镜像:
[root@server1 docker]# docker build -t busyboxecho . #根据文件内容,创建镜像。
Sending build context to Docker daemon 2.048kB #可以看到,在这里是创建了三层镜像
Step 1/3 : FROM busybox 第一层,本身具有的
---> 020584afccce
Step 2/3 : RUN echo testfile >file1 第二层
---> Running in 0de902502270
Removing intermediate container 0de902502270
---> 244f864d70e3
Step 3/3 : RUN echo testfile >file2 第三层
---> Running in 864ca57f5451
Removing intermediate container 864ca57f5451
---> 2887c30be242
Successfully built 2887c30be242
Successfully tagged busyboxecho:latest
查看本地开启的镜像
[root@server1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 020584afccce 3 days ago 1.22MB
nginx latest 540a289bab6c 11 days ago 126MB
game2048 latest 19299002fdbe 2 years ago 55.5MB
使用当前目录下的dockerfile创建镜象(path为‘ . ’):
busybox:镜像名; v1版本号
[root@server1 docker]# docker build -t busybox1.0:v1 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
---> 020584afccce
Step 2/3 : RUN echo testfile >file1
---> Running in b59d6b7eddca
Removing intermediate container b59d6b7eddca
---> 35226fda4810
Step 3/3 : RUN echo testfile >file2
---> Running in 8e6aaa675e2a
Removing intermediate container 8e6aaa675e2a
---> 5b551bd01d32
Successfully built 5b551bd01d32
Successfully tagged busybox1.0:v1
[root@server1 docker]# docker images #查看本地所有开着的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox1.0 v1 5b551bd01d32 7 seconds ago 1.22MB
busybox latest 020584afccce 3 days ago 1.22MB
nginx latest 540a289bab6c 11 days ago 126MB
game2048 latest 19299002fdbe 2 years ago 55.5MB
docker history--查看镜像的构建过程
[root@server1 docker]# docker history busybox1.0:v1
IMAGE CREATED CREATED BY SIZE COMMENT
5b551bd01d32 2 minutes ago /bin/sh -c echo testfile >file2 9B
35226fda4810 2 minutes ago /bin/sh -c echo testfile >file1 9B
020584afccce 3 days ago /bin/sh -c #(nop) CMD ["sh"] 0B
3 days ago /bin/sh -c #(nop) ADD file:1141b81e5149cc37c… 1.22MB
[root@server1 docker]# docker run -it busybox1.0:v1 #运行镜像,此时运行后产生的容器的名字是随机的,在这里是mystifying_darwin
#如果要自己取名字,采用名令 docker run -it --name busybox1.0:v1
/ # ls
bin etc file2 proc sys usr
dev file1 home root tmp var
/ # exit
显示所有的本地正在运行的镜像 docker ps
root@server1 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
显示所有的本地镜像 docker ps -a
[root@server1 docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7cef13f36d74 busybox1.0:v1 "sh" 36 seconds ago Exited (0) 13 seconds ago mystifying_darwin
9044a6a4bd60 game2048 "/bin/sh -c 'sed -i …" 18 hours ago Exited (137) 17 hours ago
创建镜像
[root@server1 docker]# cat dockerfile
FROM busybox
RUN echo testfile >file1
RUN echo testfile >file2
RUN echo testfile >file3
[root@server1 docker]# docker build -t busybox1.0:v2 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM busybox
---> 020584afccce
Step 2/4 : RUN echo testfile >file1
---> Using cache
---> 35226fda4810
Step 3/4 : RUN echo testfile >file2
---> Using cache
---> 5b551bd01d32
Step 4/4 : RUN echo testfile >file3
---> Running in c00f74a8c2cb
Removing intermediate container c00f74a8c2cb
---> 7b1844c4fa77
Successfully built 7b1844c4fa77
Successfully tagged busybox1.0:v2
[root@server1 docker]# docker images 查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox1.0 v2 7b1844c4fa77 34 seconds ago 1.22MB
busybox1.0 v1 5b551bd01d32 6 minutes ago 1.22MB
busybox latest 020584afccce 3 days ago 1.22MB
nginx latest 540a289bab6c 11 days ago 126MB
game2048 latest 19299002fdbe 2 years ago 55.5MB
进入镜像
[root@server1 docker]# docker run -it busybox1.0:v2
/ # ls
bin etc file2 home root tmp var
dev file1 file3 proc sys usr
/ # echo 333 >yyz
/ # ls
bin etc file2 home root tmp var
dev file1 file3 proc sys usr yyz
/ # echo yy >file1
/ # ls
bin etc file2 home root tmp var
dev file1 file3 proc sys usr yyz
copy的用法:
删除dockerfile创建的镜像
[root@server1 docker]# docker rmi -f 7b1844c4fa77
Untagged: busybox1.0:v2
Deleted: sha256:7b1844c4fa779a3054263b9441d8009acb969aa416f145fb89281dbb94de417d
[root@server1 docker]# cat dockerfile
FROM busybox
COPY testfile /tmp
COPY ["hello","/tmp"]
[root@server1 docker]# vim testfile
[root@server1 docker]# vim hello
[root@server1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 020584afccce 3 days ago 1.22MB
nginx latest 540a289bab6c 11 days ago 126MB
game2048 latest 19299002fdbe 2 years ago 55.5MB
[root@server1 docker]# docker build -t test:v1 .
Sending build context to Docker daemon 4.096kB
Step 1/3 : FROM busybox
---> 020584afccce
Step 2/3 : COPY testfile /tmp
---> 936f1a729bd5
Step 3/3 : COPY ["hello","/tmp"]
---> 4ce4f04be438
Successfully built 4ce4f04be438
Successfully tagged test:v1
[root@server1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test v1 4ce4f04be438 5 seconds ago 1.22MB
busybox latest 020584afccce 3 days ago 1.22MB
nginx latest 540a289bab6c 11 days ago 126MB
game2048 latest 19299002fdbe 2 years ago 55.5MB
[root@server1 docker]# docker history test:v1
IMAGE CREATED CREATED BY SIZE COMMENT
4ce4f04be438 22 seconds ago /bin/sh -c #(nop) COPY file:1b0cdc05878fe9c3… 9B
936f1a729bd5 22 seconds ago /bin/sh -c #(nop) COPY file:8994bfe58384692d… 5B
020584afccce 3 days ago /bin/sh -c #(nop) CMD ["sh"] 0B
[root@server1 docker]# docker run -it --name vm1 test:v1
/ # ls
bin dev etc home proc root sys tmp usr var
/ # cd /tmp/
/tmp # ls
hello testfile
/tmp # cat hello
hello~~~
ADD:用法和COPY类似,不同的是SRC可以是归档压缩文件,文件会自动解压到dest,也可以下载URL并且拷贝到镜像
eg:ADD html。tar /var/www
ADD http://ip/html.tar /var/www
[root@server1 docker]# ls
dockerfile hello redis-5.0.3.tar.gz testfile
[root@server1 docker]# vim dockerfile
[root@server1 docker]# cat dockerfile
FROM busybox
COPY testfile /tmp
ADD redis-5.0.3.tar.gz /tmp
[root@server1 docker]# docker build -t test:v1 .
Sending build context to Docker daemon 1.965MB
Step 1/3 : FROM busybox
---> 020584afccce
Step 2/3 : COPY testfile /tmp
---> 050d6ad30a3e
Step 3/3 : ADD redis-5.0.3.tar.gz /tmp
---> e24cd31530c4
Successfully built e24cd31530c4
Successfully tagged test:v1
[root@server1 docker]# docker run -it --name vm1 test:v1
docker: Error response from daemon: Conflict. The container name "/vm1" is already in use by container "6b0bf10240b41c8ef3db8ff572ac719b72149b3faaf5019c35198eab65751995". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
[root@server1 docker]# docker run -it --name yyz test:v1
/ # ls
bin dev etc home proc root sys tmp usr var
/ # cd /tmp
/tmp # ls
redis-5.0.3 testfile
编写Dockerfile构建镜像
(1)删除前面构建的镜像和容器
(2)从真机给server1传送一个镜像
[root@server1 ~]# ls
docker包 game2048.tar python.tar rhel7.tar
(3)导入镜像
[root@server1 ~]# docker load -i rhel7.tar
e1f5733f050b: Loading layer 147.1MB/147.1MB
[root@server1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 020584afccce 3 days ago 1.22MB
nginx latest 540a289bab6c 11 days ago 126MB
game2048 latest 19299002fdbe 2 years ago 55.5MB
rhel7 latest 0a3eb3fde7fd 5 years ago 140MB
(4)编写Dockerfile
[root@server1 docker]# cat dockerfile
FROM rhel7 # 源镜像是rhel7,最好将名为rhel7的镜像放在本地
EXPOSE 80 # 定义端口为80
MAINTAINER [email protected] #作者
COPY yum.repo /etc/yum.reos.d #将本地的yum源文件复制到容器中的/etc/yum.repo.d/目录下
RUN rpmdb --rebuilddb
# rpmdb 命令用于初始化和重建rpm数据库
# --rebuilddb:从已安装的包头文件,反向重建RPM数据库
RUN yum install -y gcc pcre-devel zlib-devel make
ADD nginx-1.17.4.tar.gz /mnt
WORKDIR /mnt/nginx-1.17.4
RUN ./configure --prefix=/usr/local/nginx
RUN make
RUN make install
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
(5)编写yum.repo(在当前目录下,也就是放置Dockerfile的目录中)
[root@server1 docker]# cat yum.repo
[rhel7.5]
baseurl=http://172.25.28.250/isos
gpgcheck=0
name=rhel7.5
Successfully built 95aa91dc32bc
Successfully tagged rhel7:v5
(6)封装镜像,并测试能否正常使用
[root@server1 docker]# docker build -t rhel7:v1 .
如果出现以下错误,则在dockerfile文件里,执行安装make的命令
/bin/sh: make: command not found
The command '/bin/sh -c make' returned a non-zero code: 127
RUN yum install -y gcc pcre-devel zlib-devel make
Successfully built 95aa91dc32bc
Successfully tagged rhel7:v5
代表封装成功
(7)基于刚才封装的镜像运行一个容器
[root@server1 docker]# docker run -d --name vm rhel7:v5
e7416e35fadd416dc49c2bbda42c51919e6cc2ccf8279bbb0eba36006eed4d98
docker inspect vm 查看容器IP地址