docker基础学习笔记

容器这四点一定要懂:注册服务器、仓库、镜像、容器
 
他们的范围依次缩小
注册服务器可以有多个仓库,仓库里面可以有多个镜像,每个镜像可以生成多个容器。
 
docker属于C/S结构
支持三种连接方式:
unix:///var/run/docker.sock
tcp://host:port
fd://socketfd
 
docker在Centos7安装
1、yum安装docker
     yum -y install docker
 
2、安装结束后,启动docker
     systemctl start docker #启动
     systemctl restart docker #重启
     systemctl stop docker #停止
     systemctl status docker #查看状态
     systemctl enable docker #自启动
     systemctl disable docker #禁止自启动
     .service可以省略。
 
3、查看docker版本
     docker version
 
4、查看当前有的镜像
     docker images
5、查看当前启动的容器
     docker ps
6、为了后面的需要,我们这里下载个ubuntu的镜像:
     docker search ubuntu
     docker pull ubuntu
 
7、# 查看所有可用镜像
     docker images -a
 
8、查看进程
     ps -ef | grep docker
 
9、下面是容器创建时的一些配置,按需添加。
 
     运行相关:
 
      -D, --debug=false
      -e,--exec-driver="native"
      -p,--pidfile="/var/run/docker.pid"
     服务器相关:
 
      -G,--group="docker"
      -H,--host=[]
      --tls=false
     RemoteAPI相关:
 
    --api-enable-cors=false
     存储相关:
 
      -S,--storage-driver=""
      --selinux-enabled=false
      --storage-opt=[]
     网络设置相关:
 
      -b,--bridge="" 设置自定义网桥
      --bip=""
      --dns=[]
      --ip=0.0.0.0
 
 
10、#只运行一次命令
     docker run ubuntu echo 'hello world' 运行一个新的容器,并执行命令echo
 
     #创建并运行容器,然后进入容器
     docker run -i -t  --name test ubuntu /bin/bash          以交互式终端运行一个新的容器,镜像是ubuntu,使用bash,容器别名test
 
 
     docker ps     查看正在运行的容器
     docker ps -a  查看所有容器
     docker ps -l  查看最近一次运行的容器
 
 11、容器操作:
     docker create 容器名或者容器ID 创建容器
     docker start [-i] 容器名       启动容器
     docker run 容器名或者容器ID    运行容器,相当于docker create + docker start
     docker attach 容器名或者容器ID 进入容器的命令行
     docker stop 容器名                             停止容器
     docker rm 容器名                               删除容器
 
     docker top 容器名          查看WEB应用程序容器的进程
     docker inspect 容器名 查看Docker的底层信息
     删除容器时,容器必须是停止状态,否则会报错误。
 
12、后台运行任务
             docker run -d --name test2 ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done;"
 
13、Nginx部署示例
            # 创建映射端口为80的交互式界面:
            docker run -p 80 --name web -i -t ubuntu /bin/bash
 
            # 第一次使用更新源
             apt-get update
 
             # 安装nginx
             apt-get install nginx
 
              # 安装vim
              apt-get install vim
 
               whereis nginx
               nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
 
               vim /etc/nginx/conf.d/localhost.conf
               发现配置文件在/etc/nginx/conf.d下面:
 
               conf.d/localhost.conf
 
server {
    listen       80;
    server_name  localhost;
 
    location / {
        root   /var/www/;
        index  index.html index.htm;
    }   
 
}
             新建个目录:
 
             mkdir -p /var/www/
 
   vim /var/www/index.html
     内容随便写。
 
# 启动nginx
nginx
使用Crtl+P(即Crtl+shift+p)退出容器,并后台运行。
查看:
 
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:32769
 
[root@localhost ~]# docker top web
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                12123               12113               0                   07:14               pts/2               00:00:00            /bin/bash
root                12159               12123               0                   07:14               ?                   00:00:00            nginx: master process nginx
33                  12160               12159               0                   07:14               ?                   00:00:00            nginx: worker process
 
[root@localhost ~]# curl http://127.0.0.1:32769
正常的话会显示网页内容。
 
如果exit退出了容器,想开启nginx服务,还可以:
 
docker start web
docker exec web nginx
 
14、从容器生成镜像
 
        假设有一容器2c74d574293f,可以使用commit命令生成镜像:
 
        docker commit -m "create images" -a "52fhy"  2c74d574293f  52fhy/test:v1
        -m 加一些改动信息,-a 指定作者相关信息,2c74d这一串为旧容器id,再后面为新镜像的名字。
 
15、使用Dockerfile创建镜像
 
       Dockerfile用来创建一个自定义的image,包含了用户指定的软件依赖等。Dockerfile文件可以用docker build命令生成一个新的镜像。
 
       Dockerfile文件示例:
 
       FROM daocloud.io/centos:7
 
     # Install Nginx.
     # WORKDIR /etc/yum.repos.d/
     RUN \
        yum update -y && \
        yum install -y wget && \
     #   wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
        wget -O /etc/yum.repos.d/CentOs-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo && \
        yum makecache && \
        yum update -y && \
        yum install -y vim && \
        yum install -y nginx && \
        yum install -y net-tools && \
        echo "daemon off;" >> /etc/nginx/nginx.conf && \
        echo "master_process  off;" >> /etc/nginx/nginx.conf
        # Define mountable directories.
        VOLUME ["/usr/share/nginx", "/etc/nginx/conf.d", "/var/log/nginx"]
        # Define working directory.
        WORKDIR /etc/nginx
        # Define default command.
        CMD ["/usr/sbin/nginx"]
        # Expose ports.
        EXPOSE 80
        EXPOSE 443
     运行如下命令:
 
     docker build -t nginx:v1 .
     进行镜像构建。成功后,从生成的镜像运行容器:
 
     docker run -d -p 8090:80 nginx:v1
 
16.Dockerfile文件指令
     构建镜像是docker使用的关键点,推荐采用DockerFile方式构建镜像,关键要掌握DockerFile文件编辑的步骤和注意点.
     DockerFile中每条指令代表类似提交一个镜像层,由多个镜像层构成我们想要的镜像.
 
     一般的Dockerfile文件第一条指令都是FROM,指定一个已经存在的镜像,后续指令都将基于该镜像进行,这个镜像被称为基础镜像.
     MAINTAINER指令告诉Docker该镜像作者以及电子邮件地址.
     RUN 指令会在当前镜像中运行指定的命令.
     EXPOSE 告诉Docker该容器内的应用程序将会使用容器的指定端口
     ENV 用来设置环境变量,可以表示地址或者时间
     CMD    通常用作当容器启动时要运行的指令,与docker通过run运行指定镜像时特别相似.
     WORKDIR 在从镜像创建一个新容器的时候,在容器内部设置一个工作目录
     USER 指定该镜像会以什么样的用户运行
     VOLUME 向基于镜像创建的容器添加卷.一个卷是可以存在于一个或者多个容器内的特定的目录,这个目录可以绕过联合文件系统
     ADD     用来将构建环境下的文件和目录复制到镜像中.
     ONBUILD  为镜像添加触发器.当一个镜像被用作其他镜像的基础竟像时,该镜像中的触发器将被执行.
 
     run运行镜像容器中-d代表要以分离的方式独立运行在后台,适合web端
              -p表示端口号映射到物理机上的情况,前面代表宿主机,后面代表容器端口.还可以在前面添加特定的IP地址.(-p 172.16.201.112 : 8080 : 80)
 

17.卸载docker
     可以使用yum命令卸载docker。
 
     查询已经安装的docker包:
     $ yum list installed | grep docker
 
     docker-engine.x86_64     1.7.1-0.1.el7@/docker-engine-1.7.1-0.1.el7.x86_64
     删除包
     $ sudo yum -y remove docker-engine.x86_64
     这个命令会卸载docker,但是不会删除镜像,容器,卷或其他用户创建的配置文件。
 
     删除所有镜像容器和卷
     $ rm -rf /var/lib/docker
     自行删除用户自己创建的配置文件。
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2017-09-23 15:20  麻辣香蕉  阅读(138)  评论(0编辑  收藏  举报