# DockerFile的使用
# DockerFile介绍
dockerfile是用来构建docker镜像的文件!
构建步骤:
1、编写一个dockerfile文件
2、docker build构建成为一个镜像
3、docker run 运行镜像
4、docker push 发布镜像(Docker Hub、阿里云镜像仓库)
# DockerFile构建过程
基础知识:
1、每个保留关键字都必须是大写字母
2、执行从上到下顺序执行
3、#表示注释
4、每个指令都会创建提交一个新的镜像层,并提交
# DockerFile的指令
FROM # 指定基础镜像,比如node:8
MAINTAINER # 镜像作者,姓名+邮箱
RUN # 构建镜像时运行的shell命令
ADD # 添加文件到镜像中,和COPY的区别是可以自动解压
WORKDIR # 设置工作目录
VOLUME # 指定挂载点到宿主机目录
EXPOSE # 生命容器使用的网络端口
CMD # 启动容器时运行的shell命令,只有最后一个会生效,运行容器的指定命令会替代CMD
ENTRYPOINT # 启动容器时运行的shell命令,运行容器时指定的命令会追加
ONBUILD # 当构建一个被继承DockerFile 时运行
COPY # 类似ADD,将文件拷贝到镜像环境
ENV # 构建的时候设置环境变量
# 创建自定义DockerFile
# 实战:添加vim命令
# 1 编写Dockerfile文件
[root@VM-20-7-centos dockerfile]# cat mydockerfile
FROM centos
MAINTAINER huyadish<gh123456@163.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "---end---"
CMD /bin/bash
# 2 通过这个文件构建镜像
[root@VM-20-7-centos dockerfile]# docker build -f mydockerfile -t mycentos:1.0 .
# 3 测试运行
[root@VM-20-7-centos dockerfile]# docker run -it mycentos:1.0 /bin/bash
# 4 查看镜像是如何生成的
docker history 镜像名
遇到的坑:yum 安装失败,无法解析mirrorlist.centos.org
=> ERROR [3/4] RUN yum -y install vim 0.8s
------
> [3/4] RUN yum -y install vim:
0.716 CentOS Linux 8 - AppStream 0.0 B/s | 0 B 00:00
0.717 Errors during downloading metadata for repository 'appstream':
0.717 - Curl error (6): Couldn't resolve host name for http://mirrorlist.centos.org/?release=8&arch=x86_64&repo=AppStream&infra=container [Could not resolve host: mirrorlist.centos.org]
注意:centos7已经在2024年6月30日停止维护,相关的镜像网站也停了
在dockerfile中添加换源信息
FROM centos
MAINTAINER huyadish<gh123456@163.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 换为阿里云的镜像源
RUN cd /etc/yum.repos.d/
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g' /etc/yum.repos.d/CentOS-*
# 安装 vim net-tools
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD /bin/bash
构建运行
# 构建
[root@VM-20-7-centos dockerfile]# docker build -f Dockerfile1 -t test .
[+] Building 16.0s (11/11) FINISHED
# 运行并进入
[root@VM-20-7-centos dockerfile]# docker run -it test /bin/bash
[root@d85874588b4a local]#
# 测试
[root@d85874588b4a local]# vim --help
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep 22 2021 11:10:49)
[root@d85874588b4a local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 实战:CMD 和 ENTRYPOINT的区别
CMD # 启动容器时运行的shell命令,只有最后一个会生效,运行容器的指定命令会替代CMD
ENTRYPOINT # 启动容器时运行的shell命令,运行容器时指定的命令会追加
测试CMD
# 创建dockerfile
[root@VM-20-7-centos dockerfile]# vim mydockerfile-test
FROM centos
CMD ["ls","-a"]
# 构建镜像
[root@VM-20-7-centos dockerfile]# docker build -f mydockerfile-test -t cmdtest .
# 运行镜像
[root@VM-20-7-centos dockerfile]# docker run -it cmdtest
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
# 追加一个命令 -l 期望 ls -al 然而报错
[root@VM-20-7-centos dockerfile]# docker run cmdtest -l
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
# cmd的情况下 -l 替换了 CMD ["ls","-a"] 命令,然而 -l 并不是个命令,所以报错
测试ENTRYPOINT
# 创建dockerfile
[root@VM-20-7-centos dockerfile]# vim mydockerfile-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
# 构建镜像
[root@VM-20-7-centos dockerfile]# docker build -f mydockerfile-entrypoint -t myentrypoint .
# 运行镜像
[root@VM-20-7-centos dockerfile]# docker run myentrypoint
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 添加参数运行 -l 相当于运行 ls -al
[root@VM-20-7-centos dockerfile]# docker run myentrypoint -l
total 56
drwxr-xr-x 1 root root 4096 Aug 19 13:30 .
drwxr-xr-x 1 root root 4096 Aug 19 13:30 ..
-rwxr-xr-x 1 root root 0 Aug 19 13:30 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 Aug 19 13:30 dev
drwxr-xr-x 1 root root 4096 Aug 19 13:30 etc
drwxr-xr-x 2 root root 4096 Nov 3 2020 home
lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------ 2 root root 4096 Sep 15 2021 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 2020 media
drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
dr-xr-xr-x 125 root root 0 Aug 19 13:30 proc
dr-xr-x--- 2 root root 4096 Sep 15 2021 root
drwxr-xr-x 11 root root 4096 Sep 15 2021 run
lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
dr-xr-xr-x 13 root root 0 Aug 17 10:04 sys
drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
drwxr-xr-x 20 root root 4096 Sep 15 2021 var
# 实战:Tomcat镜像
1、准备镜像文件 tomcat 压缩包,jdk压缩包
2、编写dockerfile文件,官方推荐命名Dockerfile
FROM centos
MAINTAINER huyadish<huyadish@163.com>
COPY README.md /usr/local/README.md
ADD jdk-8u161-linux-x64.tar.gz /usr/local
ADD apache-tomcat-8.5.55.tar.gz /usr/local
# RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_161
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.55
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.55
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-8.5.55/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.55/bin/logs/catalin
a.out
3、构建镜像
[root@VM-20-7-centos dockerfile]# docker build -t mytomcat .
4、运行镜像
# 运行并挂载数据卷
[root@VM-20-7-centos dockerfile]# docker run -d -p 9090:8080 --name huyadish-tomcat -v /home/huyadish/build/tomcat/test/:/usr/local/apache-tomcat-8.5.55/webapps/test -v /home/huyadish/build/tomcat/logs/:/usr/local/apache-tomcat-8.5.55/logs mytomcat
31a90f439ab5aad6c99e37cb4803b54bb4e31608f0cf818b99e2c957570781fe
# 进入容器
[root@VM-20-7-centos dockerfile]# docker exec -it 31a90f4 /bin/bash
# 可以看到已经解压的 tomcat 和 jdk
[root@31a90f439ab5 local]# ls
README.md apache-tomcat-8.5.55 bin etc games include jdk1.8.0_161 lib lib64 libexec sbin share src
# 工作目录也切换到了 /usr/local
[root@31a90f439ab5 local]# pwd
/usr/local
[root@31a90f439ab5 local]# cd apache-tomcat-8.5.55/
[root@31a90f439ab5 apache-tomcat-8.5.55]# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs webapps
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib temp work
# 此时通过外网也可以访问
5、发布项目(由于做了卷挂载,我们直接在本地编写项目就可以)
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>test</display-name>
</web-app>
- a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello,huyadish</title>
</head>
<body>
hello,world!<br>
<% System.out.println("-------my docker tomcat-------");%>
</body>
</html>
# 发布镜像
# 发布到docker hub
在https://hub.docker.com/注册自己的账号
# 首先登录docker hub
[root@VM-20-7-centos ~]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
[root@VM-20-7-centos ~]#
[root@VM-20-7-centos ~]# docker login -u olgstag
Password: # 输入密码
Login Succeeded
# 直接push会失败
[root@VM-20-7-centos ~]# docker push mytomcat
Using default tag: latest
The push refers to repository [docker.io/library/mytomcat]
5f70bf18a086: Preparing
8d76854d13f0: Preparing
60424f91fa9b: Preparing
87a7a415f2e5: Preparing
74ddd0ec08fa: Preparing
denied: requested access to the resource is denied
# 使用docker tag 为已有镜像创建一个标签, 并添加远程仓库名
[root@VM-20-7-centos ~]# docker tag test olgstag/test:1.0
# 重新push 网络问题 连接失败
[root@VM-20-7-centos ~]# docker push olgstag/test:1.0
# 发布到阿里云docker镜像服务
1、登录阿里云
2、找到容器镜像服务
3、创建命名空间
4、创建仓库
# 登录阿里云远程仓库
[root@VM-20-7-centos ~]# docker login --username=oldstag registry.cn-hangzhou.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
# 为本地镜像添加tag
[root@VM-20-7-centos ~]# docker tag test registry.cn-hangzhou.aliyuncs.com/oldstag/docker-test:1.0
# 推送到阿里云容器仓库
[root@VM-20-7-centos ~]# docker push registry.cn-hangzhou.aliyuncs.com/oldstag/docker-test:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/oldstag/docker-test]
02102190b029: Pushed
fd99157a0f6b: Pushed
89e8e6aeee7a: Pushed
3f25450bae38: Pushed
5f70bf18a086: Pushed
74ddd0ec08fa: Pushed
1.0: digest: sha256:c5c513ed607b3cd5a47c97979c41a82db99d16084e38cfcdcb2cbad576af91fd size: 1780
# 推送成功
使用阿里云的镜像服务需要配置一下 阿里云的镜像加速
# 小结
← ♥Docker♥ Docker常用命令 →