Docker

Docker

1、安装

#docker-ce安装
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install docker-ce

2、启动


$ sudo service docker start 
$ sudo chkconfig docker on //开机自动启动

3、安装和基本命令

docker pull centos  # 安装centos
docker run -it --name hello centos:latest /bin/bash   
############如下,已经启动容器,并进入容器中操作了########3
[root@0c30524e059a /]# 
[root@0c30524e059a /]# 
[root@0c30524e059a /]# 
[root@0c30524e059a /]# 
[root@0c30524e059a /]# 
[root@0c30524e059a /]# 
# ctrl+p+q退出容器 ,不关闭容器
docker ps   
# 然后就看到如下的内容
#CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
#0c30524e059a        centos:latest       "/bin/bash"         4 minutes ago       Up 4 minutes                            hello

# 杀掉进程
docker stop hello # 优雅关闭,等待10秒然后关闭
docker kill hello # 直接关闭



容器生命周期管理 — docker [run|start|stop|restart|kill|rm|pause|unpause]
容器操作运维 — docker [ps|inspect|exec|logs|export|import|port]
容器rootfs命令 — docker [commit|cp|diff]

4、仓库相关

1、dokcerhub

Docker pull/search/login/push/tag

  • tag [镜像名:版本] [仓库]/[镜像名:版本]:标记本地镜像,将其归入某一仓库
  • Push [仓库]/[镜像名:版本]: 推送镜像到仓库 --需要登陆
  • Search [镜像名]:在仓库中查询镜像 – 无法查询到tag版本
  • Pull [镜像名:版本]: 下载镜像到本地
  • Login:登陆仓库
# 1、login 命令登录
docker login
# 然后输入登录名密码

# 2、tag标记一个镜像,指定自己的仓库
docker tag hello-world baipu/hello:2.0
docker images # 查看是否有镜像

# 3、push命令推送到仓库
docker push baipu/hello:2.0



2、私有仓库
# 用注册镜像(在daemon中配置  {"registry-mirrors":["https://registry.docker-cn.com"]})

# 1、下载registry镜像 
docker pull registry

# 2、启动
docker run -d --name reg -p 5000:5000 registry 

#3、检查 
curl http://127.0.0.1:5000/v2/_catalog

# 4、私服仓库推送
docker tag hello-world http://127.0.0.1:5000/hello-world
docker images 
docker push http://127.0.0.1:5000/hello-world
# 5、查询私服
curl http://127.0.0.1:5000/v2/_catalog
curl http://127.0.0.1:5000/v2/hello/tags/list

3、创建镜像
1、创建容器

docker run -it --name cent centos /bin/bash

2、安装nginx
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum search nginx    ##搜索一下看看
yum install nginx -y    ## 安装

# 启动nginx
whereis nginx 
/usr/sbin/nginx
curl 127.0.0.1
3、commit将一个容器提交为一个nginx镜像

将上边的cent容器提交成一个镜像

docker ps    # 查看所有的容器
docker commit cent cent-ng:v1  # 创建镜像
docker images   # 查看所有的镜像

# 运行镜像
docker run -it --name ng cent-ng:v1 /bin/bash

# 启动的时候,自动运行镜像
docker run -d --name ngx2 cent-ng:v1 /usr/sbin/nginx -g "damemon off "

后面运行的命令都是容器命令,由于nginx命令没有设置到path中,所以全路径启动,

而nginx -g这个参数是指可以在外面添加指令到nginx的配置文件中,

daemon off是指nginx服务不运行在后端,而是在前台运行(container中的服务必须运行在前台

5、数据相关

  • 镜像与容器读写层,通过联合文件系统,组成系统文件视角
  • 容器服务运行中,一定会生成数据
  • 容器只是运行态的服务器,是瞬时的,不承载数据的持久功能
1、volume文件挂载

-v 本地路径:容器中的路径

docker run -v --name data /Users/baipu/Downloads:/opt/test -it centos /bin/bash

然后通过docker inspect data中的Mounts中的source就是本地的路径

2、volumes-from引用数据卷

新启动一个路径,引用一个上一个容器data的挂在路径,两套容器直接共享同一套文件系统

docker run -it --rm --volumes-from data --name app centos /bin/bash
# 进入到容器之后,可以看看 
ls /opt/test # 得到一个和data容器一模一样的挂载

3、备份/恢复

备份:docker run --rm --volumes-from data -v $(pwd):/backup centos tar cvf /backup/data.tar /opt/data

恢复:docker run --rm --volumes-from data -v $(pwd):/backup centos tar xvf /backup/data.tar -C /

docker run --rm ----- 启动一个新的容器,执行完毕删除

--volumes-from data ------- data容器中挂载卷

-v $(pwd):/backup --------挂载当前目录到容器中为backup

cvf /backup/data.tar /opt/data --------- 备份/opt/data目录(即卷中所有的数据)为data.tar

xvf /backup/data.tar -C / ---------- 解压data.tar 到根目录/ ,因tar归档中已包含了/opt/data路径

4、删除数据卷

docker rm -v data

6、Dockerfile

1、创建容器
  1. 找一个目录 /home/weblogic/dockerfile/cmd

  2. 编辑一个文件,dockerfile

  3. FROM centos
    #CMD echo "hello cmd"
    CMD ["/bin/bash/","-c","echo , 'hello cmd !'"]
    
  4. 创建镜像
    docker build -t cmd .

  5. docker run -it cmd (hello cmd !)

2、基本要素
标签含义备注
FROM基础镜像必须第一行,baseimage
MAINTAINER维护信息的人用来标示iamge作者
RUN命令前加上run即可启动一个容器,执行命令,提交存储的变更。多条命令联合执行用&&
ADDcopy文件,会自动解压
WORKDIR设置当前的工作目录
VOLUME挂载主机的目录
EXPOSE要打开声明运行时容器提供的服务端口,着只是一个申明,运行时候不会因为这个申明就打开这个端口的服务。
CMD启动后操作的命令执行容器的默认行为,不再用image中的CMD默认定义的命令
3、docker制作nginx
cd /Users/baipu/work/dockerfile/nginx_docker # (没有就随便找个路径,创建一个目录)
wget  http://nginx.org/download/nginx-1.19.0.tar.gz  # 下载nginx
tar -zvxf nginx-1.19.0.tar.gz
vi Dockerfile
##########Dockerfile begin #####################3
# base image
FROM centos

# MAINTAINER
MAINTAINER peter

# put nginx-1.13.2.tar.gz
ADD nginx-1.19.0 /usr/local/src/nginx-1.19.0

# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN rpm -ivh http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/6/i386/epel-release-6-8.noarch.rpm
RUN yum install -y GeoIP GeoIP-devel
RUN yum install -y libxslt-devel -y gd gd-devel  pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx

# change dir to /usr/local/src/nginx-1.19.0
WORKDIR /usr/local/src/nginx-1.19.0

# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio  --with-http_ssl_module  --with-http_realip_module    --with-http_addition_module    --with-http_xslt_module   --with-http_image_filter_module    --with-http_geoip_module  --with-http_sub_module  --with-http_dav_module --with-http_flv_module    --with-http_mp4_module --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_auth_request_module  --with-http_random_index_module   --with-http_secure_link_module   --with-http_degradation_module   --with-http_stub_status_module && make && make install

EXPOSE 80
####################Dockerfile end ##############

注意:这里原来用的是1.13的版本,然后centos和nginx不符合,折腾了一晚上,发现还是版本问题,改成1.9之后秒过

docker history cent-ngx2 

# 手动启动一下
docker run -d --name ng1 cent-ngx2 /usr/local/nginx/sbin/nginx -g "daemon off;"

# 重新编辑一个  这里用了CMD表示默认执行的cmd
####################Dockerfile begin ##############
# base image
FROM cent-ngx2:latest 

ENV PATH /usr/local/nginx/sbin:$PATH
CMD ["/bin/bash", "-c", "nginx -g 'daemon off;'"]
####################Dockerfile end ##############
docker build -t nginx:v2. 
docker run -d --name ng2 nginx:v2 

# 继续重新编辑一个  这里用了entrypoing,表示这个nginx的主业不能被随便执行命令的时候修改  
####################Dockerfile begin ##############
# base image
FROM cent-ngx2:latest 
ENV PATH /usr/local/nginx/sbin:$PATH
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
####################Dockerfile end ##############

docker build -t nginx:v3 .
docker run -d --name ng3 nginx:v3

7、SpringBoot打包方法

1、手动docker启动
1.1 新建一个springBoot项目
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springdocker</groupId>
    <artifactId>dockersty</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dockersty</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  • application.properties 申明端口8090
#启动: nohup java   -Dspring.profiles.active=dev -jar synchronius-1.0.jar >  log.erp 2>&1 &
server.port: 8090
server.compression.enabled: true

  • 写一个controller

@Controller
public class TestController {
    @GetMapping("/test")
    public ModelAndView index() throws UnknownHostException {
        ModelAndView modelAndView = new ModelAndView();
        InetAddress addr = InetAddress.getLocalHost();
        String ip=addr.getHostAddress().toString(); //获取本机ip
        modelAndView.addObject("serverIp",ip);
        modelAndView.setViewName("index");

        return modelAndView;
    }
}
  • 运行,没有任何问题
1.2 docker部署项目
  1. 服务器编写Dockerfile

    FROM openjdk:8
    ADD ["target/dockersty-0.0.1-SNAPSHOT.jar", "web.jar"]
    EXPOSE 8090
    ENTRYPOINT ["java","-jar","/web.jar"]
    
  2. SpringBoot工程打包(maven里点击package)

  3. 将target中生成的压缩包dockersty-0.0.1-SNAPSHOT.jar包上传到服务器上Dockerfile文件所在路径下的 target

  4. docker build -t dockersty .

  5. docker run -d -p 8090:8090 --name doc dockersty

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值