Day180
命令 docker build -f 文件路径 -t 镜像名:[tag] .
[root@izbp1h9ynqgizuhp85e2xkz dockerfile]# docker build -f myDockerFile-centos -t mycentos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 300e315adb2f
Step 2/10 : MAINTAINER achang995931576@qq.com
---> Using cache
---> 82edb0ac69c2
Step 3/10 : ENV MYPATH /usr/local
---> Using cache
---> e2325f72ff50
Step 4/10 : WORKDIR $MYPATH
---> Using cache
---> c3c6e1f65466
Step 5/10 : RUN yum -y install vim
---> Using cache
---> f3c0242d3727
Step 6/10 : RUN yum -y install net-tools
---> Running in 1d695361c61b
Last metadata expiration check: 0:04:02 ago on Sat Jan 30 11:45:24 2021.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 1.5 MB/s | 322 kB 00:00
Total 444 kB/s | 322 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
Removing intermediate container 1d695361c61b
---> 55b1ed9cadea
Step 7/10 : EXPOSE 5299
---> Running in b0dcf59f3190
Removing intermediate container b0dcf59f3190
---> d8c0d393ea76
Step 8/10 : CMD echo $MYPATH
---> Running in 208ebfa2d3ba
Removing intermediate container 208ebfa2d3ba
---> 57ac789f1e5b
Step 9/10 : CMD echo "---END---"
---> Running in f173e33f2b6c
Removing intermediate container f173e33f2b6c
---> 13b13c7594ba
Step 10/10 : CMD /bin/bash
---> Running in 85751b8ad3f4
Removing intermediate container 85751b8ad3f4
---> 0513d130fa1b
Successfully built 0513d130fa1b
Successfully tagged mycentos:0.1
测试运行
已经有vim
指令
有了ifconfig
命令
列出本地进行的变更历史
docker history 镜像 ID
CMD 和 ENTRYPOINT 区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代。
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
测试 cmd
编写 dockerfile 文件
$ vim dockerfile-test-cmd
dockerfile 内容
FROM centos
CMD ["ls","-a"]
构建镜像
$ docker build -f dockerfile-test-cmd -t cmd-test:0.1 .
运行镜像
$ docker run cmd-test:0.1
.
..
.dockerenv
bin
dev
想追加一个命令 -l 成为 ls -al
[root@izbp1h9ynqgizuhp85e2xkz dockerfile]# docker run cmd-test -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
cmd 的情况下 -l 替换了 CMD 成为了 CMD["ls","-l"]。 -l -->不是命令所以报错
需要写以下,通过 ls -al 完整的命令 替换上面的 ls -a
[root@izbp1h9ynqgizuhp85e2xkz dockerfile]# docker run cmd-test ls -al
发现CMD
命令是替换之前的命令,所以就需要写原生命令
测试 ENTRYPOINT
编写 dockerfile 文件
$ vim dockerfile-test-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
[root@izbp1h9ynqgizuhp85e2xkz dockerfile]# docker run 2c09d7cc807f
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
我们的命令,是直接拼接在 ENTRYPOINT 命令后面的
[root@izbp1h9ynqgizuhp85e2xkz dockerfile]# docker run 2c09d7cc807f -l
total 56
drwxr-xr-x 1 root root 4096 Jan 30 12:41 .
drwxr-xr-x 1 root root 4096 Jan 30 12:41 ..
-rwxr-xr-x 1 root root 0 Jan 30 12:41 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin -> usr/bin
drwxr-xr-x 5 root root 340 Jan 30 12:41 dev
drwxr-xr-x 1 root root 4096 Jan 30 12:41 etc
drwxr-xr-x 2 root root 4096 Nov 3 15:22 home
lrwxrwxrwx 1 root root 7 Nov 3 15:22 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64
drwx------ 2 root root 4096 Dec 4 17:37 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 15:22 media
drwxr-xr-x 2 root root 4096 Nov 3 15:22 mnt
drwxr-xr-x 2 root root 4096 Nov 3 15:22 opt
dr-xr-xr-x 123 root root 0 Jan 30 12:41 proc
dr-xr-x--- 2 root root 4096 Dec 4 17:37 root
drwxr-xr-x 11 root root 4096 Dec 4 17:37 run
lrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 15:22 srv
dr-xr-xr-x 13 root root 0 Jan 30 12:41 sys
drwxrwxrwt 7 root root 4096 Dec 4 17:37 tmp
drwxr-xr-x 12 root root 4096 Dec 4 17:37 usr
drwxr-xr-x 20 root root 4096 Dec 4 17:37 var
发现了ENTRYPOINT
命令可以在命令后面叠加
[](()实战:Tomcat 镜像
1、准备镜像文件
准备 tomcat 和 jdk 到当前目录,编写好 README
2、编写 dokerfile,官方命名Dockerfile
,build
时会自动寻找这个文件,就不需要-f
指定了
FROM centos
MAINTAINER achang995931576@qq.com
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u161-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.41.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0.161
ENV CLASSPATH JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.41
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.41
ENV PATH JAVA_HOME/bin:CATALINA_HOME/bin
WORKDIR $MYPATH
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.41/bin/startup.sh && tail -F /usr/localapache-tomcat-9.0.41/bin/logs/catalina.out
3、构建镜像,通过build
命令构建,不要忘记 .
docker build -t dirtomcat .
[root@izbp1h9ynqgizuhp85e2xkz test]# docker build -t dirtomcat .
Sending build context to Docker daemon 201.2MB
Step 1/15 : FROM centos
---> 300e315adb2f
Step 2/15 : MAINTAINER achang995931576@qq.com
---> Using cache
---> 82edb0ac69c2
Step 3/15 : COPY readme.txt /usr/local/readme.txt
---> 6139a88f8d11
Step 4/15 : ADD jdk-8u161-linux-x64.tar.gz /usr/local
---> 9de12dfdd661
Step 5/15 : ADD apache-tomcat-9.0.41.tar.gz /usr/local
---> 1686e6a55ab1
Step 6/15 : RUN yum -y install vim
---> Running in 61ebe2e2edec
CentOS Linux 8 - AppStream 1.3 MB/s | 6.3 MB 00:04
CentOS Linux 8 - BaseOS 507 kB/s | 2.3 MB 00:04
CentOS Linux 8 - Extras 15 kB/s | 8.6 kB 00:00
Last metadata expiration check: 0:00:01 ago on Sun Jan 31 03:56:13 2021.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-15.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-15.el8 appstream 6.3 M
vim-filesystem noarch 2:8.0.1763-15.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: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 294 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 1.3 MB/s | 48 kB 00:00
(3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 3.7 MB/s | 1.4 MB 00:00
(4/5): vim-common-8.0.1763-15.el8.x86_64.rpm 12 MB/s | 6.3 MB 00:00
(5/5): which-2.21-12.el8.x86_64.rpm 100 kB/s | 49 kB 00:00
Total 4.7 MB/s | 7.8 MB 00:01
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 Linux 8 - AppStream 1.3 MB/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-15.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-15.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-15.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-15.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-15.el8.x86_64
vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
which-2.21-12.el8.x86_64
Complete!
Removing intermediate container 61ebe2e2edec
---> 72d40532bcc0
Step 7/15 : ENV MYPATH /usr/local
---> Running in 64953e87fb82
Removing intermediate container 64953e87fb82
---> 157e51dd50d8
Step 8/15 : ENV JAVA_HOME /usr/local/jdk1.8.0.161
---> Running in 28e653e0397f
Removing intermediate container 28e653e0397f
---> 6bed45cea259
Step 9/15 : ENV CLASSPATH JAVA_HOME/lib/tools.jar
---> Running in 714dfa4e0f32
Removing intermediate container 714dfa4e0f32
---> 00438e7f51e3
Step 10/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.41
---> Running in f38eb17a0979
Removing intermediate container f38eb17a0979
---> 633b86a072f4
Step 11/15 : ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.41
---> Running in d26c47994ef8
Removing intermediate container d26c47994ef8
---> b7903f70d65c
Step 12/15 : ENV PATH JAVA_HOME/bin:CATALINA_HOME/bin
---> Running in 2afe4f349677
Removing intermediate container 2afe4f349677
---> aedaee0f98df
Step 13/15 : WORKDIR $MYPATH
---> Running in 0a013008277c
Removing intermediate container 0a013008277c
---> 9760898da7dd
Step 14/15 : EXPOSE 8080
---> Running in d69219c8493e
Removing intermediate container d69219c8493e
---> 46190e253c19
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.41/bin/startup.sh && tail -F /usr/localapache-tomcat-9.0.41/bin/logs/catalina.out
---> Running in 3f0b6ea8f2ed
Removing intermediate container 3f0b6ea8f2ed
---> 08532a1cace3
Successfully built 08532a1cace3
Successfully tagged dirtomcat:latest
4、run 镜像
[root@izbp1h9ynqgizuhp85e2xkz test]# docker run -d -p 5333:8080 --name achangtomcat -v /home/test/tomcat/test:/usr/local/apache-tomcat-9.0.41/webapps/test -v /home/test/build/tomcat/logs/:/usr/local/apache-tomcat-9.0.41/logs diytomcat
5、访问测试
6、发布项目(由于做了卷挂载,我们直接在本地编写项目就可以发布了!)
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>db</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp:↓
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello,achang</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println("-----test-----");
%>
</body>
</html>
发现:项目部署成功,可以直接访问!
我们以后开发的步骤:需要掌握 Dockerfile 的编写!我们之后的一切都是使用 docker 镜像来发布运行!
[](()发布自己的镜像
Dockerhub
2、确定这个账号可以登录
3、登录
docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker 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
4、提交 push 镜像
[root@izbp1h9ynqgizuhp85e2xkz logs]# docker push diytomcat
Using default tag: latest
The push refers to repository [docker.io/library/diytomcat]
8c521d4e223e: Preparing
150761504cc4: Preparing
303269e3e2c1: Preparing
f26a70e72c57: Preparing
2653d992f4ef: Preparing
denied: requested access to the resource is denied #push 失败,被拒绝
会发现 push 不上去,因为如果没有前缀的话默认是 push 到 官方的 library
解决方法
前提保证 docker login -u XXXX 账号 -p XXX 密码 # 登录过
docker tag 容器名 docker 库你自己的 id/自定义名
docker tag diytomcat achang3306/diytomcat
再 push
docker push achang3306/diytomcat
阿里云镜像服务上
进入阿里云容器镜像服务
1、登录阿里云
2、找到容器镜像服务
3、创建名称容器(一个大容器,下面放很多的镜像)
4、创建容器镜像
5、浏览阿里云
注意:要根据阿里云要求的格式书写指令
看官网 很详细 https://cr.console.aliyun.com/repository/
$ sudo docker login --username=995931576@qq.com registry.cn-hangzhou.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/achang/achang-test:[镜像版本号]
$ sudo docker push registry.cn-hangzhou.aliyuncs.com/achang/achang-test:[镜像版本号]
示例:
登录获得权限
[root@izbp1h9ynqgizuhp85e2xkz logs]# docker login --username=995931576@qq.com 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
[root@izbp1h9ynqgizuhp85e2xkz logs]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
diytomcat latest 27fce10d773a 3 hours ago 668MB
achang/tomcat 1.0 27fce10d773a 3 hours ago 668MB
achang1010/diytomcat latest 27fce10d773a 3 hours ago 668MB
achang123/diytomcat 0.1 27fce10d773a 3 hours ago 668MB
achang168/diytomcat latest 27fce10d773a 3 hours ago 668MB
achang3306/diytomcat latest 27fce10d773a 3 hours ago 668MB
achang555/tomcat 0.1 27fce10d773a 3 hours ago 668MB
entrypoint-test latest 2c09d7cc807f 26 hours ago 209MB
cmd-test latest 8e57c56caa7e 27 hours ago 209MB
mycentos 0.1 0513d130fa1b 27 hours ago 282MB
achang/centos latest d4fdaffa138e 2 days ago 209MB
tomcat02 1.0 116c7aa5521d 2 days ago 654MB
mysql 5.7 a70d36bc331a 12 days ago 449MB
redis latest 621ceef7494a 2 weeks ago 104MB
tomcat 9.0 040bdb29ab37 2 weeks ago 649MB
tomcat latest 040bdb29ab37 2 weeks ago 649MB
nginx latest f6d0b4767a6c 2 weeks ago 133MB
centos latest 300e315adb2f 7 weeks ago 209MB
portainer/portainer latest 62771b0b9b09 6 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 10 months ago 791MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
将要上传阿里云的资源改名
[root@izbp1h9ynqgizuhp85e2xkz logs]# docker tag 27fce10d773a registry.cn-hangzhou.aliyuncs.com/achang/achang-test:0.1
[root@izbp1h9ynqgizuhp85e2xkz logs]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
diytomcat latest 27fce10d773a 3 hours ago 668MB
registry.cn-hangzhou.aliyuncs.com/achang/achang-test 0.1 27fce10d773a 3 hours ago 668MB
achang/tomcat 1.0 27fce10d773a 3 hours ago 668MB
achang1010/diytomcat latest 27fce10d773a 3 hours ago 668MB
achang123/diytomcat 0.1 27fce10d773a 3 hours ago 668MB
achang168/diytomcat latest 27fce10d773a 3 hours ago 668MB
achang3306/diytomcat latest 27fce10d773a 3 hours ago 668MB
achang555/tomcat 0.1 27fce10d773a 3 hours ago 668MB
entrypoint-test latest 2c09d7cc807f 26 hours ago 209MB
cmd-test latest 8e57c56caa7e 27 hours ago 209MB
mycentos 0.1 0513d130fa1b 27 hours ago 282MB
achang/centos latest d4fdaffa138e 2 days ago 209MB
tomcat02 1.0 116c7aa5521d 2 days ago 654MB
mysql 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 5.7 a70d36bc331a 12 days ago 449MB
redis latest 621ceef7494a 2 weeks ago 104MB
tomcat 9.0 040bdb29ab37 2 weeks ago 649MB
tomcat latest 040bdb29ab37 2 weeks ago 649MB
nginx latest f6d0b4767a6c 2 weeks ago 133MB
centos latest 300e315adb2f 7 weeks ago 209MB
portainer/portainer latest 62771b0b9b09 6 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 10 months ago 791MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
上传 push 想要的镜像
[root@izbp1h9ynqgizuhp85e2xkz logs]# docker push registry.cn-hangzhou.aliyuncs.com/achang/achang-test:0.1
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/achang/achang-test]
8c521d4e223e: Pushed
150761504cc4: Pushed
303269e3e2c1: Pushed
f26a70e72c57: Pushed
2653d992f4ef: Pushed
0.1: digest: sha256:2194133a2e9eed0f20a085ffbfe0e2d1af4015adc9120b4d38dc992f8d1a914e size: 1373
评论