上云——docker基础篇:挂载、制作dockerfile

文章目录

  • 1 容器数据卷
    • 使用数据卷
    • 具名和匿名挂载
    • 预了解docker file
    • 数据卷容器
  • 2 Docker file
    • 构建过程
    • dockerfile指令介绍
    • dockerfile举例
    • CMD和ENTRYPOINT区别
    • tomcat镜像制作
    • 发布镜像到dockerhub

1 容器数据卷

# 将容器内的数据同步到本地,目录挂载在本地(持久化)

使用数据卷

方式一

# 方式一:直接使用命令来挂载
docker run -it -v 主机目录,容器内目录

# 案例 /usr/local/docker_data/test是主机目录,/home是容器目录
[root@lv94 docker_data]# docker run -it -v /usr/local/docker_data/test:/home centos /bin/bash

# 查看容器的相关信息
docker inspect 容器id
# Mounts就是挂载的相关信息

上云——docker基础篇:挂载、制作dockerfile_第1张图片

不管是在主机还是在容器内对该路径下文件操作,都会同步改变,即使容器exit后修改目录下文件后重新attach容器发现是同步修改过的

docker安装mysql利用挂载目录

# 下载docker镜像
docker pull mysql:5.7

# 主机挂载mysql conf文件、数据目录、用户名密码
# 官方案例:docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
-d 后台运行
-p 端口映射
-v 数据卷挂载
-e 环境参数配置
--name 容器名称
[root@lv94 docker]# docker run -d -p 3344:3306 -v /usr/local/soft/docker/mysql/conf:/etc/mysql/conf.d -v /usr/local/soft/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

# 启动后,用数据库管理和设计工具(比如DBeaver、Navicat)进行连接,可以连接成功
# 新建一个数据后,发现挂载的目录下也会同样出现该数据

具名和匿名挂载

# 匿名挂载
-v 容器内路径

`下面的情况就是匿名挂载,因为没有指定挂载所在主机路径,只是指定了容器的目录`
[root@lv94 docker]# docker run -d -P --name nginx03 -v /etc/nginx nginx
205ac0f317013b86114a14736f5f7a721a5295264bfa88b64d082dc09eb7f1c3

# 查看所有volume的情况
[root@lv94 docker]# docker volume ls
DRIVER    VOLUME NAME
local     4bcc8cf2fc420c1e0f909ebb3085e7d77b66c851752592527e5d492932616ca7
local     56e19184c820b393305a44b93e8aa5869bfce2606d4bc26c2514d014ab1a4f3b
local     223a3e41073f3ac968a86625aec7c6957052e3cce6982f571a10751c5502631a
[root@lv94 docker]#



[root@lv94 docker]# docker run -d -P --name nginx04 -v specificname_nginx:/etc/nginx nginx
28626c8b0afbe19539e58650b5d80159b9e151650f1185cc5e628ff32b480743
[root@lv94 docker]# docker volume ls
DRIVER    VOLUME NAME
local     4bcc8cf2fc420c1e0f909ebb3085e7d77b66c851752592527e5d492932616ca7
local     56e19184c820b393305a44b93e8aa5869bfce2606d4bc26c2514d014ab1a4f3b
local     223a3e41073f3ac968a86625aec7c6957052e3cce6982f571a10751c5502631a
local     specificname_nginx
[root@lv94 docker]#

# 如何用volume name找到主机挂载的目录
docker volume inspect [VOLUME NAME]

[root@lv94 docker]# docker volume inspect specificname_nginx
[
    {
     
        "CreatedAt": "2021-11-12T19:48:33+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/specificname_nginx/_data",
        "Name": "specificname_nginx",
        "Options": null,
        "Scope": "local"
    }
]

# 没有指定的挂载目录其实都在/var/lib/docker/volumes/xxx 下

区别具名挂载、匿名挂载、指定路径挂载?

-v + xxx 挂载类型
容器内路径 匿名挂载
卷名:容器内路径 具名挂载
/主机路径:容器内路径 指定路径挂载

部分还有加上权限的功能,ro:只读、rw:可读可写,ro只能通过主机操作,容器内部无权限,默认是rw

比如:docker run -d -P --name nginx04 -v specificname_nginx:/etc/nginx:ro nginx

docker run -d -P --name nginx04 -v specificname_nginx:/etc/nginx:rw nginx

预了解docker file

方式二

docker file是用来构建docker镜像的构建文件命令脚本

通过脚本可以生成镜像,镜像是分层结构的,脚本自然是一个个的,对应每一层

举例:创建一个文件,比如dockerfile1,输入以下代码保存

FROM centos

VOLUME ["volume01","volume02"]

CMD echo "---success---"
CMD /bin/bash

命令行输入docker bulid

[root@lv94 docker-test-volume]# docker build -f dockerfile1 -t xiaoyoupei/centos .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in b479f0273842
Removing intermediate container b479f0273842
---> 52090f97f6f6
Step 3/4 : CMD echo "---success---"
---> Running in 9c3ea2e0f86a
Removing intermediate container 9c3ea2e0f86a
---> 903915689e09
Step 4/4 : CMD /bin/bash
---> Running in 0a71b49dd88d
Removing intermediate container 0a71b49dd88d
---> c898e5abd096
Successfully built c898e5abd096
Successfully tagged xiaoyoupei/centos:latest

查看镜像docker images

[root@lv94 docker-test-volume]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
xiaoyoupei/centos     latest    c898e5abd096   53 seconds ago   231MB

可以看到我们是带了挂载的直接docker ps,查看容器id再查看详细信息docker inspect 容器id,就可以找到Mounts下的文件挂载主机的路径,这是匿名的

数据卷容器

多个容器如何同步数据?采用数据卷容器

# 启动第一个
[root@lv94 ~]# docker run -it --name docker01 xiaoyoupei/centos
[root@0f1d9d632b4e /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var       volume02
dev  home  lib64  media       opt  root  sbin  sys  usr  volume01
# 还记得怎么退出?ctrl+p+q

# 启动第二个
[root@lv94 ~]# docker run -it --name docker02 --volumes-from docker01 xiaoyoupei/centos
[root@cf4f92daf880 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var       volume02
dev  home  lib64  media       opt  root  sbin  sys  usr  volume01

# 在被挂载的docker01目录中创建目录看docker02是否存在
[root@lv94 ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                               NAMES
cf4f92daf880   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   54 seconds ago   Up 53 seconds                                       docker02
0f1d9d632b4e   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   4 minutes ago    Up 4 minutes                                        docker01
1bc0aebaf557   mysql:5.7             "docker-entrypoint.s…"   29 hours ago     Up 29 hours     33060/tcp, 0.0.0.0:3344->3306/tcp   mysql03
cd225e4de078   portainer/portainer   "/portainer"             6 days ago       Up 6 days       0.0.0.0:8089->9000/tcp              sleepy_liskov
[root@lv94 ~]# docker attach 0f1d9d632b4e
[root@0f1d9d632b4e /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var       volume02
dev  home  lib64  media       opt  root  sbin  sys  usr  volume01
[root@0f1d9d632b4e /]# cd volume01/
[root@0f1d9d632b4e volume01]# ls
[root@0f1d9d632b4e volume01]# touch docker01
[root@0f1d9d632b4e volume01]# read escape sequence
[root@lv94 ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                               NAMES
cf4f92daf880   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   2 minutes ago   Up 2 minutes                                       docker02
0f1d9d632b4e   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   5 minutes ago   Up 5 minutes                                       docker01
1bc0aebaf557   mysql:5.7             "docker-entrypoint.s…"   29 hours ago    Up 29 hours    33060/tcp, 0.0.0.0:3344->3306/tcp   mysql03
cd225e4de078   portainer/portainer   "/portainer"             6 days ago      Up 6 days      0.0.0.0:8089->9000/tcp              sleepy_liskov
[root@lv94 ~]# docker attach cf4f92daf880
[root@cf4f92daf880 /]# cd volume01/
[root@cf4f92daf880 volume01]# ls
docker01

得出结论,docker01创建的内容同步到了docker02上面,可以类比java中子类继承父类的;这个docker01就是数据卷容器

# 同理,再创建docker03也会共享
[root@lv94 ~]# docker run -it --name docker03 --volumes-from docker01 xiaoyoupei/centos
[root@be64d3656dfb /]# cd volume0
bash: cd: volume0: No such file or directory
[root@be64d3656dfb /]# cd volume01/
[root@be64d3656dfb volume01]# ls
docker01
[root@be64d3656dfb volume01]# touch docker03
[root@be64d3656dfb volume01]# 
[root@lv94 ~]# docker attach docker01
[root@0f1d9d632b4e volume01]# ls
docker01  docker03

那问题来了?如何删除docker01,会出现什么影响?数据还在嘛?

[root@lv94 ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                               NAMES
be64d3656dfb   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   2 minutes ago    Up 2 minutes                                        docker03
cf4f92daf880   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   9 minutes ago    Up 9 minutes                                        docker02
0f1d9d632b4e   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   12 minutes ago   Up 12 minutes                                       docker01
1bc0aebaf557   mysql:5.7             "docker-entrypoint.s…"   29 hours ago     Up 29 hours     33060/tcp, 0.0.0.0:3344->3306/tcp   mysql03
cd225e4de078   portainer/portainer   "/portainer"             6 days ago       Up 6 days       0.0.0.0:8089->9000/tcp              sleepy_liskov
[root@lv94 ~]# docker rm -f 0f1d9d632b4e
0f1d9d632b4e
[root@lv94 ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                               NAMES
be64d3656dfb   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   2 minutes ago   Up 2 minutes                                       docker03
cf4f92daf880   xiaoyoupei/centos     "/bin/sh -c /bin/bash"   9 minutes ago   Up 9 minutes                                       docker02
1bc0aebaf557   mysql:5.7             "docker-entrypoint.s…"   29 hours ago    Up 29 hours    33060/tcp, 0.0.0.0:3344->3306/tcp   mysql03
cd225e4de078   portainer/portainer   "/portainer"             6 days ago      Up 6 days      0.0.0.0:8089->9000/tcp              sleepy_liskov
[root@lv94 ~]# docker attach docker02
[root@cf4f92daf880 volume01]# ls
docker01  docker03

结论是即使被挂载的容器删除了,需挂载的容器数据还在

,为什么?因为这是备份的,所以容器虽然是隔离的,但是数据是可以共享的,应用场景比如mysql共享

# 还记得-v指的是什么?匿名挂载
[root@lv94 ~]# docker run -d -p 3344:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

[root@lv94 ~]# docker run -d -p 3344:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-form mysql01 mysql:5.7

我们可以设想一下这个应用场景,是不是想到了安装某些集群组件的时候,拷贝文件?如何消除

2 Docker file

`命令参数的脚本`
# 构建的步骤
1、编写docker file文件
2、docker bulid 构建镜像
3、docker run 运行镜像
4、docker push 发布镜像(DockerHub)

构建过程

# 前提知识
1、每个关键字(指令)都必须是大写字母
2、执行从上到下顺序执行
3# 号符表示注释
4、每一个指令都会创建一个新的镜像层(分层结构),并提交

在实际项目的时,发布项目需要做镜像,需要创作dockerfile文件,越来越成为项目交付的主流

名词关系

docker file:梳理流程、构建文件,相当于源代码

docker images:通过dockerfile构建生成的镜像,最终发布运行的产品,可以理解为jar——>dockerimages

docker container:将dockerimages运行提供服务

dockerfile指令介绍

关键字 解释
FROM 基础镜像
MAINTAINER 镜像是谁创作的
RUN 镜像运行的命令
ADD 镜像层级步骤
WORKDIR 镜像工作目录
VOLUME 镜像挂载的目录
EXPOSE 镜像暴露的端口
CMD 指定容器启动运行时命令,只有最后一个会生效,可被替代
ENTRYPOINT 指定容器启动运行时命令,可以追加命令
ONBUILD 构建一个被继承dockerfile后,会触发该命令
COPY 类似ADD,将文件拷贝到镜像中
ENV 构建时设置环境变量

dockerfile举例

dockerfile的FROM基本先写一个FROM scratch

# 构建属于你的centos
# 1、构建dockerfile文件
[root@lv94 dockerfile]# cat dockerfile_test_centos
FROM centos
MAINTAINER xiaoyoupei<masu_cs_xyp@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 "----successed----"
CMD /bin/bash

# 2、通过dockerfile构建镜像
[root@lv94 dockerfile]# docker build -f dockerfile_test_centos -t mycentos .
成功后会
Successfully built 65cbdddb7fb9
Successfully tagged mycentos:latest

# 3、运行该容器
[root@lv94 dockerfile]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED              SIZE
mycentos              latest    65cbdddb7fb9   About a minute ago   342MB
xiaoyoupei/centos     latest    c898e5abd096   4 days ago           231MB
tomcat_test           1.0       89422e30211d   5 days ago           684MB
tomcat                9.0       43e421a14aec   3 weeks ago          680MB
tomcat                latest    b0e0b0a92cf9   3 weeks ago          680MB
mysql                 5.7       938b57d64674   4 weeks ago          448MB
redis                 latest    7faaec683238   5 weeks ago          113MB
nginx                 latest    87a94228f133   5 weeks ago          133MB
centos                latest    5d0da3dc9764   2 months ago         231MB
portainer/portainer   latest    580c0e4e98b0   8 months ago         79.1MB
elasticsearch         7.6.2     f29a1ee41030   20 months ago        791MB
[root@lv94 dockerfile]# docker run -it mycentos
[root@e1b09e3d0467 local]# pwd
/usr/local
[root@e1b09e3d0467 local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@e1b09e3d0467 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        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

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        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

[root@e1b09e3d0467 local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@e1b09e3d0467 local]# vim test
[root@e1b09e3d0467 local]# vim test
[root@e1b09e3d0467 local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src  test
# 显示镜像构建的历史步骤
[root@lv94 dockerfile]# docker history 65cbdddb7fb9
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
65cbdddb7fb9   5 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B
098b46b0dea9   5 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
e4375ef988bc   5 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
2f4f46c7b8e6   5 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B
50d8e8fc024d   5 minutes ago   /bin/sh -c yum -y install net-tools             35MB
98a19a4bd5dc   5 minutes ago   /bin/sh -c yum -y install vim                   76.2MB
d8849c91e2f3   6 minutes ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B
873203768a7b   6 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B
a739de5e10c7   6 minutes ago   /bin/sh -c #(nop)  MAINTAINER xiaoyoupei
5d0da3dc9764   2 months ago    /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>      2 months ago    /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>      2 months ago    /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB

CMD和ENTRYPOINT区别

CMD 指定容器启动运行时命令,只有最后一个会生效,可被替代 ENTRYPOINT 指定容器启动运行时命令,可以追加命令

# 测试CMD
[root@lv94 dockerfile]# cat docker_test_cmd
FROM centos
CMD ["ls","-a"]
[root@lv94 dockerfile]# docker build -f docker_test_cmd -t cmdtest .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 6c3a23421744
Removing intermediate container 6c3a23421744
 ---> c37b2be7374b
Successfully built c37b2be7374b
Successfully tagged cmdtest:latest
[root@lv94 dockerfile]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
cmdtest               latest    c37b2be7374b   30 seconds ago   231MB
mycentos              latest    65cbdddb7fb9   11 minutes ago   342MB
xiaoyoupei/centos     latest    c898e5abd096   4 days ago       231MB
tomcat_test           1.0       89422e30211d   5 days ago       684MB
tomcat                9.0       43e421a14aec   3 weeks ago      680MB
tomcat                latest    b0e0b0a92cf9   3 weeks ago      680MB
mysql                 5.7       938b57d64674   4 weeks ago      448MB
redis                 latest    7faaec683238   5 weeks ago      113MB
nginx                 latest    87a94228f133   5 weeks ago      133MB
centos                latest    5d0da3dc9764   2 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   8 months ago     79.1MB
elasticsearch         7.6.2     f29a1ee41030   20 months ago    791MB
[root@lv94 dockerfile]# docker run cmdtest
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

# 可以追加命令嘛?试试看
[root@lv94 dockerfile]# docker run cmdtest -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled
# 直接报错

# 写完全就可以
[root@lv94 dockerfile]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
cmdtest               latest    c37b2be7374b   5 minutes ago    231MB
[root@lv94 dockerfile]# docker run c37b2be7374b ls -al
total 0
drwxr-xr-x   1 root root   6 Nov 16 12:24 .
drwxr-xr-x   1 root root   6 Nov 16 12:24 ..
-rwxr-xr-x   1 root root   0 Nov 16 12:24 .dockerenv
lrwxrwxrwx   1 root root   7 Nov  3  2020 bin -> usr/bin
......


# 测试ENTRYPOINT
[root@lv94 dockerfile]# cat docker_test_entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
[root@lv94 dockerfile]# docker build -f docker_test_entrypoint -t entrypointtest .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 02731ff5e636
Removing intermediate container 02731ff5e636
 ---> 85392d34fde9
Successfully built 85392d34fde9
Successfully tagged entrypointtest:latest
[root@lv94 dockerfile]# docker run entrypointtest
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
......
[root@lv94 dockerfile]# docker run entrypointtest -l
total 0
drwxr-xr-x   1 root root   6 Nov 16 12:27 .
drwxr-xr-x   1 root root   6 Nov 16 12:27 ..
-rwxr-xr-x   1 root root   0 Nov 16 12:27 .dockerenv
lrwxrwxrwx   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root 340 Nov 16 12:27 dev
drwxr-xr-x   1 root root  66 Nov 16 12:27 etc
drwxr-xr-x   2 root root   6 Nov  3  2020 home
lrwxrwxrwx   1 root root   7 Nov  3  2020 lib -> 
......

对比发现,entrypoint是可以直接拼接命令然后执行的,而cmd必须要写原生的命令

tomcat镜像制作

1、需要准备镜像文件tomcat压缩包、jdk的压缩包

[root@lv94 docker_tomcat_images]# ll -al
total 198636
drwxr-xr-x 2 root root        75 Nov 17 18:19 .
drwxr-xr-x 7 root root       114 Nov 17 18:02 ..
-rw-r--r-- 1 root root  11579748 Nov 17 18:19 apache-tomcat-9.0.55.tar.gz
-rw-r--r-- 1 root root 191817140 Nov 17 18:05 jdk-8u201-linux-x64.tar.gz

2、编写dockerfile,官方命名Dockerfile,bulid会自动找到该文件,不需要-f指定

[root@lv94 docker_tomcat_images]# cat Dockerfile
FROM centos
MAINTAINER xiaoyoupei<masu_cs_xyp@163.com>

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u201-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.55.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_201
ENV CLASS_HOME $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.55
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.55
ENV PATH $PATH:$JAVA_HOME/bin:$CATAINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.55/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.55/bin/logs/catalina.out

3、构建镜像

docker build -t mytomcat .

4、启动镜像

[root@lv94 docker_tomcat_images]# docker run -d -p 7777:8080 --name mytomcat -v /usr/local/soft/docker/docker_tomcat_images/test:/usr/local/apache-tomcat-9.0.55/webapps/test -v /usr/local/soft/docker/docker_tomcat_images/logs:/usr/local/apache-tomcat-9.0.55/logs mytomcat

5、访问测试

6、发布项目(在本地发布)

在test目录下新建WEB-INF目录,再在WEB-INF下新建web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

</web-app>

在test目录下新建index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>youpei test</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println("test test test");
%>
</body>
</html>

7、访问主机7777端口的test页面(localhost:7777/test),发现成功

发布镜像到dockerhub

地址:hub.docker.com

# 登陆hub.docker.home
[root@lv94 docker_tomcat_images]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xiaoyoupei
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

# 直接push会被拒绝,没有所属(例如:/xiaoyoupei/mytomcat)
[root@lv94 docker_tomcat_images]# docker push mytomcat:latest
The push refers to repository [docker.io/library/mytomcat]
0f413793c84a: Preparing
8bed38372bc5: Preparing
c59641a64679: Preparing
295cda1076a7: Preparing
74ddd0ec08fa: Preparing
denied: requested access to the resource is denied

# 出现这样的问题怎么解决?
[root@lv94 docker_tomcat_images]# docker push xiaoyoupei/mytomcat:latest
The push refers to repository [docker.io/xiaoyoupei/mytomcat]
An image does not exist locally with the tag: xiaoyoupei/mytomcat

# 加一个分支就行
[root@lv94 docker_tomcat_images]# docker tag 16d505da0355 xiaoyoupei/tomcat:1.0
[root@lv94 docker_tomcat_images]# docker push xiaoyoupei/tomcat:1.0

save、load

上云——docker基础篇:挂载、制作dockerfile_第2张图片

你可能感兴趣的