Docker入门

Docker简介

Docker是一个能够把开发的应用程序自动部署到容器的开源引擎。该引擎的目标是为了提供一个轻量、快速的环境,能够运行开发者的程序,并方便高效的将程序从开发者的PC部署到测试环境,然后再到生产环境。Docker的目标之一就是为了缩短代码从开发、测试到部署、上线运行的周期,让你的应用程序具备可移植性,易于构建,并易于协作。

Docker程序环境:

环境配置文件:
    /etc/sysconfig/docker-network
    /etc/sysconfig/docker-storage
    /etc/sysconfig/docker
Unit File:
    /usr/lib/systemd/system/docker.service
Docker Registry配置文件:
    /etc/containers/registries.conf

容器的简单操作

安装Docker

配置yum源仓库(此处使用的aliyun的yum仓库)
[root@docker ~]# cat /etc/yum.repos.d/server.repo
[base]
name=danran
baseurl=https://mirrors.aliyun.com/centos/7.4.1708/os/x86_64/
gpgcheck=0

[epel]
name=opel
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/7.4.1708/extras/x86_64/
gpgcheck=0

[updates]
name=update
baseurl=https://mirrors.aliyun.com/centos/7.4.1708/updates/x86_64/
gpgcheck=0

安装docker
    yum -y install docker

启动docker
    systemctl start docker
    systemctl status docker

docker pull 下载镜像

从镜像仓库下载镜像,docker pull不指定镜像仓库,默认从docker官方镜像仓库docker hub下载

由于国内网络从docker hub下载镜像速度及其慢,经常出现连接超时的情况,建议配置docker镜像下载加速器
此处使用aliyun的加速器,加速器配置见链接    
     https://cr.console.aliyun.com/#/accelerator

配置好加速器之后,再次使用pull从官方镜像仓库下载镜像
    [root@danran ~]# docker pull centos:latest
    Trying to pull repository docker.io/library/centos ... 
    latest: Pulling from docker.io/library/centos
    af4b0a2388c6: Pull complete 
    Digest: sha256:6247c7082d4c86c61b00f7f2e3edbf7f072a24aa8edc28b5b68b3de3101bc1ce

docker images 显示docker镜像

[root@danran ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              f6e427c148a7        3 days ago          1.146 MB
docker.io/centos    latest              ff426288ea90        7 weeks ago         207.2 MB

docker run 运行容器

Options:
    -name:指定容器的name
    -d: 后台运行容器,并返回容器ID;
    -i: 以交互模式运行容器,通常与 -t 同时使用;
    -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
    -P, --publish-all=false    指定容器暴露的端口    
    -p, --publish=[]           指定容器暴露的端口   
    -h, --hostname=""          指定容器的主机名 
    --rm=false                 指定容器停止后自动删除容器(不支持以docker run -d启动的容器)  
    -v, --volume=[]            给容器挂载存储卷,挂载到容器的某个目录    
    --volumes-from=[]          给容器挂载其他容器上的卷,挂载到容器的某个目录
    --device=[]                添加主机设备给容器,相当于设备直通    
    --dns=[]                   指定容器的dns服务器    
    --dns-search=[]            指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件    
    --link=[]                  指定容器间的关联,使用其他容器的IP、env等信息
    --net="bridge"             容器网络设置:  
            bridge 使用docker daemon指定的网桥       
            host    //容器使用主机的网络    
            container:NAME_or_ID  >//使用其他容器的网路,共享IP和PORT等网络资源    
            none 容器使用自己的网络(类似--net=bridge),但是不进行配置   

    -u, --user=""              指定容器的用户 
    -a, --attach=[]            登录容器(必须是以docker run -d启动的容器)  
    -w, --workdir=""           指定容器的工作目录   
    -c, --cpu-shares=0         设置容器CPU权重,在CPU共享场景使用    
    -e, --env=[]               指定环境变量,容器中可以使用该环境变量    
    -m, --memory=""            指定容器的内存上限    
    --cap-add=[]               添加权限,权限清单详见:http://linux.die.net/man/7/capabilities    
    --cap-drop=[]              删除权限,权限清单详见:http://linux.die.net/man/7/capabilities    
    --cidfile=""               运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法    
    --cpuset=""                设置容器可以使用哪些CPU,此参数可以用来容器独占CPU    
    --entrypoint=""            覆盖image的入口点    
    --env-file=[]              指定环境变量文件,文件格式为每行一个环境变量    
    --expose=[]                指定容器暴露的端口,即修改镜像的暴露端口        
    --lxc-conf=[]              指定容器的配置文件,只有在指定--exec-driver=lxc时使用    
    --privileged=false         指定容器是否为特权容器,特权容器拥有所有的capabilities    
    --restart="no"             指定容器停止后的重启策略:  
              no:容器退出时不重启    
              on-failure:容器故障退出(返回值非零)时重启   
              always:容器退出时总是重启    
    --sig-proxy=true           设置由代理接受并处理信号,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理   
   

运行容器
    [root@danran ~]# docker run --name bbox1 -it docker.io/busybox:latest
    / # ls
    bin   dev   etc   home  proc  root  run   sys   tmp   usr   var

运行容器并执行指定的命令
    运行bbox3容器并执行ifconfig命令自动停止容器
    [root@danran ~]# docker run --name bbox3 -it docker.io/busybox:latest ifconfig
    eth0     Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
	         inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
	         inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link
	         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
	         RX packets:1 errors:0 dropped:0 overruns:0 frame:0
	         TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
	         collisions:0 txqueuelen:0 
	         RX bytes:90 (90.0 B)  TX bytes:90 (90.0 B)

    lo       Link encap:Local Loopback  
	         inet addr:127.0.0.1  Mask:255.0.0.0
	         inet6 addr: ::1/128 Scope:Host
	         UP LOOPBACK RUNNING  MTU:65536  Metric:1
	         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
	         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
	         collisions:0 txqueuelen:1 
	         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

容器运行后直接删除
    [root@danran ~]# docker run --rm --name bbox3 docker.io/busybox:latest ifconfig
    eth0     Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
	         inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
	         inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link
	         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
	         RX packets:1 errors:0 dropped:0 overruns:0 frame:0
	         TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
	         collisions:0 txqueuelen:0 
	         RX bytes:90 (90.0 B)  TX bytes:90 (90.0 B)

    lo       Link encap:Local Loopback  
	         inet addr:127.0.0.1  Mask:255.0.0.0
	         inet6 addr: ::1/128 Scope:Host
	         UP LOOPBACK RUNNING  MTU:65536  Metric:1
	         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
	         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
	         collisions:0 txqueuelen:1 
	         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

    [root@danran ~]# docker ps -a
    CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
    65ee0d02f313        docker.io/busybox:latest   "sh"                43 minutes ago      Up 9 minutes  

docker ps 显示容器进程

Options:
    -a, --all             Show all containers (default shows just running)              显示所有的进程,包括停止的进程
    -f, --filter value    Filter output based on conditions provided (default [])
          --format string   Pretty-print containers using a Go template
          --help            Print usage
    -n, --last int        Show n last created containers (includes all states) (default -1)
    -l, --latest          Show the latest created container (includes all states)
    --no-trunc        Don't truncate output
    -q, --quiet           Only display numeric IDs
    -s, --size            Display total file sizes

显示所有的容器进程	
    [root@danran ~]# docker ps -a
    CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                      PORTS               NAMES
    65ee0d02f313        docker.io/busybox:latest   "sh"                21 minutes ago      Exited (0) 21 minutes ago  

仅显示正在运行的容器
    [root@danran ~]# docker ps 
    CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
    65ee0d02f313        docker.io/busybox:latest   "sh"                26 minutes ago      Up 25 seconds              

docker start/restart 启动容器

启动容器,容器后台运行
    [root@danran ~]# docker start bbox1
    bbox1
    [root@danran ~]# docker ps
    CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
    65ee0d02f313        docker.io/busybox:latest   "sh"                34 minutes ago      Up 9 seconds 

启动容器,保持终端前台运行
    [root@danran ~]# docker start -ai bbox1
    / # 
    / # ls
    bin   dev   etc   home  proc  root  run   sys   tmp   usr   var
    / # exit

docker rm 删除容器

docker rm
    Usage:	docker rm [OPTIONS] CONTAINER [CONTAINER...]
        Options:
            -f, --force     Force the removal of a running container (uses SIGKILL)
            --help      Print usage
            -l, --link      Remove the specified link
            -v, --volumes   Remove the volumes associated with the container   

显示当前的所有容器进程
    [root@danran ~]# docker ps -a
    CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                     PORTS               NAMES
    bd5546d48e92        docker.io/busybox:latest   "ifconfig"          8 minutes ago       Exited (0) 8 minutes ago                       bbox3
    e133038cb5fc        docker.io/busybox:latest   "ipconfig"          9 minutes ago       Created                                        bbox2
    65ee0d02f313        docker.io/busybox:latest   "sh"                39 minutes ago      Up 5 minutes                                   bbox1

删除bbox3容器
    [root@danran ~]# docker rm bbox3
    bbox3

再次显示所有的容器进程,bbox3容器已删除
    [root@danran ~]# docker ps -a
    CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
    e133038cb5fc        docker.io/busybox:latest   "ipconfig"          9 minutes ago       Created                                 bbox2
    65ee0d02f313        docker.io/busybox:latest   "sh"                40 minutes ago      Up 5 minutes   

docker rmi 删除指定镜像

显示系统上的docker镜像
    [root@danran ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    docker.io/busybox   latest              f6e427c148a7        3 days ago          1.146 MB
    docker.io/centos    latest              ff426288ea90        7 weeks ago         207.2 MB

删除docker.io/busybox:latest镜像
    [root@danran ~]# docker rmi docker.io/busybox:latest
   
再次显示本地的docker镜像
    [root@danran ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    docker.io/centos    latest              ff426288ea90        7 weeks ago         207.2 MB

docker inspect 探测容器内部的详细信息

探测bbox1容器内部的详细信息
    root@danran ~]# docker inspect bbox1

docker save 打包镜像文件

docker save
    Usage:docker save [OPTIONS] IMAGE [IMAGE...]
    Options:
        --help            Print usage
        -o, --output string   Write to a file, instead of STDOUT

将官方仓库的centos镜像打包为/root/centos.tar
    [root@danran ~]# docker save -o /root/centos.tar centos:latest
    [root@danran ~]# ls
    anaconda-ks.cfg  centos.tar

docker load 从镜像打包文件载入镜像

装入镜像打包文件
docker load 
    Usage:	docker load [OPTIONS]
    Options:
        --help           Print usage
        -i, --input string   Read from tar archive file, instead of STDIN
        -q, --quiet          Suppress the load output
		
将centos.tar镜像打包文件载入系统		
    [root@danran ~]# docker load -i /root/centos.tar 
    Loaded image: centos:latest		
    [root@danran ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              latest              ff426288ea90        8 weeks ago         207.2 MB

docker tag 镜像打标签

docker tag  给镜像重打标签
    Usage:	docker tag IMAGE[:TAG] IMAGE[:TAG]

将centos:latest标签重打为centos:1.0
    [root@danran ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              1.0                 ff426288ea90        8 weeks ago         207.2 MB
    centos              latest              ff426288ea90        8 weeks ago         207.2 MB

将docker.io上的busybox镜像设置标签为registry.cn-hangzhou.aliyuncs.com/jevonwei/test:bbox-0.1
    [root@danran ~]# docker tag docker.io/busybox:latest registry.cn-hangzhou.aliyuncs.com/jevonwei/test:bbox-0.1
    [root@danran ~]# docker images
    REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
    docker.io/busybox                                 latest              f6e427c148a7        5 days ago          1.146 MB
    registry.cn-hangzhou.aliyuncs.com/jevonwei/test   bbox-0.1            f6e427c148a7        5 days ago          1.146 MB

docker push 仓库推送镜像到镜像

docker push 
    Usage:	docker push [OPTIONS] NAME[:TAG]
    Options:
        --disable-content-trust   Skip image verification (default true)

将指定的镜像push推送到镜像仓库,可在镜像仓库中查看到push的镜像
    [root@danran ~]# docker push registry.cn-hangzhou.aliyuncs.com/jevonwei/test:bbox-0.1

docker commit 提交镜像

基于运行中的容器制作提交镜像
    docker commit
        Usage:	docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
        Options:
            -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")    标记字符串信息
            -c, --change value     Apply Dockerfile instruction to the created image (default [])    修改镜像默认的运行命令
            --help      Print usage
            -m, --message string   Commit message
            -p, --pause            Pause container during commit (default true)       暂停容器

基于运行中的bbox1容器制作镜像

运行一个bbox1容器,修改容器并在容器中新建目录 /date/web/html
    [root@danran ~]# docker run --name bbox1 -it docker.io/busybox:latest
    / # mkdir /date/web/html -pv
    created directory: '/date/'
    created directory: '/date/web/'
    created directory: '/date/web/html'
    / # vi /date/web/html/index.html
    / # cat /date/web/html/index.html 
    <h1> Test Page @docker container</h1>
    / # 

确认bbox1容器正在运行
    [root@danran ~]# docker ps
    CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
    5cb5ae8125c5        docker.io/busybox:latest   "sh"                6 minutes ago       Up 6 minutes                            bbox1

根据修改过的bbox1容器制作镜像registry.cn-hangzhou.aliyuncs.com/jevonwei/test:data,镜像标签为data
    [root@danran ~]# docker commit -p -a "jevonwei" bbox1 registry.cn-hangzhou.aliyuncs.com/jevonwei/test:data
    sha256:a9b7ac7922957b192bae854cf8ffd4796e590559ec999a100d94caf2a1b5168c
    [root@danran ~]# docker images
    REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
    registry.cn-hangzhou.aliyuncs.com/jevonwei/test   data                a9b7ac792295        9 seconds ago       1.146 MB
	
将镜像registry.cn-hangzhou.aliyuncs.com/jevonwei/test:data推送到远程仓库registry.cn-hangzhou.aliyuncs.com/jevonwei
    [root@danran ~]# docker push registry.cn-hangzhou.aliyuncs.com/jevonwei/test:data

doker commit 提交镜像并修改默认运行命令

由bbox1容器提交新的镜像,并修改默认运行命令为httpd,-f为指定httpd命令为前台运行,-h指定家目录为/data/web/html
    [root@danran ~]# docker commit -p -a "jevonwei"  -c 'CMD ["httpd","-f","-h /data/web/html"]' bbox1 registry.cn-hangzhou.aliyuncs.com/jevonwei/test:httpd
    sha256:5da3c5a7fcf8b6466f9a60d3b770c5980f9f4e7f92adfbda63a1ef82b2200eb9

    [root@danran ~]# docker images
    REPOSITORY                                        TAG                 IMAGE ID            CREATED              SIZE
    registry.cn-hangzhou.aliyuncs.com/jevonwei/test   httpd               5da3c5a7fcf8        About a minute ago   1.146 MB

基于新的镜像运行容器
    [root@danran ~]# docker run -it --name httpd registry.cn-hangzhou.aliyuncs.com/jevonwei/test:httpd

docker exec

在运行的容器中额外执行指定的命令

docker exec
    Usage:	docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
    Run a command in a running container
        -d, --detach         Detached mode: run command in the background
        --detach-keys        Override the key sequence for detaching a container
        --help               Print usage
        -i, --interactive    Keep STDIN open even if not attached
        --privileged         Give extended privileges to the command
        -t, --tty            Allocate a pseudo-TTY
        -u, --user           Username or UID (format: <name|uid>[:<group|gid>])

运行ls命令查看bbox1容器中/data目录下的文件
    [root@danran ~]# docker exec bbox1 ls /data
    web
posted @ 2018-03-07 20:16  JevonWei  阅读(264)  评论(0编辑  收藏  举报