docker入门-Javaweb环境搭建--centos7

一. 安装docker

命令:yum install -y docker

安装完成后docker version 可以查看版本信息, 验证是否成功安装

二. helloworld

镜像跟ios镜像文件一个意思,docker 就是一个容器,虚拟机。 而运行虚拟机就需要镜像
1. hello-world
docker run hello-world
运行后的结果:

Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
535020c3e8ad: Pull complete 
af340544ed62: Pull complete 
Digest: sha256:a68868bfe696c00866942e8f5ca39e3e31b79c1e50feaee4ce5e28df2f051d5c
Status: Downloaded newer image for hello-world:latest

Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

hello成功运行,可以看出docker会自动查找镜像并进行下载。

三:搜索并下载镜像

  1. 搜索镜像
    docker search 命令用于搜索镜像
用法:docker search 镜像名称

这里写图片描述

  1. 下载镜像
    docker pull 命令用于下载镜像
    用法:docker pull 镜像名称

三: 查看镜像

这里写图片描述

PEROSITORY:镜像名称
TAG:标签
IMAGE_ID:镜像ID

其中hello-word就是之前docker自动从docker.io下载的镜像,centos也是自己下的, 其它都是自己保存的。

四:启动镜像

容器是在镜像的基础上来运行的,一旦容器启动了,我们就可以登录到容器中,安装自己所需的软件或应用程序。

用法:docker run <相关参数> <镜像 ID> <初始命令>
docker run -i -t -v /usr/localhost/:/data/soft/ 196e0ce0c9fb /bin/bash
-i:表示以“交互模式”运行容器
  ● -t:表示容器启动后会进入其命令行
  ● -v:表示需要将本地哪个目录挂载到容器中,格式:-v <宿主机目录>:<容器目录>

初始命令表示一旦容器启动,需要运行的命令,此时使用“/bin/bash”,表示启动后直接进入bash shell。

进入容器后,ctrl+d 或者exit命令, 退出容器

五:安装Java环境

centos镜像启动后,就是一起全新的centos操作系统,需要我们安装Java环境。tomcat, jdk,环境变量ok后 编写启动脚本
这里写图片描述

!/bin/bash:sh脚本中的一部分,必须要,代表脚本开头。 以前没写过sh脚本, 跟着被人的博客写时, 以为是注释的语句所以自己省略了, 导致后面容器一启动,就自动退出,出现如下错误, 就是提示脚本格式错误

这里写图片描述

六: 查看容器与创建镜像

docker ps -a 查看所有容器

这里写图片描述

Java环境搭建好后, 这时我们需要创建我们自己的镜像, 下次启动我们创建的镜像,不然退出容器后, 重新进入,上次在容器中的修改都会丢失, 因为docker每次启动都是重新加载镜像。

docker commit 命令创建镜像
docker commit -m "创建说明" CONTAINER ID(容器id) REPOSITORY(镜像名):TAG(标签

这里写图片描述

查看镜像是否创建成功 docker images

七:后台启动容器

docker run -d -p 80:8080 镜像id /start.sh

  -d:表示以“守护模式”执行/root/run.sh脚本,此时 Tomcat 控制台不会出现在输出终端上。
  -p:表示宿主机与容器的端口映射,此时将容器内部的 8080 端口映射为宿主机的80 端口,这样就向外界暴露了 80 端口,可通过 Docker 网桥来访问容器内部的 8080 端口了。
/start.sh 是前面编写的启动tomat的脚本文件

在浏览器上输入主机:端口就可以访问tocmat主页了

八. 其它命令

docker ps -a 可查看容器的状态
docker stop 容器id 停止容器
docker rm 容器id  删除容器
docker logs -f 容器id  查看日志
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值