Docker容器简用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker容器简用相关的知识,希望对你有一定的参考价值。
一、安装
①RedHat/CentOS下的docker安装
RedHat/CentOS必须要6.6版本以上,或者7.x才能安装docker,建议在RedHat/CentOS 7上使用docker,因为RedHat/CentOS 7的内核升级到了kernel 3.10,对lxc容器支持更好。
# yum -y install docker # systemctl start docker
②Ubuntu下docker安装
# sudo apt-get install docker.io # service docker.io start
二、查看docker版本
# docker version Client: Version: 1.8.2-el7.centos API version: 1.20 Package Version: docker-1.8.2-10.el7.centos.x86_64 Go version: go1.4.2 Git commit: a01dc02/1.8.2 Built: OS/Arch: linux/amd64 Server: Version: 1.8.2-el7.centos API version: 1.20 Package Version: Go version: go1.4.2 Git commit: a01dc02/1.8.2 Built: OS/Arch: linux/amd64
三、查看docker安装的相关信息
# docker info Containers: 1 Images: 6 Storage Driver: devicemapper Pool Name: docker-253:0-35256846-pool Pool Blocksize: 65.54 kB Backing Filesystem: xfs Data file: /dev/loop0 Metadata file: /dev/loop1 Data Space Used: 2.052 GB Data Space Total: 107.4 GB Data Space Available: 14.88 GB Metadata Space Used: 1.774 MB Metadata Space Total: 2.147 GB Metadata Space Available: 2.146 GB Udev Sync Supported: true Deferred Removal Enabled: false Data loop file: /var/lib/docker/devicemapper/devicemapper/data Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata Library Version: 1.02.107-RHEL7 (2015-10-14) Execution Driver: native-0.2 Logging Driver: json-file Kernel Version: 3.10.0-229.el7.x86_64 Operating System: CentOS Linux 7 (Core) CPUs: 1 Total Memory: 475.7 MiB Name: docker ID: 22N4:5F3A:UW62:O5MQ:LVV2:RLQY:3YFU:AABC:DJFC:3HNV:UG3E:7OEK
四、列出、搜索镜像
# docker images ##列出本地已有的镜像 # docker search cenots ##从默认镜像仓库搜索镜像
注:没有/分隔前缀的表示官方镜像,尽量选择官方镜像或者下载量较高的镜像使用
五、下载镜像并查看
①下载Ubuntu15.10
# docker pull ubuntu:15.10 3e0c71ada2db: Download complete 6cd64557b0fd: Download complete b53bfb363b42: Download complete 7644a86243c4: Download complete Status: Downloaded newer image for docker.io/ubuntu:15.10
②下载CentOS7
# docker pull centos:7 60e65a8e4030: Download complete 47d44cb6f252: Download complete 838c1c5c4f83: Download complete 5764f0a31317: Download complete Status: Downloaded newer image for docker.io/centos:7
③查看镜像
# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 储存库 标签 镜像ID 创建时间 大小 docker.io/centos 7 60e65a8e4030 2 weeks ago 196.6 MB docker.io/ubuntu 15.10 3e0c71ada2db 4 weeks ago 133.5 MB
六、启动一个docker容器
# docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
主要选项:
-d : 以后台进行方式运行容器
-t : 提供一个伪终端
-i : 提供交互输入,一般与“-t”一起使用,如只提供“-i”选项,则容器启动后无法退出
-v : 映射一个volume给容器,如: -p /data/www:/var/www/html
-p : 将容器的端口映射给宿主机,如: -p 8080:80
实例1:
# docker run -it centos:7 /bin/bash ##以交互方式启动一个容器 [[email protected] /]# ls anaconda-post.log dev home lib64 media opt root sbin sys usr bin etc lib lost+found mnt proc run srv tmp var [[email protected] /]# exit exit
# docker commit -m "centos" -a "zhi" 12984bf5e533 zhi/centos:v1 016e8163a8b0dcc93c7ea651bb8e6f9490a1f4321e2c2df0be6d1b6b08c12ad3
注:commit命令用来将容器转化为镜像,-m参数用来指定提交的说明信息;
-a参数用来指定用户信息;12984bf5e533指容器的ID;
zhi/centos:v1指定目标镜像的用户名、仓库名及tag信息。创建成功后会返回该镜像的ID信息。
# docker images ##列出镜像列表 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE zhi/centos v1 016e8163a8b About a minute ago 196.6 MB docker.io/centos 7 60e65a8e4030 4 weeks ago 196.6 MB docker.io/ubuntu 15.10 3e0c71ada2db 7 weeks ago 133.5 MB # docker run -it zhi/centos:v1 [[email protected] /]# uname Linux
实例2:
# docker run -it ubuntu:15.10 [email protected]:/# apt-get update [email protected]:/# sudo apt-get -y install nginx [email protected]:/# nginx -v nginx version: nginx/1.9.3 (Ubuntu) [email protected]:/# exit exit
# docker ps ##查看当前运行的容器 # docker ps -a ##查看所有容器,包括已停止容器 # docker commit -m "Nginx in Ubuntu" -a "zhi" c340d2ffb0a0 zhi/ubuntu-nginx:v1 ceb3f407b6c4959514d72a420e4c0d3ade47d6b401eb6d2b5cbfd51f58918918 # docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE zhi/ubuntu-nginx v1 ceb3f407b6c4 53 seconds ago 237.7 MB
# docker run -it zhi/ubuntu-nginx:v1
[email protected]:/# nginx -v
nginx version: nginx/1.9.3 (Ubuntu)
六、推送镜像到 Docker Hub
# docker login ##创建docker hub帐号(也可网页注册) Username: wangzhijian Password: Email: [email protected] WARNING: login credentials saved in /root/.docker/config.json Account created. Please use the confirmation link we sent to your e-mail to activate it. # docker login ##进行登录 Username (wangzhijian): wangzhijian WARNING: login credentials saved in /root/.docker/config.json Login Succeeded # docker push zhi/ubuntu-nginx ##推送镜像到 Docker Hub # docker rmi user/ubuntu:docker ##移除镜像 Untagged: user/ubuntu:docker Deleted: 6373a9f4d5704ea80ad7d1d93e98f1094341b038ef266ef0ba332c5878f28ded Deleted: cfeb58b1dc4e657387dc0495f651e0771b6079870f259548f86f01d77652a187
七、使用 Dockerfile 构建镜像
①编写dockerfile文件
# mkdir docker # cd docker/ # touch dockerfile # vi dockerfile # This is a comment FROM ubuntu MAINTAINER zhi&j <[email protected]> RUN apt-get update && apt-get install -y nginx
注:
第一行为注释同时每一个指令的前缀都必须是大写的;
第二行FROM告诉Docker所使用的镜像源;
第三行使用 MAINTAINER 指令来指定谁在维护这个新镜像;
第四行指定了两个 RUN 指令,RUN 指令在镜像内执行一条命令,这里更新
APT缓存并安装nginx
②通过 docker build 命令来构建一个镜像
# docker build -t wangzhijian/ubuntu:one . Sending build context to Docker daemon 2.048 kB Step 0 : FROM ubuntu ---> d55e68e6cc9c Step 1 : MAINTAINER zhi&j <[email protected]> ---> Running in a8e92a582884 ---> cfeb58b1dc4e Removing intermediate container a8e92a582884 Step 2 : RUN apt-get update && apt-get install -y nginx ---> Running in aeba1f533058
解释:
-t标示属于wangzhijian,镜像名称为ubuntu,标签是one,"."指定dockerfile所在
八、设置镜像标签
# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE <none> <none> 6373a9f4d570 About an hour ago 227.6 MB # docker tag 6373a9f4d570 user/ubuntu:docker # docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE user/ubuntu docker 6373a9f4d570 About an hour ago 227.6 MB
Image Digests
内容定位标示符叫做 digest。只要用于生成镜像的镜像源不更改,digests 值就是可以预料的。使用 --digests 标识来列出镜像 digests 的值
# docker images --digests | head REPOSITORY TAG DIGEST IMAGE ID CREATED VIRTUAL SIZE docker.io/centos latest <none> 14dab3d40372 2 weeks ago 194.7 MB docker.io/ubuntu latest <none> d55e68e6cc9c 3 weeks ago 187.9 MB
九、在Docker中运行Reids服务
# mkdir docker # cd docker/ # touch dockerfile # vi dockerfile FROM ubuntu RUN apt-get update RUN apt-get -y install redis-server EXPOSE 6379 ENTRYPOINT ["/usr/bin/redis-server"] # docker build -t redis/ubuntu .
本文出自 “记事本” 博客,请务必保留此出处http://wangzhijian.blog.51cto.com/6427016/1750858
以上是关于Docker容器简用的主要内容,如果未能解决你的问题,请参考以下文章