Centos7下安装Docker Engine

.

Docker 需要安装在64位系统的CentOS上,而且linux内核至少在3.10版本以上,这个版本的内核在CenOS7上运行. 所以只要安装一个CenOS7  64位的系统就可以了.作为学习,大家只需安装个虚拟机就OK.

可以通过uname  -r 来查看linux内核版本.

[root@localhost ~]# uname -r

3.10.0-327.el7.x86_64

最后,建议你全面更新你的系统. 请记住,你已经完全修复任何潜在的内核bug.

二.安装Docker

有两种方式来安装Docker,这里只介绍其中的一种,通过yum来安装Docker.

1、用一个用户登录你的机器,这个用户必须拥有sudo或root权限.

2、为了确保你的yum包是最新的,用下面脚本做下更新.

[root@localhost ~]# sudo yum update

3、加入yum repo

$ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'

[dockerrepo]

name=Docker Repository

baseurl=https://yum.dockerproject.org/repo/main/centos/7/

enabled=1

gpgcheck=1

gpgkey=https://yum.dockerproject.or

4、安装Docker包

$ sudo yum install docker-engine

5、启动Docker守护进程

$ sudo service docker start

6、通过运行容器中的一个测试image,来验证你的Docker安装正确.

$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally

    latest: Pulling from hello-world

    a8219747be10: Pull complete

    91c95931e552: Already exists

    hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

    Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d

    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.

            (Assuming it was not already locally available.)

     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

 

    For more examples and ideas, visit:

     http://docs.docker.com/userguide/

至此,Docker就已安装好了.

三、自动启动Docker

若要保证,你开机时,自动启动Docker,可以执行如下的命令:

[root@localhost ~]# systemctl enable docker.service

Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

出现以上说明设置开机自启动成功

四、开启docker远程访问功能(可以开发电脑,远程push 镜像到此服务器)

·  编辑 /usr/lib/systemd/system/docker.service 文件,修改 ExecStart 处:
vim /usr/lib/systemd/system/docker.service

·   ExecStart=/usr/bin/dockerd-current 修改为 ExecStart=/usr/bin/dockerd-current--registry-mirror=https://registry.docker-cn.com -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

(此处注意,如果你的原内容没有current,那么修改的文件也不带current)

修改完后 先执行:systemctl daemon-reload

再执行启动:systemctl restart docker

测试配置是否成功
输入命令:curl http://127.0.0.1:2375/info

测试配置是否成功
curl http://127.0.0.1:2375/info

 

如上所示成功

 

设置 2375端口可以被外部机器正常访问

 

Springboot 项目配置

  1. 添加 docker依赖

<!--docker 支持-->

            <plugin>

                <groupId>com.spotify</groupId>

                <artifactId>docker-maven-plugin</artifactId>

                <version>0.4.13</version>

                <configuration>

<!--生成的镜像名 此处名称一定为小写,大写通不过,可以添加横线或者下划线也可以写成这种动态的<imageName>ramer/${project.artifactId}</imageName>,但是如果是这样的话,springboot工程名不能有大写-->

                    <imageName>ramer/test-server-wyx</imageName>

                    <!--docker 远程访问地址-->

                    <dockerHost>http://120.79.29.204:2375</dockerHost>

                    <dockerDirectory>src/main/docker</dockerDirectory>

                    <resources>

                        <resource>

                            <targetPath>/</targetPath>

                            <directory>${project.build.directory}</directory>

                            <include>${project.build.finalName}.jar</include>

                        </resource>

                    </resources>

                </configuration>

            </plugin>

  1. 添加springboot 项目打包所需要的插件依赖

<!-- 打包成可执行jar,防止乱码,必须要下面这一个插件  -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

<encoding>utf-8</encoding>

</configuration>

</plugin>

<plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

        <configuration>

<!--  springboot项目的启动类名称-->

<mainClass>com.whd.serviceCenter.TestServiceApplication</mainClass>

        </configuration>

        <executions>

          <execution>

            <goals>

              <goal>repackage</goal>

            </goals>

          </execution>

        </executions>

</plugin>

 

  1. 在项目main目录下添加docker目录,docker目录下添加Dockerfile文本
  2. 在idea中直接在main下添加就行,在eclipse中通过新建src/main/docker来新建目录
  3. Dockerfile的内容如下:

FROM frolvlad/alpine-oraclejdk8

VOLUME /tmp

ADD testService-0.0.1-SNAPSHOT.jar app.jar  这个地方红色是打包后的jar包名

RUN sh -c 'touch /app.jar'                 app.jar是别名

ENV JAVA_OPTS=""

ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

  1. 特别注意:<imageName>ramer/test-server-wyx</imageName> 在命名docker镜像名称时不能有大写字母出现,可以采用横线或者纯小写,又或者下划线(但没试验过),否则打包会失败
  2. 然后打包push到docker镜像:eclipse中先执行一下maven clean,以免产生污染,然后在maven build内输入 package docker:build 回车就行,idea中直接cd到根目录,然后输入mvn clean package docker:build 命令就行
  3. 成功后,在服务器输入 docker images 查看是否有镜像   

   

以上即为打包成功

  1. 然后启动镜像:

可以通过

docker run -p 8080:8080 -t ramer/springboot-with-docker  

或者通过

docker run -p 8762:8761 da279ae920c8  

第一种是根据打包后的镜像名称来启动,第一种是根据打包后的镜像id来启动  其中端口号,前面一个表示你设置的端口号,也就是你启动后要访问的端口号,后面一个端口号是springboot项目中的端口号

  1. 停止镜像: docker stop 镜像id
  2. 删除镜像:  docker rmi 镜像id  

在删除镜像时,如果镜像已经置于容器中要先停止容器:docker container kill [容器ID]

查看容器输入命令:docker ps -a  ,然后再删除容器docker container rm [containerID]

删除容器后才能删除镜像

1. 运行容器  

docker container run ###(容器名)

2. 停止容器  

docker container kill [容器ID]

3. 列出本机正在运行的容器

docker container ls

4. 列出本机所有容器,包括终止运行的容器

docker container ls --all    |   docker ps -a (显示包括已删除的,退出的等)

5. 删除容器文件,这样就不会占用硬盘空间

docker container rm [containerID]

12.docker 命令补充

# 先重启 systemctl 守护进程

$ sudo systemctl daemon-reload

# 再开启 docker 服务

$ sudo systemctl start docker

13.docker容器的日志查看

先切换到 /var/lib/containers/

然后ls 找到你要查看的容器id ,进入到对应的id目录

然后ls 找到.log文件,cat文件名 或者 less文件名查看

也可以vim 或者vi 编辑查看

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值