docker命令行使用教程

2017-11-27  本文已影响0人  _我和你一样

知道有些人费劲九牛二虎之力也装不了Kitematic
但你可以使用命令来使用docker。(假设你的终端可以执行docker命令)

写在前面:使用docker命令行,安装并使用mongodb

  1. 在docker中运行mongodb

    -d 在后台运行

    -p docker内部端口27017映射到主机端口27017

docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo

-p 27017:27017 :将容器的27017 端口映射到主机的27017 端口

-v $PWD/db:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录

如果不需要可以将-v相关参数去掉

  1. 链接mongodb

命令行:(注意是两个连续的小短横 - - )

mongo —host ip —port 27017

代码:

mongodb://ip:27017

Docker架构

Docker使用C/S架构(客户端-服务器)模式,使用远程API来管理和创建Docker容器

docker和docker machine都属于docker的客户端

常用术语:

docker镜像( Images) Docker镜像是用户创建docker容器的模板
Docker 容器(Container) 容器是独立运行的一个或一组应用。
Docker 客户端(Client) Docker 客户端通过命令行或者其他工具使用 Docker API
Docker 主机(Host) 一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。
Docker 仓库(Registry) Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。
Docker Machine Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker

Docker安装

Ubuntu docker安装

Docker 要求 Ubuntu 系统的内核版本高于 3.10

使用 uname -r 查看当前内核版本

使用脚本安装docker

wget -qO- https://get.docker.com/ | sh

安装完后会有个提示:

If you would like to use Docker as a non-root user, you should now consider

adding your user to the "docker" group with something like:

sudo usermod -aG docker your-user

Remember that you will have to log out and back in for this to take effect!

当非root用户使用docker时,就需要执行这个命令sudo usermod -aG docker your-user

启动docker后台服务

service docker start

测试运行hello word

docker run hello-world

Dcoker 使用

  1. 在容器内运行应用程序

    docker run ubuntu:15.10 /bin/echo "Hello world"
    

    Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。

  2. 运行交互式的容器

我们通过docker的两个参数 -i -t,让docker运行的容器实现"对话"的能力

docker run -i -t ubuntu:15.10 /bin/bash

可以使用Ctrl D或者 exit 来退出容器

  1. 启动容器(后台模式)

    使用以下命令创建一个以进程方式运行的容器

    docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
    

返回一个容器id,我们可以使用容器id看查看容器内部都发生了什么。

通过docker ps列出正在运行的容器

在容器内使用docker logs 后跟容器id或容器名称 ,查看容器内的标准输出、

  1. 停止容器

    使用stop命令停止容器

    docker stop id或名称
    

Docker容器使用

输入docker命令查看docker客户端所有的命令选项

运行一个web应用

我们将在docker容器中运行一个 Python Flask 应用来运行一个web应用。

docker run -d -P training/webapp python app.py

-d:后台运行

-p:将容器内部使用的网络端口映射到我们的主机上

使用 docker ps 查看正在运行的容器

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c92836457244 training/webapp "python app.py" 53 seconds ago Up 52 seconds 0.0.0.0:32768->5000/tcp upbeat_bose

这里多了端口信息,docker开放了5000映射到主机端口32768

我们也可以指定 -p 标识来绑定指定端口,注意这里是小写

docker run -d -p 5000:5000 training/webapp python app.py

网络端口的快捷方式

通过docker ps 命令可以查看到容器的端口映射,docker还提供了另一个快捷方式:docker port,使用 docker port 可以查看指定 (ID或者名字)容器的某个确定端口映射到宿主机的端口号。

docker port id号或名字

查看web应用程序日志

docker logs [ID或者名字] 可以查看容器内部的标准输出。

docker logs -f 338b88d6c0ee
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
211.148.72.22 - - [27/Nov/2017 10:19:18] "GET / HTTP/1.1" 200 -
211.148.72.22 - - [27/Nov/2017 10:19:19] "GET /favicon.ico HTTP/1.1" 404 -

-f 让 dokcer logs 像使用** tail -f** 一样来输出容器内部的标准输出。

从上面,我们可以看到应用程序使用的是 5000 端口并且能够查看到应用程序的访问日志。

查看web应用程序容器的进程

docker top id或名字

查看web应用程序

使用 docker inspect id或名字来查看Docker的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息。

停止web应用容器

docker stop id或名字

重启web应用容器

已经停止的容器,我们可以使用命令 docker start 来启动。

正在运行的容器,我们可以使用 docker restart 命令来重启

移除WEB应用容器

我们可以使用 docker rm 命令来删除不需要的容器

删除容器时,容器必须是停止状态,否则就会报错

Docker镜像使用

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。

列出镜像列表

docker images

每个镜像都有一个tag来标识镜像的版本号,们使用 REPOSITORY:TAG 来定义不同的镜像。

我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:

docker run -t -i ubuntu:15.10 /bin/bash 

其中15.10就是tag版本,如果不指定版本docker 将默认使用 ubuntu:latest 镜像。

获取新镜像

使用没有时,docker会自动下载,如果想自己下载,可以使用pull命令

docker pull ubuntu:13.10

我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个httpd的镜像来作为我们的web服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。

docker search httpd

使用镜像

docker run httpd

如果不存在会自动下载。

创建镜像

我们可以通过以下两种方式对镜像进行更改

更新镜像

更新镜像之前,我们需要使用镜像来创建一个容器。

在运行的容器内使用 apt-get update 命令进行更新。

在完成操作之后,输入ctrl +D 或 exit命令来退出这个容器。

docker run -t -i ubuntu:15.10 /bin/bash

root@a1a384775696:/# apt-get update

此时ID为a1a384775696的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit来提交容器副本。

docker commit -m="has update" -a="jiashu" a1a384775696 jiashu/ubuntu:v2

-m 提交描述信息

-a 指定镜像作者

a1a384775696 容器id

jiashu/ubuntu:v2 要创建的目标镜像名

使用docker igages查看,就能看到刚才创建的新镜像

使用像镜像启动容器

docker run -t -i jiashu/ubuntu:v2 /bin/bash 

构建镜像

我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。

FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"

RUN /bin/echo 'root:123456' | chpasswd
RUN useradd js
RUN /bin/echo 'js:123456' | chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D

每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。

第一条FROM,指定使用哪个镜像源

RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。

然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。

docker build -t js/centos:6.7 .

注意后面的点

-t :指定要创建的目标镜像名

. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径

设置镜像标签

我们可以使用 docker tag 命令,为镜像添加一个新的标签

docker tag 40a992c7ad19 js/centos:dev

docker tag 镜像ID,这里是 40a992c7ad19 ,镜像名:新的标签名(tag)。不过改便签时一般只修改tag镜像名并不修改

使用 docker images 命令可以看到,ID为860c279d2fec的镜像多一个标签。

删除镜像

删除容器使用的是 docker rm 容器id或名称

删除镜像使用的是 docker rmi 容器id或名称 强制删除请使用 -f参数

Docker容器链接

网络端口映射

我们使用 -P 参数创建了一个 python 应用的容器

docker run -d -P training/webapp python app.py
a6080e89c4e9ab43250e35d078c2b45a7807067f265ecc8c32d645fec2b68854

使用 docker ps 来看到端口5000绑定主机端口32769。

我们也可以使用 -p 标识来指定容器端口绑定到主机端口。

docker run -d -p 5000:5000 training/webapp python app.py

两种方式的区别是:

我们可以指定容器绑定的网络地址,比如绑定127.0.0.1。

docker run -d -p 127.0.0.1:5001:5002 training/webapp python app.py

这样我们就可以通过访问127.0.0.1:5001来访问容器的5002端口。

上面的例子中,默认都是绑定 tcp 端口,如果要绑定 UDP 端口,可以在端口后面加上 /udp

run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

docker port 命令可以让我们快捷地查看端口的绑定情况。

docker容器链接

端口映射并不是唯一把 docker 连接到另一个容器的方法。

docker有一个连接系统允许将多个容器连接在一起,共享连接信息。

docker连接会创建一个父子关系,其中父容器可以看到子容器的信息。

容器命名

当我们创建一个容器的时候,docker会自动对它进行命名。另外,我们也可以使用--name标识来命名容器,例如:

 docker run -d -P --name js training/webapp python app.py

附件:docker命令

输入docker自己就出来了

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -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:
  config      Manage Docker configs
  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
  trust       Manage trust on Docker images (experimental)
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams 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 to files or directories 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
上一篇 下一篇

猜你喜欢

热点阅读