A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 大蓝鲸小蟀锅 于 2020-4-23 14:22 编辑

服务相关命令
启动docker服务:
[mw_shl_code=shell,true]
systemctl start docker
[/mw_shl_code]
停止docker服务:
[mw_shl_code=shell,true]
systemctl stop docker
[/mw_shl_code]
重启docker服务:
[mw_shl_code=shell,true]
systemctl restart docker
[/mw_shl_code]
查看docker服务状态:
[mw_shl_code=shell,true]
systemctl status docker
[/mw_shl_code]
设置开机启动docker服务:
[mw_shl_code=shell,true]
systemctl enable docker
[/mw_shl_code]
镜像相关命令
查看镜像  
[mw_shl_code=shell,true]
docker images
[/mw_shl_code]
搜索redis镜像
[mw_shl_code=shell,true]
docker search redis
[/mw_shl_code]
下载最新版本redis镜像
[mw_shl_code=shell,true]
docker pull redis
[/mw_shl_code]
下载指定版本的redis镜像
[mw_shl_code=shell,true]
docker pull redis:版本号
[/mw_shl_code]
查看所有镜像的id
[mw_shl_code=shell,true]
docker images -q
[/mw_shl_code]
删除所有镜像
[mw_shl_code=shell,true]
docker rmi 'docker images -q'
[/mw_shl_code]
删除指定镜像(根据镜像ID或者软件的版本号)
[mw_shl_code=shell,true]
docker rmi 镜像的ID
docker rmi redis:版本号
[/mw_shl_code]
容器相关命令
查看所有的镜像
[mw_shl_code=shell,true]
docker images
[/mw_shl_code]
创建容器(并进入容器)
[mw_shl_code=shell,true]
docker run -it --name=c1 centos:7 /bin/bash
[/mw_shl_code]
退出容器
[mw_shl_code=shell,true]
exit
[/mw_shl_code]
查看容器信息
[mw_shl_code=shell,true]
docker ps -a
[/mw_shl_code]
创建容器(不进入容器,退出容器不会关闭)
[mw_shl_code=shell,true]
docker run -id --name=c2 centos:7
[/mw_shl_code]
进入容器
[mw_shl_code=shell,true]
docker exec -it c2 /bin/bash
[/mw_shl_code]
退出容器(c2容器不会关闭)
[mw_shl_code=shell,true]
exit
[/mw_shl_code]
停止容器c2
[mw_shl_code=shell,true]
docker stop c2
[/mw_shl_code]
启动c2
[mw_shl_code=shell,true]
docker start c2
[/mw_shl_code]
删除容器(根据id或者名称)
[mw_shl_code=shell,true]
docker rm c1
[/mw_shl_code]
查看所有的容器的id
[mw_shl_code=shell,true]
docker ps -aq
[/mw_shl_code]
删除所有的容器(注意:正在运行的容器无法删除)
[mw_shl_code=shell,true]
docker rm 'docker ps -aq'
[/mw_shl_code]
查看c2容器信息
[mw_shl_code=shell,true]
docker inspect c2
[/mw_shl_code]

容器数据卷相关命令
查看所有容器
[mw_shl_code=shell,true]
docker ps -a
[/mw_shl_code]
创建数据卷容器
[mw_shl_code=shell,true]
docker run -it --name=c1 -v /root/data:/root/data_container centos:7 /bin/bash
[/mw_shl_code]
打开一个新窗口(到宿主)
切换到data目录下面
[mw_shl_code=shell,true]
cd data
[/mw_shl_code]
创建一个新的文件
[mw_shl_code=shell,true]
touch a.txt
[/mw_shl_code]
在另一个窗口(到容器)
切换到data_container目录(可以看到a.txt)
[mw_shl_code=shell,true]
cd data_container
[/mw_shl_code]
退出容器c1
[mw_shl_code=shell,true]
exit
[/mw_shl_code]
删除c1容器
[mw_shl_code=shell,true]
docker rm c1
[/mw_shl_code]
打开一个新窗口(到宿主)
切换到data目录下面(看数据,发现容器删除,宿主下数据还在)
[mw_shl_code=shell,true]
cd data
[/mw_shl_code]
创建容器挂载多个数据卷
[mw_shl_code=shell,true]
docker run -it --name=c2 -v ~/data2:/root/data2 -v ~/data3:/root/data3 centos:7 /bin/bash
[/mw_shl_code]
多容器进行数据交换
删除之前的容器(删除c1, c2, c3, c4)
[mw_shl_code=shell,true]
docker rm c1 c2 c3 c4
[/mw_shl_code]
  
创建启动c3数据卷容器,使用 –v 参数 设置数据卷
[mw_shl_code=shell,true]
docker run -it --name=c3 -v /volume centos:7 /bin/bash
[/mw_shl_code]
创建启动 c1 c2 容器,使用 –-volumes-from 参数 设置数据卷
[mw_shl_code=shell,true]
docker run -it --name=c1 --volumes-from c3 centos:7 /bin/bash
docker run -it --name=c2 --volumes-from c3 centos:7 /bin/bash
[/mw_shl_code]
  
到任意一个容器volume目录下面
[mw_shl_code=shell,true]
cd volume
[/mw_shl_code]
创建一个文件(其他容器也可以看到这个文件)
[mw_shl_code=shell,true]
touch a.txt
[/mw_shl_code]
镜像制作 1.容器转为镜像
看到当前容器
[mw_shl_code=shell,true]
docker ps -a
[/mw_shl_code]
把容器转为镜像
[mw_shl_code=shell,true]
docker commit 容器id 镜像名称:版本号
[/mw_shl_code]
例如: docker commit 容器id itheima_tomcat:1.0
查看镜像
[mw_shl_code=shell,true]
docker images
[/mw_shl_code]
把镜像保存为一个压缩文件
[mw_shl_code=shell,true]
docker save -o 压缩文件名称 镜像名称:版本号
[/mw_shl_code]
例如: docker save -o itheima_tomcat.tar itheima_tomcat:1.0

查看目录下面的压缩文件
[mw_shl_code=shell,true]
ll
[/mw_shl_code]
挂载压缩文件
[mw_shl_code=shell,true]
docker load -i itheima_tomcat.tar
[/mw_shl_code]
创建容器
[mw_shl_code=shell,true]
docker run -it --name=new_tomcat itheima_tomcat:1.0 bash
[/mw_shl_code]
2.dockerfile制作镜像
查看镜像
[mw_shl_code=shell,true]
docker images
[/mw_shl_code]
在宿主
创建docker_files目录
[mw_shl_code=shell,true]
mkdir docker_files
[/mw_shl_code]
进入docker_files文件
[mw_shl_code=shell,true]
cd docker_files
[/mw_shl_code]
创建并编辑centos_dockerfile文件
[mw_shl_code=shell,true]
vim centos_dockerfile
[/mw_shl_code]
在centos_dockerfile添加如下内容
[mw_shl_code=shell,true]
FROM centos:7
MAINTAINER itheima
RUN yum install -y vim
WORKDIR /usr
CMD /bin/bash
[/mw_shl_code]
通过dockerfile构建镜像
[mw_shl_code=shell,true]
docker build -f ./centos_dockerfile -t itheima_centos:1 .
[/mw_shl_code]
查看镜像
[mw_shl_code=shell,true]
docker images
[/mw_shl_code]
启动镜像创建容器
[mw_shl_code=shell,true]
docker run -it --name=c11 itheima_centos:1
[/mw_shl_code]

3.部署springboot项目
打开新SFTP窗口,把资料中springboot-hello-0.0.1-SNAPSHOT.jar上传到linux中
[mw_shl_code=shell,true]
put springboot-hello-0.0.1-SNAPSHOT.jar的路径(直接拖动window下文件即可)
[/mw_shl_code]
回到宿主窗口
把springboot-hello-0.0.1-SNAPSHOT.jar放到docker_files目录下面
[mw_shl_code=shell,true]
mv springboot-hello-0.0.1-SNAPSHOT.jar ./docker_files/
[/mw_shl_code]
进入docker_files目录下面
[mw_shl_code=shell,true]
cd docker_files
[/mw_shl_code]
创建并编辑springboot_dockerfile文件
[mw_shl_code=shell,true]
vim springboot_dockerfile
[/mw_shl_code]
按i进入插入模式
[mw_shl_code=shell,true]
FROM java:8
MAINTAINER itheima
ADD springboot-hello-0.0.1-SNAPSHOT.jar app.jar
CMD java -jar app.jar
[/mw_shl_code]
保存退出
ESC
:wq
通过dockerfile构建app镜像
[mw_shl_code=shell,true]
docker build -f ./springboot_dockerfile -t app .
[/mw_shl_code]
查看镜像
[mw_shl_code=shell,true]
docker images
[/mw_shl_code]
运行app镜像
[mw_shl_code=shell,true]
docker run -id -p 9000:8080 app
[/mw_shl_code]
浏览器访问
[mw_shl_code=shell,true]
192.168.200.139:9000/hello
[/mw_shl_code]



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马