一、安装步骤
1、sudo apt install docker
2、 sudo systemctl start docker
3、sudo systemctl enable docker ----设置开机启动docker
4、docker -v
5、如需要停止docker执行:systemctl stop docker
6、配置docker镜像源:vim /etc/docker/deamon.json在其中加入
{
"registry-mirrors": [
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://cr.console.aliyun.com",
"https://registry.docker-cn.com"
]
}
然后重启docker即可。
二、常用命令:
2.1、docker images
2.2、docker search
2.3、sudo docker pull mysql
三、切换至root用户
3.1、docker ps
3.2、docker top ef0e8328bacb
3.3、docker inspect ef0e8328bacb
3.4、docker ps -a:
查看所有的容器,包括已经停止的容器。
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be8697e5a152 nginx "/docker-entrypoint.…" 2 minutes ago Exited (0) 23 seconds ago mynginx
7a99d1d29a7a mytom1 "/bin/sh -c '/usr/lo…" 2 years ago Exited (0) 2 years ago pedantic_maxwell
3.5、删除已经停止的容器:docker rm id或name
强制删除运行的容器:docker rm -f id或name
3.6、启动容器:docker start id或name
3.7、停止运行中的 容器:docker stop id或name
四、安装tomcat
0、官方使用(使用完后,自动停止容器并删除容器,用于测试)
docker run -it --rm tomcat:9.0
1、下载再启动tomcat
sudo docker pull tomcat
2、启动容器。
docker run -d -p 8090:8080 --name xuzining tomcat
可以加--restart awalay 开机重启后,容器也重启
3、测试访问8090端口
表明连接tomcat成功,但是无页面展示,进入容器查看tomcat的webapps下是否有程序。
4、 docker exec -it xuzining /bin/bash进入容器
5、把webapps.dist下的项目全部复制到webapps下:cp -r ./webapps.dist/* webapps
6、重新访问正常。或通过命令访问: curl localhost:8090
7、查看docker的状态:
docker stats
5、打包镜像
1、先准备jdk+tomcat包。
2、编写Dockerfile
3、执行打包操作 :docker build -t diytomcat .
4、打包:
Sending build context to Docker daemon 203.2MB
Step 1/15 : FROM centos
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
---> 300e315adb2f
Step 2/15 : MAINTAINER xujf<59988@qqcom>
---> Running in e8de3d932f9c
Removing intermediate container e8de3d932f9c
---> 14d3f3d45373
Step 3/15 : COPY readme.txt /usr/local/
---> 7ae6da93e690
Step 4/15 : ADD jdk-8u192-linux-x64.tar.gz /usr/loacl/
---> 3181df1fcd96
Step 5/15 : ADD apache-tomcat-9.0.45.tar.gz /usr/loacl/
---> 56bcf40c0fda
Step 6/15 : RUN yum -y install vim
---> Running in 0447543c0f70
CentOS Linux 8 - AppStream 2.0 MB/s | 6.3 MB 00:03
CentOS Linux 8 - BaseOS 1.8 MB/s | 2.3 MB 00:01
CentOS Linux 8 - Extras 16 kB/s | 9.6 kB 00:00
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
5、docker images
6、启动:docker run -d -p 9090:8080 --name xuziningtomcat -v /home/xuzining/bulid/tomcat/test:/usr/local/apache-tomcat-9.0.45/webapps/test -v /home/xuzining/bulid/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.45/logs diytomcat
7、执行后发现,无法访问9090,
于是,查看了docker logs ID值;