Docker
与运行环境无关,真正做到一次构建到处运行
拉取镜像:
docker pull ip:port/tomcat:9-jre8
docker pull tomcat
docker pull tomcat:9-jre8
启动tomcat:
docker run -p 8080:8080 tomcat
查看镜像列表:
docker images
查看运行的容器:
docker ps
docker ps -a
启动镜像容器:
docker run -it —rm ubuntu:16.04 bash
docker run:启动容器
-I:交互式操作
-t:终端
—rm:容器退出后删除容器
bash:命令--shell脚本处理器
手动 删除容器:
docker rm CONTAINER ID
虚悬镜像:需要手动删除
产生:
docker pull 拉取更新时原有的景象依旧会存在。(既没有仓库名也没有标签,均为none)
docker build (build失败会产生虚悬镜像,build成功之前的镜像会变为虚悬镜像)
删除虚悬镜像:
docker image prune(-a包括删除任何引用为空的镜像)
删除除本地镜像:
docker image rm镜像名:版本号
docker image rm镜像id
docker rmi镜像id
dockerfile定制镜像:
docker run会启动新容器
以交互的方式启动容器:
docker run -it容器id bash
docker run -it —rm容器id bash
以交互的方式进入容器:
docker exec -it容器id bash
步骤:
在宿主机内创建:
mkdir docker
cd docker
mkdir tomcat
cd tomcat
vi Dockerfile
脚本:
FROM:指定基础镜像
ADD:复制需要自动解压的tar,zip压缩包(一般不用)
COPY:cp将上下文中的文件复制到工作空间中】
WORKDIR:切换工作目录
EXPOSE:暴露容器接口
「
FROM tomcat
WORKDIR /usr/local/tomcat/webapps/ROOT/
RUN rm -rf *
RUN echo“hello docker”> /usr/local/tomcat/webapps/ROOT/index.html
」
「
FROM tomcat
WORKDIR /usr/local/tomcat/webapps/ROOT/
RUN rm -rf *
COPY myshop.war .
RUN unzip -oq myshop.war
WORKDIR /usr/local/tomcat/
」
执行:
docker build -t mytomcat .
以80端口运行容器名为tomcat的tomcat:
docker run -p 80:8080 —name tomcat8.5 tomcat
重新启动已退出的容器(退出未删除):
终止容器:
产看引用容器:
查看未引用的容器:
docker restart name | id
docker stop name | id
docker ps
docker ps -a
docker container ls
docker container ls -a
以后台进程启动tomcat:
docker run -p 80:8080 —name tomcat8.5 -d tomcat
删除所有没有引用的容器:
docker container prune
docker数据持久化:
数据卷:(容器里的tomcat/webapps/ROOT与宿主机的tomcat/ROOT共用文件)
docker run -p 80:8080 —name tomcat -d -v /usr/local/tomcat/ROOT:/usr/local/tomcat/webapps/ROOT tomcat
Docker持久化数据库:
docker pull mysql5.7.22
Docker run -p 3306:3306 —name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL ROOT PASSWORD=123456 \
-d mysql:5.7.22
Docker run -p 3306:3306 —name mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL ROOT PASSWORD=123456 \
-d mysql:5.7.22
docker exec -it mysql bash
追加mysql执行脚本文件大小
echo“max_alow_packet = 128m”>> /etc/mysql/conf…..
exit
cd /usr/local/mysql/conf/
将容器里的文件复制到宿主机
docker cp mysql:/etc/my/sql .
mv *.* ..
cd ..
rm -rf mysql
Docker run -p 3306:3306 —name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL ROOT PASSWORD=123456 \
-d mysql:5.7.22
部署项目到容器:
查看tomcat日志:
docker logs tomcat
docker logs -f tomcat
Docker compose:对docker集群的快速编排
安装后:docker-compose version
cd /usr/local/docker/tomcat/
vi docker-compose.yml
脚本:
[
version: 3
services:
tomcat:
restart: always
image: tomcat
container_name: tomcat
ports:
- 8080:8080
mysql:
restart: always
]
启动:(必须在有yml的位置)
docker-compose up
docker-compose up -d
删除停止的容器:(必须在有yml的位置)
docker-compose down
查看日志:
docker-compose logs tomcat
docker-compose logs -f tomcat
部署项目到容器:
cd /usr/local/docker/myproject/
vi docker-compose.yml
脚本
[
version: 3
services:
myproject:
restart: always
image: tomcat
container_name: tomcat
ports:
- 8080:8080
volumes:
- /usr/local/docker/myproject/ROOT:/usr/local/tomcat/webapps/ROOT
mysql:
restart: always
image: mysql:5.7.22
container_name: mysql
Ports:
- 3306:3306
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 13456
command:
—character-set-server=utf8mb4
—collation.server=utf8mb4_general_ci
—explicit_defaults_for_timestamp=true
—lower_case_table_names=1
—max_allowd_packet=128M
—sql-mode=“STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO”
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data
]
网友评论