Docker容器:常用命令实践

发布于 2017-03-14 · 本文总共 8221 字 · 阅读大约需要 24 分钟

1.Docke介绍

Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。 开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机)、bare metal、OpenStack集群和其他的基础应用平台。

Docker通常用于如下场景:

web应用的自动化打包和发布;

自动化测试和持续集成、发布;

在服务型环境中部署和调整数据库或其他的后台应用;

从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境。

1.1 Docker镜像

镜像是一个只读的模板,用来创建Docker容器;

Docker提供了一个简单的机制来创建或更新现有的镜像,用户可以直接从其他人那里下载一个已经做好的镜像来直接使用;

1.2 Docker容器

容器是从镜像创建的运行实例;

可以看做是一个简易版的Linux环境和运行在其中的应用程序;(镜像是只读的)

1.3仓库

集中存放镜像文件的场所;

仓库注册服务器(Register)存放着多个仓库,每个仓库包含了多个镜像,每个镜像有不同的标签;

类比:
Docker仓库–Git
注册服务器–GitHub

2.Docker安装(CentOS7)

CentOS7系统Centos-Extras库已经自带Docker,可以直接安装:

# yum -y install docker

启动Docker服务:

# systemctl start docker

# systemctl status docker

测试运行hello-world

# docker run hello-world

[root@localhost qwq_bak]# docker run hello-world

Hello from Docker!

This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

1. The Docker client contacted the Docker daemon.

2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

3. The Docker daemon created a new container from that image which runs the

executable that produces the output you are currently reading.

4. The Docker daemon streamed that output to the Docker client, which sent it

to your terminal.

To try something more ambitious, you can run an Ubuntu container with:

$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:

https://cloud.docker.com/

For more examples and ideas, visit:

https://docs.docker.com/engine/userguide/

3.镜像

3.1获取镜像

docker pull

# docker pull ubuntu:16.04

docker pull

3.2 列出本地镜像

docker images

# docker images
docker images

3.3 创建

$ docker run learn/tutorial apt-get install -y ping

4 容器

4.1 启动、运行、终止

4.1.1 启动容器

两种方式:

基于镜像新建容器并启动:docker run

将终止状态的容器重启:docker start

# docker run -t -i ubuntu:16.04 /bin/bash

-t 分配一个伪终端并绑定到容器标准输入

-i 让容器的标准输入保持打开

docker run

# docker run ubuntu:16.04 /bin/echo "hello World"

利用docker run创建容器时,其执行步骤为:

1.检查本地是否存在指定镜像,否则下载

2.利用镜像创建并启动一个容器

3.分配文件系统

4.从宿主机网桥接口桥接一个虚拟接口到容器

5.配置IP

6.执行用户指定应用程序

7.执行完毕后终止容器

4.1.2 后台运行 -d

# docker run -d ubuntu:16.04 /bin/sh -c "while true;do echo hello World;sleep 2;done"

docker logs a2130bf7254c

通过docker logs获取容器输出信息

4.1.3 终止容器

docker stop

终止状态的容器可以用docker ps -a命令看到

docker ps -a

4.2 进入容器(-d 容器进入后台)

4.2.1 attach命令:

# docker run -idt ubuntu
# docker ps
# docker attach <docker-name>
docker run -idt
docker attach

这个文件中定义了很多方便使用 Docker 的命令,例如 docker-pid 可以获取某个容器的 PID;而 docker-enter 可以进入容器或直接在容器内执行命令。

# echo $(docker-pid )

# docker-enter

4.3导入导出

4.3.1导出容器:docker export

# docker export ed1a7cb1f5af > ubuntu.tar

4.3.2 导入容器快照为镜像:docker import

#cat ubuntu.tar | docker import ubuntu.tar

也可以通过指定URL或某个目录来导入:

#docker import http://example.com/exampleimage example/imagerepo

4.3.3 导入镜像存储文件到本地镜像库:docker load

4.4 删除容器

删除终止状态的容器:docker rm

删除运行中的容器:docker rm -f

清理所有处于终止状态的容器:docker rm $(docker ps -a -q)

5 仓库

5.1 登录

# docker login
docker login

5.2基本操作

5.2.1查找仓库中的镜像

# docker search ...

5.2.2 下载镜像

# docker pull ...

docker push推送镜像到Docker Hub

5.2.3 自动创建

5.3 私有仓库

5.3.1安装运行docker-registry

其他操作

  • docker 容器共享网络和volume
docker run --net=B --volume-from=B --name=A image-A ...

6 常用命令总结

docker help

 docker --help

Usage:  docker COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
      --help               Print usage
  -H, --host list          Daemon socket(s) to connect to (default [])
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  container   Manage containers
  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
  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
  diff        Inspect changes on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a 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
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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.

docker常用命令

docker attach

Attach to a running container

docker attach 登录一个已经在执行的容器

docker build

docker build 建立一个新的image

docker commit

docker commit 提交一个新的image

docker cp

docker cp 将容器中的文件拷贝到主机上

docker daemon

docker daemon docker运行可指定项详解

docker diff

docker diff 较一个容器不同版本提交的文件差异

docker events

docker events 获取sever中的实时事件

docker export

docker export 导出一个容器

docker history

docker history 显示一个image的历史

docker images

docker images 列出image

docker import

docker import 导入已有的image

docker info

docker info 展示docker的信息

docker info 
Containers: 41
 Running: 3
 Paused: 0
 Stopped: 38
Images: 64
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 9c3c5f853ebf0ffac0d087e94daef462133b69c7 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
Kernel Version: 3.10.0-957.21.3.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 991.1 MiB
Name: qwq
ID: H7HK:SYYO:SHCA:KQXG:ZFL7:YZRU:JGBM:3ZYH:PFWA:FHWK:DZUN:2SXL
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)

docker inspect

docker inspect 显示更底层的容器或image信息

docker kill

docker kill 杀死docker容器进程

docker load

docker load 加载image

docker login

docker login 登录docker注册服务器

docker logs

docker logs 获取容器的日志

docker pause

docker pause 暂停容器中的所有进程

docker port

docker port 端口转发

docker ps

docker ps 列出所有容器

docker pull

docker pull 从远端拉取一个image

docker push

docker push 推送image到注册服务器

docker restart

docker restart 重启一个容器或多个容器

docker rmi

docker rmi 删除image

docker rm

docker rm 删除一个或多个容器

docker run

docker run 运行一个新的容器

docker save

docker save 打包image

docker search 搜索images

docker start

docker start 启动一个容器

docker stop

docker stop 停止一个容器

docker tag

docker tag 为image打标签

docker top

docker top 显示容器中的进程

docker top 4aca65f7b2f1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7753                7737                0                   2019                pts/7               00:00:00            /bin/bash
root                7774                7753                0                   2019                pts/7               00:00:00            vi /tmp/web_backend.log
root                14111               14096               0                   2019                pts/3               00:00:00            /bin/sh -x /root/scripts/start_web_backend.sh /bin/bash

docker unpause

docker unpause 取消暂停所有的进程

docker version

docker version 显示版本信息

 docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      b2f74b2/1.13.1
 Built:           Wed May  1 14:55:20 2019
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      b2f74b2/1.13.1
 Built:           Wed May  1 14:55:20 2019
 OS/Arch:         linux/amd64
 Experimental:    false

docker wait

docker wait 阻塞容器运行




本博客所有文章采用的授权方式为 自由转载-非商用-非衍生-保持署名 ,转载请务必注明出处,谢谢。
声明:
本博客欢迎转发,但请保留原作者信息!
博客地址:邱文奇(qiuwenqi)的博客;
内容系本人学习、研究和总结,如有雷同,实属荣幸!
阅读次数:

文章评论

comments powered by Disqus


章节列表