Contents
一.Docker基础入门实战
1.系统环境
~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) ~]# uname -r 3.10.0-327.el7.x86_64
1.1安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum list docker-ce --showduplicates | sort -r yum install -y docker-ce-17.06.1.ce systemctl start docker ps aux | grep docker docker run hello-world1. 安装依赖包: yum install -y yum-utils device-mapper-persistent-data lvm2 2 . 添加yum 源仓库 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # 如果下载不了,可以使用阿里云的镜像: # yum config-manager -add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 3. 清楚yum 缓存,重新加载yum源, 这一步文档上没有,但要执行一下,不然会报错。 yum clean all yum makecache 4. 下载安装docker-ce yum install docker-ce 5. 注册自己daocloud账号,可以直接使用github账号。 网址: http://www.daocloud.io/ 登陆进去控制台后,右上角点击 加速器: 6. 配置docker hub加速器 ,直接复制命令到对应的系统上执行操作即可。这里直接复制linux的命令执行: curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io 然后重启一下docker即可。 systemctl restart docker
#安装docker ~]# yum install -y docker #设置开机自启动docker ~]# systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. #更改docker仓库不从docker官方镜像下载速度慢不稳定 https://cr.console.aliyun.com ~]# vim /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd-current --registry-mirror=https://9fjlbhg9.mirror.aliyuncs.com \ ##或者 https://pmf0owpo.mirror.aliyuncs.com ~]# systemctl daemon-reload ~]# systemctl start docker ~]# systemctl status docker
1.2Docker基础命令
Usage: docker COMMAND A self-sufficient runtime for containers Options: #客户端配置文件的位置 --config string Location of client config files (default "/root/.docker") #启用Debug调试模式 -D, --debug Enable debug mode #获取帮助信息 --help Print usage #守护进程的套接字(Socket)连接 -H, --host list Daemon socket(s) to connect to (default []) #log等级 -l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info") --tls Use TLS; implied by --tlsverify #信任证书签名CA --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") #TLS证书文件路径 --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") #TLS密钥文件路径 --tlskey string Path to TLS key file (default "/root/.docker/key.pem") #使用TLS验证远程 --tlsverify Use TLS and verify the remote #打印版本信息并退出 -v, --version Print version information and quit Management Commands: container Manage containers #当前shell下attach连接指定运行镜像 image Manage images #管理镜像 network Manage networks #管理网络 node Manage Swarm nodes #节点管理 plugin Manage plugins #管理插件 secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker volume Manage volumes Commands: attach Attach to a running container #进入一个以运行的容器 build Build an image from a Dockerfile #通过Dockerfile定制镜像 commit Create a new image from a container's changes #提交当前容器为新的镜像 cp Copy files/folders between a container and the local filesystem #从容器中拷贝指定文件或者目录到宿主机中 create Create a new container #创建一个新的容器,同run 但不启动容器 diff Inspect changes on a container's filesystem #查看docker容器变化 events Get real time events from the server #从docker服务获取容器实时事件 exec Run a command in a running container #在已存在的容器上运行命令 export Export a container's filesystem as a tar archive #导出容器的内容流作为一个tar归档文件(对应import) history Show the history of an image #展示一个镜像形成历史 images List images #列出系统当前镜像 import Import the contents from a tarball to create a filesystem image #从tar包中的内容创建一个新的文件系统映像(对应export) info Display system-wide information #显示系统相关信息 inspect Return low-level information on Docker objects #查看容器详细信息 kill Kill one or more running containers #kill指定docker容器 load Load an image from a tar archive or STDIN #从一个tar包中加载一个镜像(对应save) login Log in to a Docker registry #注册或者登陆一个docker源服务器 logout Log out from a Docker registry #从当前Docker registry退出 logs Fetch the logs of a container #输出当前容器日志信息 pause Pause all processes within one or more containers #暂停容器 port List port mappings or a specific mapping for the container #查看映射端口对应的容器内部源端口 ps List containers #列出容器列表 pull Pull an image or a repository from a registry #从docker镜像源服务器拉取指定镜像或者库镜像 push Push an image or a repository to a registry #推送指定镜像或者库镜像至docker源服务器 rename Rename a container #重命名容器 restart Restart one or more containers #重启运行的容器 rm Remove one or more containers #移除一个或者多个容器 rmi Remove one or more images #移除一个或多个镜像(无容器使用该镜像才可以删除,否则需要删除相关容器才可以继续或者-f强制删除) run Run a command in a new container #创建一个新的容器并运行一个命令 save Save one or more images to a tar archive (streamed to STDOUT by default) #保存一个镜像为一个tar包(对应load) search Search the Docker Hub for images #在docker hub中搜索镜像 start Start one or more stopped containers #启动容器 stats Display a live stream of container(s) resource usage statistics #统计容器使用资源 stop Stop one or more running containers #停止容器 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE #给源中镜像打标签 top Display the running processes of a container #查看容器中运行的进程信息 unpause Unpause all processes within one or more containers #取消暂停容器 update Update configuration of one or more containers #更新一个或多个容器的配置 version Show the Docker version information #查看容器版本号 wait Block until one or more containers stop, then print their exit codes #截取容器停止时的退出状态值 Run 'docker COMMAND --help' for more information on a command.
1.3Docker镜像管理
查看镜像的使用方法地址:https://hub.docker.com/explore
1.3.1搜索Docker镜像
[root@node01 ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL(官方) AUTOMATED(自动化)
docker.io docker.io/centos The official build of CentOS. 4419 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 114 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8... 97 [OK]
docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 56 [OK]
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 43
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 42 [OK]
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 31
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 31 [OK]
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 27
docker.io docker.io/centos/python-35-centos7 Platform for building and running Python 3... 25
docker.io docker.io/centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relationa... 21
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 21 [OK]
1.3.2获取Docker镜像
[root@node01 ~]# docker pull alpine Using default tag: latest Trying to pull repository docker.io/library/alpine ... latest: Pulling from docker.io/library/alpine ff3a5c916c92: Pull complete Digest: sha256:e1871801d30885a610511c867de0d6baca7ed4e6a2573d506bbec7fd3b03873f Status: Downloaded newer image for docker.io/alpine:latest #下载nginx镜像 [root@node01 ~]# docker pull nginx
1.3.3查看本机获取的镜像
[root@node01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 649dcb69b782 Less than a second ago 109 MB docker.io/alpine latest 3fd9065eaf02 4 months ago 4.15 MB
1.3.4导出镜像(没有网络本地需要安装)
#docker镜像可以理解为是一个压缩包 [root@node01 ~]# docker save nginx >/tmp/nginx.tar.gz #导入镜像 [root@node01 ~]# docker rmi nginx #删除本地nginx镜像 [root@node01 ~]# docker load </tmp/nginx.tar.gz #导入
docker镜像(相关配置文件,容器启动)的默认存放位置:/var/lib/docker
目录下
更换默认存储目录
~]# vim /usr/lib/systemd/system/docker.service --graph=/opt/docker #更换存储目录 --dns=xxxx #默认docker是采用宿主机的dns,可以采用--dns=xxxx的方式指定
1.4Docker容器管理
1.4.1镜像的启动
#docker命令 run alpine镜像名 sh命令 [root@node01 ~]# docker run alpine sh #没有运行的终端 说明: run=create + start run参数相当于create一个容器再去start #运行终端进入容器操作 [root@node01 ~]# docker run -it alpine sh / # #退出容器而不关闭进程 安装ctrl+p+q 退出不关闭进程 [root@node01 ~]# docker ps #查看docker进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 58d55804b48e alpine "sh" 8 seconds ago Up 6 seconds kind_tesla #查看所有的进程 [root@node01 ~]# docker ps -a
1.4.2删除容器
[root@node01 ~]# docker rm -f 58d55804b48e #强制删除 [root@node01 ~]# docker kill `docker ps -a -q` #停止所有正在运行的容器 [root@node01 ~]# docker rm `docker ps -a -q` #删除所有容器
1.4.3手动指定容器的名字运行
[root@node01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2cd8c0576eeb nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 80/tcp mynginx #查看容器的详细信息 [root@node01 ~]# docker inspect mynginx #容器停止的时候删除进程实验环境使用 [root@node01 ~]# docker run -it --rm --name mynginx nginx #启动一个已经停止的容器 [root@node01 ~]# docker start 403324fc69d5
1.4.4进入一个正在运行的容器
#第一种方法 [root@node01 ~]# docker attach mynginx #通过启动指令来实现的相互的,一处操作都会生效 #第二种方法优点不会对其它用户有影响独立的 [root@node01 ~]# docker exec -it mynginx sh # #第三种方法 使用nsenter命令 [root@node01 ~]# yum install -y util-linux #安装nsenter命令 #获取docker本身的pid [root@node01 ~]# docker inspect --format "{{.State.Pid}}" 403324fc69d5 19995 [root@node01 ~]# nsenter -t `docker inspect --format "{{.State.Pid}}" 403324fc69d5` -m -u -i -n -p [root@403324fc69d5 ~]# #编写脚本快速进入 [root@node01 ~]# vim docker_in.sh #!/bin/sh pid=$(docker inspect --format "{{.State.Pid}}" $1) nsenter -t $pid -m -u -i -n -p #进入容器 [root@node01 ~]# sh docker_in.sh 403324fc69d5 [root@403324fc69d5 ~]#
1.4.5后台运行一个终端容器
[root@node01 ~]# docker run -it -d --name mynginx nginx d41993030d2b24489ad520b46a0e2a2b15b7eab05323535f7083ffa060e2fc48 [root@node01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d41993030d2b nginx "nginx -g 'daemon ..." 10 seconds ago Up 9 seconds 80/tcp mynginx
1.5日志管理
#查看log文件 [root@node01 ~]# docker logs mynginx 172.17.0.1 - - [11/May/2018:07:37:42 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" #实时显示log内容 [root@node01 ~]# docker logs -f mynginx 172.17.0.1 - - [11/May/2018:07:37:42 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 172.17.0.1 - - [11/May/2018:07:38:13 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
如无特殊说明,文章均为本站原创,转载请注明出处
- 转载请注明来源:Docker系列二之基础入门实战
- 本文永久链接地址:https://www.xionghaier.cn/archives/480.html