docker run 命令参数 

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]   

   
  -d, --detach=false         指定容器运行于前台还是后台,默认为false    
  -i, --interactive=false    打开STDIN,用于控制台交互   
  -t, --tty=false            分配tty设备,该可以支持终端登录,默认为false   
  -u, --user=""              指定容器的用户   
  -a, --attach=[]            登录容器(必须是以docker run -d启动的容器) 
  -w, --workdir=""           指定容器的工作目录  
  -c, --cpu-shares=0         设置容器CPU权重,在CPU共享场景使用   
  -e, --env=[]               指定环境变量,容器中可以使用该环境变量   
  -m, --memory=""            指定容器的内存上限   
  -P, --publish-all=false    指定容器暴露的端口   
  -p, --publish=[]           指定容器暴露的端口  
  -h, --hostname=""          指定容器的主机名   
  -v, --volume=[]            给容器挂载存储卷,挂载到容器的某个目录   
  --volumes-from=[]          给容器挂载其他容器上的卷,挂载到容器的某个目录 
  --cap-add=[]               添加权限,权限清单详见:http://linux.die.net/man/7/capabilities   
  --cap-drop=[]              删除权限,权限清单详见:http://linux.die.net/man/7/capabilities   
  --cidfile=""               运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法   
  --cpuset=""                设置容器可以使用哪些CPU,此参数可以用来容器独占CPU   
  --device=[]                添加主机设备给容器,相当于设备直通   
  --dns=[]                   指定容器的dns服务器   
  --dns-search=[]            指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件   
  --entrypoint=""            覆盖image的入口点   
  --env-file=[]              指定环境变量文件,文件格式为每行一个环境变量   
  --expose=[]                指定容器暴露的端口,即修改镜像的暴露端口   
  --link=[]                  指定容器间的关联,使用其他容器的IP、env等信息   
  --lxc-conf=[]              指定容器的配置文件,只有在指定--exec-driver=lxc时使用   
  --name=""                  指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字   
  --net="bridge"             容器网络设置: 
                                bridge 使用docker daemon指定的网桥      
                                host    //容器使用主机的网络   
                                container:NAME_or_ID  >//使用其他容器的网路,共享IP和PORT等网络资源   
                                none 容器使用自己的网络(类似--net=bridge),但是不进行配置  
  --privileged=false         指定容器是否为特权容器,特权容器拥有所有的capabilities   
  --restart="no"             指定容器停止后的重启策略: 
                                no:容器退出时不重启   
                                on-failure:容器故障退出(返回值非零)时重启  
                                always:容器退出时总是重启   
  --rm=false                 指定容器停止后自动删除容器(不支持以docker run -d启动的容器)   
  --sig-proxy=true           设置由代理接受并处理信号,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理

Hello World

之前也有学过Docker,然而由于平时用的时候不多,整个学习过程不过是囫囵吞枣,浅藏辄止而已。所以再来熟悉熟悉罢了(以备后续自查)。docker正常的 hello world 应该是这样的,不过也可以用其他镜像来输出 “hello world”。

[root@master ~]# docker pull hello-world
[root@master ~]# 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.
    (amd64)
 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://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

我用的是 centOS7 的虚拟机,docker我记得也在虚拟机安装过,所以一上来就来了个 docker images 命令,结果,就报错了。因为我虚拟机少不了要频繁的开开关关,所以启动后要记得启动docker服务,最后按照网上的教程重启了一下好了。

[root@master ~]# docker images
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@master ~]# systemctl daemon-reload
[root@master ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@master ~]# service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-07-18 12:38:33 CST; 16s ago
     Docs: https://docs.docker.com
 Main PID: 2039 (dockerd)
    Tasks: 13
   Memory: 156.1M
   CGroup: /system.slice/docker.service
           └─2039 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jul 18 12:38:30 master dockerd[2039]: time="2020-07-18T12:38:30.277660215+08:00" level=info msg="pickfirstBalancer: Handl...le=grpc
Jul 18 12:38:30 master dockerd[2039]: time="2020-07-18T12:38:30.278112890+08:00" level=info msg="pickfirstBalancer: Handl...le=grpc
Jul 18 12:38:31 master dockerd[2039]: time="2020-07-18T12:38:31.043399929+08:00" level=info msg="[graphdriver] using prio...erlay2"
Jul 18 12:38:32 master dockerd[2039]: time="2020-07-18T12:38:32.638615621+08:00" level=info msg="Loading containers: start."
Jul 18 12:38:33 master dockerd[2039]: time="2020-07-18T12:38:33.209006424+08:00" level=info msg="Default bridge (docker0)...ddress"
Jul 18 12:38:33 master dockerd[2039]: time="2020-07-18T12:38:33.274649719+08:00" level=info msg="Loading containers: done."
Jul 18 12:38:33 master dockerd[2039]: time="2020-07-18T12:38:33.659005690+08:00" level=info msg="Docker daemon" commit=6a...19.03.2
Jul 18 12:38:33 master dockerd[2039]: time="2020-07-18T12:38:33.659723430+08:00" level=info msg="Daemon has completed ini...zation"
Jul 18 12:38:33 master dockerd[2039]: time="2020-07-18T12:38:33.719042892+08:00" level=info msg="API listen on /var/run/d...r.sock"
Jul 18 12:38:33 master systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              faa63f80f414        10 months ago       129MB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
hello-world         latest              fce289e99eb9        18 months ago       1.84kB
redis               3.2                 87856cc39862        21 months ago       76MB

不过,刚上来,还是来个 "hello world" ,我准备用镜像 ubuntu:15.10 来运行这个 “hello world”。当然,由于我镜像库里边还没有这个镜像,所以它自动给我下载了一个:

[root@master ~]# docker run ubuntu:15.10 /bin/echo "hello world"
Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete 
759aa75f3cee: Pull complete 
3fa871dc8a2b: Pull complete 
224c42ae46e7: Pull complete 
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
hello world

再次查看本地镜像时,已经有了ubuntu:15.10这个镜像了:

[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              faa63f80f414        10 months ago       129MB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
hello-world         latest              fce289e99eb9        18 months ago       1.84kB
redis               3.2                 87856cc39862        21 months ago       76MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB

然后加上 -it 参数试试(这样就可以直接进入到镜像创建的容器内了):

  • -t: 在新容器内指定一个伪终端或终端。

  • -i: 允许你对容器内的标准输入 (STDIN) 进行交互

再查看一下目录结构,是不是和你见过的 Ubuntu 系统很像?

[root@master ~]# docker run -it ubuntu:15.10 /bin/bash
root@7481a5986831:/# cat /proc/version
Linux version 3.10.0-957.27.2.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Mon Jul 29 17:46:05 UTC 2019
root@7481a5986831:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

也可以使用 -d 参数启动后台模式,并使用 docker logs查看标准输出:

[root@master ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
7a9868d2bd3fe7776d74a3e87a1ea6424f5e1b480e568d49347d08397b51d933
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7a9868d2bd3f        ubuntu:15.10        "/bin/sh -c 'while t…"   11 seconds ago      Up 10 seconds                           trusting_wozniak
[root@master ~]# docker logs 7a9868d2bd3
hello world
hello world
hello world
hello world
hello world
hello world
hello world

使用 docker stop 停止容器,至于容器ID(CONTAINER ID),原始的容器ID比较长,比如此例当作,原始的CONTAINER ID:7a9868d2bd3fe7776d74a3e87a1ea6424f5e1b480e568d49347d08397b51d933,自己使用的时候可以截取前面一部分,只要 docker 能自己找到相应的容器就没有太大影响:

[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7a9868d2bd3f        ubuntu:15.10        "/bin/sh -c 'while t…"   3 minutes ago       Up 3 minutes                            trusting_wozniak
[root@master ~]# docker stop 7a9868d2
7a9868d2
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@master ~]# 

当然,如果你自己没有主动指定容器名称的话,也可以使用自动分配的容器名称来进行操作:

[root@master ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
5128468ffe061ca4848af54d74b78465273beacc736de03b30d8e6d74c008459
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5128468ffe06        ubuntu:15.10        "/bin/sh -c 'while t…"   7 seconds ago       Up 6 seconds                            pedantic_stallman
[root@master ~]# docker stop pedan
Error response from daemon: No such container: pedan
[root@master ~]# docker stop pedantic_stallman
pedantic_stallman

容器使用

容器常用操作

docker run -it --name vm1 ubuntu bash #创建容器
docker ps -a #查看容器状态
docker attach vm1 #连接容器
docker top vm1 #查看容器进程
docker logs vm1 #查看容器指令输出 -f 参数可以实时查看
docker inspect vm1 #查看容器详情
docker stats vm1 #查看容器资源使用率
docker diff vm1 #查看容器修改
docker run -d --name vm1 ubuntu bash -c "while true; do echo westos; sleep 1; done" #后台运行
docker stop vm1 #停止容器
docker start vm1 #启动容器
docker kill vm1 #强制干掉容器
docker restart vm1 #重启容器
docker pause/unpause vm1 #暂停/恢复容器
docker rm vm1 #删除容器
docker export vm1 > vm1.tar #导出容器
docker import vm1.tar image #导入容器为镜像 image

基本命令

输入docker,可以看到 docker 的命令和选项有这么些些:

root@master ~]# docker

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context
                           use")
  -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:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  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
  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

Run 'docker COMMAND --help' for more information on a command.

启动容器

docker run -it ubuntu:15.10 /bin/bash

docker start <container-name>

# 第一次通过镜像实例化容器并启动
[root@master ~]# docker run -it ubuntu:15.10 /bin/bash
root@7481a5986831:/# 

# 再次启动可以通过 CONTAINER ID或者容器名称进行启动
[root@master ~]# docker start 7481a5986831
7481a5986831

[root@master ~]# docker start exciting_noyce
exciting_noyce

[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7481a5986831        ubuntu:15.10        "/bin/bash"         24 minutes ago      Up About a minute                       exciting_noyce

退出容器

exit

root@7481a5986831:/# exit
exit

后台运行

docker run -d ubuntu:15.10 /bin/bash

[root@master ~]# docker run -d ubuntu:15.10 /bin/bash
941bc1424c7a33fc48f107fc6c4be4fff585eb5a12c997905f3e93b565d63bb9

停止容器

docker stop <container-name>

[root@master ~]# docker stop 50126c772ac6
50126c772ac6
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

命名容器

docker run -itd --name ubuntu-test ubuntu:15.10 /bin/bash

# 不手动命名的话,会自动生成随机名称。使用--name ubuntu-test指定容器名称
[root@master ~]# docker run -itd --name ubuntu-test ubuntu:15.10 /bin/bash
f3e86f6694437356b2b66de34797d8c9a8081acc5abee570c18cf2dbeb1e82ea
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f3e86f669443        ubuntu:15.10        "/bin/bash"         6 seconds ago       Up 5 seconds                            ubuntu-test

重命名容器 

 docker rename <old-name> <new-name>

[root@master workspace]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
45cc1f7e4abd        ubuntu:15.10        "/bin/sh -c 'while t…"   4 seconds ago       Up 3 seconds                            goofy_perlman
[root@master workspace]# docker rename goofy_perlman looking
[root@master workspace]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
45cc1f7e4abd        ubuntu:15.10        "/bin/sh -c 'while t…"   35 seconds ago      Up 34 seconds                           looking
[root@master workspace]# 

查看活动容器

docker ps

# 这个命令仅查看活动的容器
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
50126c772ac6        ubuntu:15.10        "/bin/bash"         3 seconds ago       Up 2 seconds

查看所有容器

docker ps -a 

# 包含已停止等容器的查看
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@master ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
f3e86f669443        ubuntu:15.10        "/bin/bash"              10 minutes ago      Exited (0) 8 minutes ago                         ubuntu-test
50126c772ac6        ubuntu:15.10        "/bin/bash"              13 minutes ago      Exited (0) 11 minutes ago                        elegant_lovelace
941bc1424c7a        ubuntu:15.10        "/bin/bash"              14 minutes ago      Exited (0) 14 minutes ago                        quirky_booth
4a02b0442f15        hello-world         "/hello"                 16 minutes ago      Exited (0) 16 minutes ago                        amazing_perlman
7481a5986831        ubuntu:15.10        "/bin/bash"              18 minutes ago      Exited (0) 17 minutes ago                        exciting_noyce
c23328f94a64        hello-world         "/bin/bash"              18 minutes ago      Created                                          boring_benz
5128468ffe06        ubuntu:15.10        "/bin/sh -c 'while t…"   2 hours ago         Exited (137) 2 hours ago                         pedantic_stallman
7a9868d2bd3f        ubuntu:15.10        "/bin/sh -c 'while t…"   2 hours ago         Exited (137) 2 hours ago                         trusting_wozniak
ee7f3dc08a50        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                           flamboyant_booth
89e4fe6fd9ee        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                           gifted_black
e28a462bd4b3        ubuntu:15.10        "/bin/echo 'hello wo…"   2 hours ago         Exited (0) 2 hours ago                           epic_edison
3d4514ad0af7        ubuntu:15.10        "/bin/echo 'hello wo…"   2 hours ago         Exited (0) 2 hours ago                           hopeful_hermann
1838455c70e7        ubuntu:15.10        "/bin/echo 'hello wo…"   3 hours ago         Exited (0) 3 hours ago                           heuristic_mendeleev
a76143c7b736        faa63f80f414        "/bin/sh -c 'set -x …"   10 months ago       Exited (137) 10 months ago                       admiring_colden

进入容器

docker attach <container-name>

# 使用attach进入容器,如果从这个容器退出,会导致容器的停止。
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7481a5986831        ubuntu:15.10        "/bin/bash"         30 minutes ago      Up 52 seconds                           exciting_noyce
[root@master ~]# docker attach 7481a5986831
root@7481a5986831:/# 
root@7481a5986831:/# exit
exit
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


# 使用exec进入容器,如果从这个容器退出,不会导致容器的停止。
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7481a5986831        ubuntu:15.10        "/bin/bash"         30 minutes ago      Up 4 seconds                            exciting_noyce
[root@master ~]# docker exec -it 7481a5986831 /bin/bash
root@7481a5986831:/# exit
exit
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7481a5986831        ubuntu:15.10        "/bin/bash"         30 minutes ago      Up 52 seconds                           exciting_noyce

导出容器

docker export <container-name> > ubuntu.tar

导出以后,可以在目录结构下看到多了一个ubuntu.tar文件

[root@master ~]# docker export 7481a5986831 > ubuntu.tar
[root@master ~]# ll
total 121748
drwxr-xr-x.  2 root root       162 Sep  6  2019 deeplearning
drwxr-xr-x. 11 root root       150 May 19 15:46 django_project
-rw-r--r--.  1 root root     13185 Sep 18  2019 get-docker.sh
drwxr-xr-x.  3 root root       182 Mar 24 09:19 GitTest
-rw-r--r--.  1 root root       439 Apr 28 13:39 ip-address.py
drwxr-xr-x.  5 root root       195 Apr 21 10:17 looking
drwxr-xr-x.  2 root root        96 Mar 25 11:03 PaodingAI
-rw-r--r--.  1 root root        90 Apr  2 14:28 passwd.txt
drwxr-xr-x. 13 root root      4096 May 16  2019 pycorrector
-rw-r--r--.  1 root root      3889 Apr 27 14:49 render.html
-rwxr-xr-x.  1 root root        95 Apr  8 14:37 test.sh
-rw-r--r--.  1 root root     38513 Apr 27 14:31 test.txt
-rw-r--r--.  1 root root 124584448 Jul 18 17:16 ubuntu.tar
-rw-r--r--.  1 root root       276 Apr 27 14:34 user.txt

导入容器

导入以后,我们可以从本地镜像看到多了一个 test/ubuntu 镜像

[root@master ~]# cat ubuntu.tar | docker import - test/ubuntu:v1
sha256:7e7c29ee095d13887bec8c8c031c435b69c0ed5b13e2e20929adc3f45409784e
[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1                  7e7c29ee095d        6 seconds ago       119MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB

删除容器

docker rm -f <container-name>

删除单个容器

[root@master ~]# docker stop exciting_noyce
exciting_noyce
[root@master ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
f3e86f669443        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) About an hour ago                       ubuntu-test
50126c772ac6        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             elegant_lovelace
941bc1424c7a        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             quirky_booth
4a02b0442f15        hello-world         "/hello"                 2 hours ago         Exited (0) 2 hours ago                             amazing_perlman
7481a5986831        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 6 seconds ago                           exciting_noyce
c23328f94a64        hello-world         "/bin/bash"              2 hours ago         Created                                            boring_benz
5128468ffe06        ubuntu:15.10        "/bin/sh -c 'while t…"   3 hours ago         Exited (137) 3 hours ago                           pedantic_stallman
7a9868d2bd3f        ubuntu:15.10        "/bin/sh -c 'while t…"   4 hours ago         Exited (137) 3 hours ago                           trusting_wozniak
ee7f3dc08a50        ubuntu:15.10        "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                             flamboyant_booth
89e4fe6fd9ee        ubuntu:15.10        "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                             gifted_black
e28a462bd4b3        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             epic_edison
3d4514ad0af7        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             hopeful_hermann
1838455c70e7        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             heuristic_mendeleev
a76143c7b736        faa63f80f414        "/bin/sh -c 'set -x …"   10 months ago       Exited (137) 10 months ago                         admiring_colden
[root@master ~]# docker rm -f exciting_noyce
exciting_noyce
[root@master ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
f3e86f669443        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) About an hour ago                       ubuntu-test
50126c772ac6        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             elegant_lovelace
941bc1424c7a        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             quirky_booth
4a02b0442f15        hello-world         "/hello"                 2 hours ago         Exited (0) 2 hours ago                             amazing_perlman
c23328f94a64        hello-world         "/bin/bash"              2 hours ago         Created                                            boring_benz
5128468ffe06        ubuntu:15.10        "/bin/sh -c 'while t…"   3 hours ago         Exited (137) 3 hours ago                           pedantic_stallman
7a9868d2bd3f        ubuntu:15.10        "/bin/sh -c 'while t…"   4 hours ago         Exited (137) 3 hours ago                           trusting_wozniak
ee7f3dc08a50        ubuntu:15.10        "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                             flamboyant_booth
89e4fe6fd9ee        ubuntu:15.10        "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                             gifted_black
e28a462bd4b3        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             epic_edison
3d4514ad0af7        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             hopeful_hermann
1838455c70e7        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             heuristic_mendeleev
a76143c7b736        faa63f80f414        "/bin/sh -c 'set -x …"   10 months ago       Exited (137) 10 months ago                         admiring_colden

清除容器

docker container prune

清理掉所有处于终止状态的容器(谨慎使用)

[root@master ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
f3e86f669443        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) About an hour ago                       ubuntu-test
50126c772ac6        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             elegant_lovelace
941bc1424c7a        ubuntu:15.10        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             quirky_booth
4a02b0442f15        hello-world         "/hello"                 2 hours ago         Exited (0) 2 hours ago                             amazing_perlman
c23328f94a64        hello-world         "/bin/bash"              2 hours ago         Created                                            boring_benz
5128468ffe06        ubuntu:15.10        "/bin/sh -c 'while t…"   3 hours ago         Exited (137) 3 hours ago                           pedantic_stallman
7a9868d2bd3f        ubuntu:15.10        "/bin/sh -c 'while t…"   4 hours ago         Exited (137) 4 hours ago                           trusting_wozniak
ee7f3dc08a50        ubuntu:15.10        "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                             flamboyant_booth
89e4fe6fd9ee        ubuntu:15.10        "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                             gifted_black
e28a462bd4b3        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             epic_edison
3d4514ad0af7        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             hopeful_hermann
1838455c70e7        ubuntu:15.10        "/bin/echo 'hello wo…"   4 hours ago         Exited (0) 4 hours ago                             heuristic_mendeleev
a76143c7b736        faa63f80f414        "/bin/sh -c 'set -x …"   10 months ago       Exited (137) 10 months ago                         admiring_colden
[root@master ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
f3e86f6694437356b2b66de34797d8c9a8081acc5abee570c18cf2dbeb1e82ea
50126c772ac616d51152498552abaf8551a35bcd1defb70042f90ff9b36f2bde
941bc1424c7a33fc48f107fc6c4be4fff585eb5a12c997905f3e93b565d63bb9
4a02b0442f151f0444fe639b9ff0dcb9cff41d19f24159c0f3d34f22ded9c189
c23328f94a64390a128cf4666625ef40eda64c330aa39c06611b68abe7e90820
5128468ffe061ca4848af54d74b78465273beacc736de03b30d8e6d74c008459
7a9868d2bd3fe7776d74a3e87a1ea6424f5e1b480e568d49347d08397b51d933
ee7f3dc08a50a0a803403ffb026b7881e0c95661d67bad60da79aaba90d470cf
89e4fe6fd9ee1759c0fbd14c35a647b07c81bfbf05eac7143e9662e58380d318
e28a462bd4b34209d3bf58126a4fd27deb4d9d74de79648f90d7aa78ec2e266c
3d4514ad0af7c4fbebc7fb0cd798f88137aff4c0d55346fb89f46135703c83eb
1838455c70e78e8ba8034a54c27a4a6e759de368a1ad617d9d019569a2e981c1
a76143c7b736e374e4430751bab03f4b779bd211abdaa931a0ea649fd295e16a

Total reclaimed space: 37.27MB
[root@master ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Web应用容器实例

web应用容器启动

# 先载入webapp镜像
[root@master ~]# docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
[DEPRECATION NOTICE] registry v2 schema1 support will be removed in an upcoming release. Please contact admins of the docker.io registry NOW to avoid future disruption.
e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
10bbbc0fc0ff: Pull complete 
fca59b508e9f: Pull complete 
e7ae2541b15b: Pull complete 
9dd97ef58ce9: Pull complete 
a4c1b0cb7af7: Pull complete 
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker.io/training/webapp:latest
[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1                  7e7c29ee095d        About an hour ago   119MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB
training/webapp     latest              6fae60ef3446        5 years ago         349MB

# 使用镜像生成一个docker容器并在其中中运行一个 Python Flask 应用来运行一个web应用
[root@master ~]# docker run -d -P training/webapp python app.py
3a0953bcbd9a45367f14944746620b5ade508025f40afb048439e144ed088fd6
# -P:将容器内部使用的网络端口随机映射到我们使用的主机上
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
3a0953bcbd9a        training/webapp     "python app.py"     6 seconds ago       Up 3 seconds        0.0.0.0:32768->5000/tcp   fervent_nobel

 Docker 开放了 5000 端口(默认 Python Flask 端口)映射到主机(我的centOS虚拟机)端口 32768 上。

运行容器查看

[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
90852ef0a131        training/webapp     "python app.py"     12 seconds ago      Up 11 seconds       0.0.0.0:32769->5000/tcp   web-test
3a0953bcbd9a        training/webapp     "python app.py"     20 minutes ago      Up 20 minutes       0.0.0.0:32768->5000/tcp   fervent_nobel

容器应用程序日志查看

[root@master ~]# docker logs fervent_nobel
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.128.1 - - [18/Jul/2020 10:49:37] "GET / HTTP/1.1" 200 -
192.168.128.1 - - [18/Jul/2020 10:49:37] "GET /favicon.ico HTTP/1.1" 404 -
192.168.128.1 - - [18/Jul/2020 10:50:01] "GET / HTTP/1.1" 200 -
192.168.128.1 - - [18/Jul/2020 10:50:01] "GET /favicon.ico HTTP/1.1" 404 -
192.168.128.1 - - [18/Jul/2020 10:52:28] "GET / HTTP/1.1" 200 -
[root@master ~]# docker logs -f fervent_nobel
# -f: 让 docker logs 像使用 tail -f 一样来输出容器内部的标准输出。
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.128.1 - - [18/Jul/2020 10:49:37] "GET / HTTP/1.1" 200 -
192.168.128.1 - - [18/Jul/2020 10:49:37] "GET /favicon.ico HTTP/1.1" 404 -
192.168.128.1 - - [18/Jul/2020 10:50:01] "GET / HTTP/1.1" 200 -
192.168.128.1 - - [18/Jul/2020 10:50:01] "GET /favicon.ico HTTP/1.1" 404 -
192.168.128.1 - - [18/Jul/2020 10:52:28] "GET / HTTP/1.1" 200 -
192.168.128.1 - - [18/Jul/2020 10:53:51] "GET / HTTP/1.1" 200 -

容器应用程序进程查看

[root@master ~]# docker top fervent_nobel
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                9622                9604                0                   18:45               ?                   00:00:00            python app.py

容器网络端口映射查看

[root@master ~]# docker port fervent_nobel
5000/tcp -> 0.0.0.0:32768

容器启动

[root@master ~]# docker start fervent_nobel
fervent_nobel

容器重启

[root@master ~]# docker restart fervent_nobel
fervent_nobel

容器停止

[root@master ~]# docker stop fervent_nobel
fervent_nobel

容器移除

# 正常移除(需要先停止容器)
[root@master ~]# docker rm fervent_nobel
Error response from daemon: You cannot remove a running container 3a0953bcbd9a45367f14944746620b5ade508025f40afb048439e144ed088fd6. Stop the container before attempting removal or force remove
[root@master ~]# docker stop fervent_nobel
fervent_nobel
[root@master ~]# docker rm fervent_nobel
fervent_nobel

# 强制移除 使用 -f (force)参数
[root@master ~]# docker rm -f fervent_nobel
fervent_nobel

镜像使用

查找镜像

第一次这么查找的时候,报错了。据说是docker镜像地址的原因,如果不行就换一个镜像再重启试一下。

[root@master ~]# docker search mysql
Error response from daemon: Get https://index.docker.io/v1/search?q=mysql&n=25: net/http: TLS handshake timeout
[root@master ~]# cd /etc/docker/
[root@master docker]# ll
total 12
-rw-r--r--. 1 root root  61 Jul 18 20:12 daemon.json
-rw-r--r--. 1 root root  67 Sep 19  2019 daemon.json.rpmsave
-rw-------. 1 root root 244 Sep 17  2019 key.json
# 修改为阿里云镜像
[root@master docker]# vi daemon.json
{
  "registry-mirrors": ["https://r9xxm8z8.mirror.aliyuncs.com"]
}
systemctl daemon-reload
[root@master docker]# systemctl daemon-reload  # 重载daemon文件
[root@master docker]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@master docker]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9743                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3554                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   713                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   77                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   73                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      44                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   30                                      [OK]
prom/mysqld-exporter                                                              29                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         27                                      
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   25                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   19                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   16                                      
arey/mysql-client                 Run a MySQL client from a docker container      14                                      [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   8                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
monasca/mysql-init                A minimal decoupled init container for mysql    0                     

列出镜像

[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB

拉取镜像

[root@master ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB

删除镜像

[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              faa63f80f414        10 months ago       129MB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
hello-world         latest              fce289e99eb9        18 months ago       1.84kB
redis               3.2                 87856cc39862        21 months ago       76MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB
[root@master ~]# docker rmi redis:3.2  # 一般使用 REPOSITORY:TAG来删除镜像
Untagged: redis:3.2
Untagged: redis@sha256:7b0a40301bc1567205e6461c5bf94c38e1e1ad0169709e49132cafc47f6b51f3
Deleted: sha256:87856cc39862cec77541d68382e4867d7ccb29a85a17221446c857ddaebca916
Deleted: sha256:6650bf9ad80dc21652fb0a98eb55be7a30a860fbb0b76b2a542ee0af4111b890
Deleted: sha256:9a7166b18cebf0cff5d029c76f0104abb28b37a75e540d152a3165ea34bb77bb
Deleted: sha256:cf1be06e96098c6a378519ceb223d87a04f2a556df061bde1003db5f43337912
Deleted: sha256:7ae66985fd3a3a132fab51b4a43ed32fd14174179ad8c3041262670523a6104c
Deleted: sha256:bf45690ef12cc54743675646a8e0bafe0394706b7f9ed1c9b11423bb5494665b
Deleted: sha256:237472299760d6726d376385edd9e79c310fe91d794bc9870d038417d448c2d5
[root@master ~]# docker rmi faa63f80f414
Error response from daemon: conflict: unable to delete faa63f80f414 (must be forced) - image is being used by stopped container a76143c7b736
[root@master ~]# docker rmi -f faa63f80f414
Deleted: sha256:faa63f80f414bfd803c6cd50e8b843fa799a39f0720d90532d32448bad07c61b
Deleted: sha256:bb1fcbd4ba9789e7c8648f36feb4ba70f6c2971f5b71d2f0c4aec3240346574a
[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
hello-world         latest              fce289e99eb9        18 months ago       1.84kB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB

更新镜像

从已创建的容器中更新镜像并提交镜像(新建了个testing_dir目录作为变动标识)

[root@master ~]# docker run -t -i ubuntu:15.10 /bin/bash
root@527abc181a62:/# mkdir testing_dir
root@527abc181a62:/# exit
exit
[root@master ~]# docker commit -m="has update" -a="runoob" 527abc181a62 runoob/ubuntu:v2
sha256:6bb5f254672d2af0169c926ebe917b7b9b62a628e8f83a6beca4b3653b2388aa
[root@master ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
runoob/ubuntu       v2                  6bb5f254672d        5 seconds ago       137MB
test/ubuntu         v1                  7e7c29ee095d        4 hours ago         119MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB
training/webapp     latest              6fae60ef3446        5 years ago         349MB

# 用新镜像创建容器,我们从下边可以看到,刚才镜像里新建的 testing_dir 目录已经写入到新的镜像里边了
# 用这个新镜像创建的容器里边也包含了 testing_dir 目录
[root@master ~]# docker run -t -i runoob/ubuntu:v2 /bin/bash
root@a5699921a996:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  testing_dir  tmp  usr  var
root@a5699921a996:/# 

构建镜像

从零开始来创建一个新的镜像

[root@master ~]# mkdir docker
[root@master ~]# cd docker/
[root@master docker]# touch Dockerfile
[root@master docker]# vi Dockerfile 
# FROM,指定使用哪个镜像源
# RUN 指令告诉docker 在镜像内执行命令,安装了什么
[root@master docker]# cat Dockerfile 
FROM    centos:6.7
MAINTAINER      Fisher "2392863668@qq.com"
RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd runoob
RUN     /bin/echo 'runoob:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D

# 开始构建
# -t :指定要创建的目标镜像名
# . :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
[root@master docker]# docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon  2.048kB
Step 1/9 : FROM    centos:6.7
6.7: Pulling from library/centos
cbddbc0189a0: Pull complete 
Digest: sha256:4c952fc7d30ed134109c769387313ab864711d1bd8b4660017f9d27243622df1
Status: Downloaded newer image for centos:6.7
 ---> 9f1de3c6ad53
Step 2/9 : MAINTAINER      Fisher "2392863668@qq.com"
 ---> Running in ed11839748bb
Removing intermediate container ed11839748bb
 ---> d6caeeac7a37
Step 3/9 : RUN     /bin/echo 'root:123456' |chpasswd
 ---> Running in c133297b4c98
Removing intermediate container c133297b4c98
 ---> bad804ae1a20
Step 4/9 : RUN     useradd runoob
 ---> Running in 1620fdacb511
Removing intermediate container 1620fdacb511
 ---> f80e707eb0a9
Step 5/9 : RUN     /bin/echo 'runoob:123456' |chpasswd
 ---> Running in 8f7d3ed64e19
Removing intermediate container 8f7d3ed64e19
 ---> cd6aa534c3d9
Step 6/9 : RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
 ---> Running in d2e2d8ec2524
Removing intermediate container d2e2d8ec2524
 ---> 4786ccee3f0a
Step 7/9 : EXPOSE  22
 ---> Running in ca823a455293
Removing intermediate container ca823a455293
 ---> 27e7ded0cda7
Step 8/9 : EXPOSE  80
 ---> Running in 71bed30c3da9
Removing intermediate container 71bed30c3da9
 ---> 7b951ff6b73a
Step 9/9 : CMD     /usr/sbin/sshd -D
 ---> Running in 0611ad3968a4
Removing intermediate container 0611ad3968a4
 ---> 70c33b3dee3c
Successfully built 70c33b3dee3c
Successfully tagged runoob/centos:6.7

# 查看构建的镜像
[root@master docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
runoob/centos       6.7                 70c33b3dee3c        About a minute ago   191MB
runoob/ubuntu       v2                  6bb5f254672d        15 minutes ago       137MB
test/ubuntu         v1                  7e7c29ee095d        5 hours ago          119MB
hello-world         latest              bf756fb1ae65        6 months ago         13.3kB
mongo               latest              cda2c466f867        10 months ago        361MB
tomcat              latest              365b0a528e2e        10 months ago        506MB
python              3.5                 67f4589d3c88        10 months ago        908MB
nginx               latest              ab56bba91343        10 months ago        126MB
httpd               latest              19459a872194        10 months ago        154MB
mysql               5.6                 95e0fc47b096        10 months ago        257MB
debian              jessie              c9d6adb06e4d        10 months ago        129MB
centos              6.7                 9f1de3c6ad53        16 months ago        191MB
ubuntu              15.10               9b9cb95443b5        3 years ago          137MB
training/webapp     latest              6fae60ef3446        5 years ago          349MB

构建镜像创建容器

[root@master docker]# docker run -it runoob/centos:6.7 /bin/bash
[root@2ed5c38912e5 /]# id root
uid=0(root) gid=0(root) groups=0(root)
[root@2ed5c38912e5 /]# id runoob
uid=500(runoob) gid=500(runoob) groups=500(runoob)

重新设置镜像标签

[root@master docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
runoob/centos       6.7                 70c33b3dee3c        6 minutes ago       191MB
runoob/ubuntu       v2                  6bb5f254672d        20 minutes ago      137MB
test/ubuntu         v1                  7e7c29ee095d        5 hours ago         119MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
centos              6.7                 9f1de3c6ad53        16 months ago       191MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB
training/webapp     latest              6fae60ef3446        5 years ago         349MB
[root@master docker]# docker tag 70c33b3dee3c runoob/centos:dev
[root@master docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
runoob/centos       6.7                 70c33b3dee3c        8 minutes ago       191MB
runoob/centos       dev                 70c33b3dee3c        8 minutes ago       191MB
runoob/ubuntu       v2                  6bb5f254672d        22 minutes ago      137MB
test/ubuntu         v1                  7e7c29ee095d        5 hours ago         119MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
mongo               latest              cda2c466f867        10 months ago       361MB
tomcat              latest              365b0a528e2e        10 months ago       506MB
python              3.5                 67f4589d3c88        10 months ago       908MB
nginx               latest              ab56bba91343        10 months ago       126MB
httpd               latest              19459a872194        10 months ago       154MB
mysql               5.6                 95e0fc47b096        10 months ago       257MB
debian              jessie              c9d6adb06e4d        10 months ago       129MB
centos              6.7                 9f1de3c6ad53        16 months ago       191MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB
training/webapp     latest              6fae60ef3446        5 years ago         349MB

容器连接

网络端口映射

之前有用过 -P 参数来随机映射端口,当然也可以使用 -p 参数来手动建立端口映射关系:

# [root@master ~]# docker run -d -P training/webapp python app.py

[root@master ~]# docker run -d -p 5000:5000 training/webapp python app.py
58dc741e3a08f347b56309bf549ba156e1efc8a565d8ef937abf0eb09d125adc
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                    NAMES
58dc741e3a08        training/webapp     "python app.py"     About a minute ago   Up About a minute   0.0.0.0:5000->5000/tcp   frosty_buck

当然,也可以指定容器绑定的网络地址,不过因为 docker 是在虚拟机上运行的,这时候就不能像之前那样在本地电脑上使用地址:127.0.0.1:5001去测试了,因为Windows下默认把 IP 127.0.0.1映射到了本机(localhost)而不是虚拟机对应的地址,不过可以在虚拟机使用 curl 命令来进行验证:

[root@master ~]# docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
3cf9e1679d44b45e30526a10cbbcbfb90a4150ad6da754cc99a6bfac72bbeeec
[root@master ~]# curl 127.0.0.1:5001
Hello world!
[root@master ~]# 
[root@master ~]# docker port frosty_buck
5000/tcp -> 0.0.0.0:5000
[root@master ~]# docker port flamboyant_villani
5000/tcp -> 127.0.0.1:5001

新建网络

[root@master ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
6e7962edeb74        bridge              bridge              local
b116d28afaa7        host                host                local
a06c6cfba31c        none                null                local
[root@master ~]# docker network create -d bridge test-net
1daa356d55fc97c5f820879e0b919b8c90085bdf93bdeb3b748822dd5058852a
[root@master ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
6e7962edeb74        bridge              bridge              local
b116d28afaa7        host                host                local
a06c6cfba31c        none                null                local
1daa356d55fc        test-net            bridge              local
[root@master ~]# docker run -itd --name test1 --network test-net ubuntu:15.10 /bin/bash
c495bbbf56804389147c742d31a7b78bb4e4c71f93fae88baa60206a8dbbefcd
[root@master ~]# docker run -itd --name test2 --network test-net ubuntu:15.10 /bin/bash
3f0b5650e2d33fcbf8d4732d3697d06953df8dbf1ef66849712859db82bec28a
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                      NAMES
3f0b5650e2d3        ubuntu:15.10        "/bin/bash"         17 seconds ago      Up 16 seconds                                  test2
c495bbbf5680        ubuntu:15.10        "/bin/bash"         30 seconds ago      Up 27 seconds                                  test1
3cf9e1679d44        training/webapp     "python app.py"     13 minutes ago      Up 13 minutes       127.0.0.1:5001->5000/tcp   flamboyant_villani
58dc741e3a08        training/webapp     "python app.py"     16 minutes ago      Up 16 minutes       0.0.0.0:5000->5000/tcp     frosty_buck
[root@master ~]# docker exec -it test1 /bin/bash
root@c495bbbf5680:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]                                          
0% [1 InRelease 1147 B/265 kB 0%] [Waiting for headers

# 我这儿使用功能 apt-get update 由于网络原因一直卡住,所以先这样了。

DNS配置

[root@master ~]# vi /etc/docker/daemon.json

{
   "registry-mirrors": ["https://r9xxm8z8.mirror.aliyuncs.com"]
}
{
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

[root@master ~]# docker run -it --rm ubuntu  cat etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.128.2

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐