CentOS 7下安装Docker以及Docker的基本使用

CentOS 7下安装Docker以及Docker的基本使用
原创莫失莫忘c 最后发布于2019-08-26 17:27:06 阅读数 7109 收藏
展开
Docker支持以下CentOS版本

CentOS 7 (64-bit)
CentOS 6.5 (64-bit) 或更高的版本
Docker的安装
前提条件
目前,CentOS 仅发行版本中的内核支持 Docker。

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

使用 yum 安装(CentOS 7下)
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。

通过 uname -r 命令查看你当前的内核版本

[root@node1 ~]# uname -r
3.10.0-957.21.3.el7.x86_64

从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。

Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。

本文介绍 Docker CE 的安装使用。

较旧版本的Docker被称为docker或docker-engine,如果已安装这些,请卸载它们:

yum remove -y docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

安装一些必要的工具:

yum install -y yum-utils device-mapper-persistent-data lvm2

添加Docker的存储库

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安装Docker-ce

yum install -y docker-ce

启动Docker

systemctl start docker

测试运行 hello-world

docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
Status: Downloaded newer image for hello-world:latest

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/

由于本地没有hello-world这个镜像,所以会下载一个hello-world的镜像,并在容器内运行,看到以上界面说明Docker已经成功安装

使用脚本安装Docker
1、使用sudo或者root权限登录CentOS

2、确保yum包是最新版

yum update

3、执行Docker安装脚本

curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh

执行这个脚本会添加 docker.repo 源并安装 Docker。

4、启动Docker

systemctl start docker

5、验证 docker 是否安装成功并在容器中执行一个测试的镜像。

docker run hello-world
docker ps

镜像加速
如果发现Docker拉取镜像的速度非常缓慢,可以配置加速器来解决

在这里使用阿里云的加速地址:

https://br10hqrl.mirror.aliyuncs.com

修改Docker的配置文件来设置加速地址

vim /etc/docker/daemon.json
1
{
  "registry-mirrors": ["https://br10hqrl.mirror.aliyuncs.com"]
}

如果没有该文件,新建一个

也可以通过以下命来设置

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://br10hqrl.mirror.aliyuncs.com"]
}
EOF

使配置文件生效

systemctl daemon-reload && systemctl restart docker

删除Docker-ce
yum remove -y docker-ce
rm -rf /var/lib/docker

Docker Hello World
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。

输出Hello world

docker run ubuntu /bin/echo "hello world"

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7413c47ba209: Pull complete 
0fe7e7cbb2e8: Pull complete 
1d425c982345: Pull complete 
344da5c95cec: Pull complete 
Digest: sha256:c303f19cfe9ee92badbbbd7567bc1ca47789f79303ddcef56f77687d4744cd7a
Status: Downloaded newer image for ubuntu:latest
hello world

参数解析:

docker:Docker的二进制文件
run:与前面的docker组合使用来运行一个容器
ubuntu:latest:指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
/bin/echo “hello world”:在启动的容器里执行的命令
整句意思是:Docker以ubuntu:latest镜像创建一个容器,然后在容器里面执行bin/echo "hello world"命令,然后输出结果。

运行交互式的容器
我们可以通过docker的-i-t参数来让容器实现"对话"功能

docker run -it ubuntu:latest /bin/bash
root@a08b25f150bc:/# 

参数解析:

-i:允许你对容器内的标准输入 (STDIN) 进行交互。
-t:在新容器内指定一个伪终端或终端。
通过-i-t两个参数我们进入到了ubuntu这个容器内部

在容器内部运行以下命令查看当前系统版本信息和当前目录下的文件列表

root@a08b25f150bc:/# cat /proc/version
Linux version 3.10.0-957.21.3.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Tue Jun 18 16:35:19 UTC 2019
root@a08b25f150bc:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@a08b25f150bc:/# 

启动容器
在我们启动容器的时候通过-d参数可以使容器后台运行

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

在输出中,我们没有看到期望的"hello world",而是一串长字符

1391c001ef7f562c8c95730998a94b638f9be9fc05be9075db19fd2aa8f2fc9b

这个长字符串叫做容器ID,对每个容器来说都是唯一的,我们可以通过容器ID来查看对应的容器发生了什么。

首先,我们需要确认容器在运行,可以通过docker ps来查看,加-a参数可以查看所有容器,包括退出、创建中和正在运行的容器。

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1391c001ef7f        ubuntu:latest       "/bin/sh -c 'while t…"   5 minutes ago       Up 5 minutes                            mystifying_burnell
docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
1391c001ef7f        ubuntu:latest       "/bin/sh -c 'while t…"   About an hour ago   Exited (137) 19 minutes ago                          mystifying_burnell
700af38bbd37        ubuntu:latest       "/bin/bash"              About an hour ago   Exited (0) About an hour ago                         practical_wescoff
a08b25f150bc        ubuntu:latest       "/bin/bash"              About an hour ago   Exited (130) About an hour ago                       gallant_curie
6eb5efafcfe8        ubuntu              "/bin/echo 'hello wo…"   About an hour ago   Exited (0) About an hour ago                         sharp_curran
64a28ff4f5cd        hello-world         "/hello"                 2 hours ago         Exited (0) 2 hours ago                               clever_booth

参数解析:

CONTAINER ID:容器ID
NAMES:自动分配的容器名称
我们可以使用docker logs ["CONTAINER" | "NAMES"]来查看容器内的标准输出和日志

docker logs 1391c001ef7f
hello world
hello world
hello world
hello world
hello world
docker logs mystifying_burnell
hello world
hello world
hello world
hello world
hello world

停止容器
我们使用docker stop ["CONTAINER" | "NAMES"]来停止容器

docker stop 1391c001ef7f
1391c001ef7f

通过docker ps查看,容器已经停止工作

docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

也可以使用下面的命令来停止

docker stop mystifying_burnell

Docker 容器的使用
Docker的所有命令
我们可以输入doker --help或者直接输入docker来查看docker的所有命令

docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

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:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  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 COMMAND --help来查看某个选项的更多信息

docker rm --help

运行一个Web应用
前面我们运行的容器并没有什么特别的用处。

接下来我们尝试使用Docker构建一个web应用程序

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

下载镜像
我们可以通过docker pull命令来下载镜像,如果不指定镜像的tag则默认是用latest版本的镜像

docker pull training/webapp

docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
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

上面我所下载的镜像没有指定tag,所以默认使用的是latest的镜像版本,我们可以使用docker pull image:tag来下载指定版本的镜像。

例如,我想下载一个1.16版本的Nginx稳定版镜像

docker pull nginx:1.16

1.16: Pulling from library/nginx
1ab2bdfe9778: Pull complete 
f50f9bfaef26: Pull complete 
eef791162233: Pull complete 
Digest: sha256:764877c3b96e7d57f8f15bb84597dd108dcdf700bfc43fbe340dae764630386d
Status: Downloaded newer image for nginx:1.16
docker.io/library/nginx:1.16

当指定了tag时,docker下载镜像就会下载指定的tag版本镜像,而不是默认的latest镜像。

运行容器
docker run -d -P training/webapp python app.py
5bc0e295d0033648eb98943f33d7acfc0f603d7b62225f3db21d812f81da1634
1
2
参数解析:

-d:让容器在后台运行。
-P:将容器内部使用的端口映射到主机的随机端口,如果使用-p参数,则是指定容器内部端口映射到主机上的某个端口,具体用法-p <主机端口> : <容器内部端口> 。
查看Web应用容器
我们可以使用docker ps来查看docker正在运行的容器:

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5bc0e295d003 training/webapp "python app.py" 16 seconds ago Up 13 seconds 0.0.0.0:32768->5000/tcp upbeat_knuth
1
2
3
各栏目解析:

CONTAINER ID:指容器的ID,每一个容器运行时都会有一个唯一的ID。

IMAGE:容器使用的镜像。

COMMAND:容器执行的命令。

CREATED:容器创建的时间。

STATUS:容器运行的时长。

PORTS:容器的端口映射情况,后面是容器内部使用的5000端口,映射到主机的32768端口,我们访问主机的32768端口,就可以使用容器提供的服务了。

NAMES:容器的名字,可以通过--name指定,和ID一样是唯一的。

通过标准输出,我们看到了容器提供服务的端口,打开浏览器,输入主机的ip:容器映射到主机的端口,来访问容器提供的服务了,这里我输入http://192.168.6.128:32768来访问。

前面我们使用-P来随机映射端口,下面我们自己来指定端口,使用-p参数指定

docker run -d -p 5000:5000 --name web training/webapp
4c1acc0051961193cc8faf8dcacf05dea454fb960217a243ae89b8e327ee8daf
1
2
我们把容器内部的5000端口映射到主机的5000端口,打开浏览器访问http://192.168.6.128:5000,就可以得到容器提供的服务了。

查看容器的端口
我们可以通过docker ps来查看正在运行的容器的ID、名字、和端口,要查看某个容器的端口,可以根据他的容器ID或者名字来获取它的端口信息,例如:

docker ps
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c1acc005196 training/webapp "python app.py" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp web
5bc0e295d003 training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:32768->5000/tcp upbeat_knuth

1
2
3
4
我们要查看第一个容器的端口信息,它的ID是4c1acc005196,名字是web

docker port 4c1acc005196
5000/tcp -> 0.0.0.0:5000
1
2
docker port web
5000/tcp -> 0.0.0.0:5000
1
2
查看容器的日志信息
当我们运行docker容器时,需要查看它的日志信息,来确定是否运行正常,我们通过docker logs [CONTAINER | NAMES]来查看

docker logs -f 4c1acc005196

  • Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    192.168.6.1 - - [16/Aug/2019 07:00:48] "GET / HTTP/1.1" 200 -
    192.168.6.1 - - [16/Aug/2019 07:00:48] "GET /favicon.ico HTTP/1.1" 404 -
    1
    2
    3
    4
    -f:让docker logs可以向tail -f一样持续输出容器内部的标准输出

查看WEB应用程序容器的进程
我们可以使用docker top来查看容器内部运行的进程

docker top 4c1acc005196
UID PID PPID C STIME TTY TIME CMD
root 19910 19891 0 14:02 ? 00:00:01 python app.py
1
2
3
检查WEB应用程序
使用docker inspect来查看Docker的底层信息,他会返回一个json文件记录着Docker容器的配置和状态信息。

docker inspect 4c1acc005196
1
[
{
"Id": "4c1acc0051961193cc8faf8dcacf05dea454fb960217a243ae89b8e327ee8daf",
"Created": "2019-08-16T06:02:31.210492449Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 19910,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-08-16T06:02:32.26229118Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
停止WEB应用容器
docker stop 4c1acc005196
4c1acc005196
1
2
重启WEB应用程序
已经停止的容器,我们可以使用docker start来启动

我们先找出退出容器的ID和名字,然后启动它,我们使用docker ps -a来找出退出的容器。

docker ps -a
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c1acc005196 training/webapp "python app.py" 2 hours ago Exited (137) 6 minutes ago web
5bc0e295d003 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp upbeat_knuth
1
2
3
我们还可以通过docker ps -l查询最后一次创建的容器

docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c1acc005196 training/webapp "python app.py" 2 hours ago Up 8 minutes 0.0.0.0:5000->5000/tcp web
1
2
3
上面输出我们获取了退出容器的ID,下面启动它

docker start 4c1acc005196
4c1acc005196
1
2
正在运行的容器我们可以通过docker restart来重启。

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

docker stop 4c1acc005196
4c1acc005196

docker rm 4c1acc005196
4c1acc005196
1
2
3
4
5
删除容器时,容器必须是停止状态,否则会报错,但是可以使用-f参数来强制删除。

docker rm 4c1acc005196
Error response from daemon: You cannot remove a running container 4c1acc0051961193cc8faf8dcacf05dea454fb960217a243ae89b8e327ee8daf. Stop the container before attempting removal or force remove
1
2
Docker 镜像的使用
当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。

下面我们来学习:

1、管理和使用本地 Docker 主机镜像
2、创建镜像
列出镜像列表
我们可以使用 docker images 来列出本地主机上的镜像。

runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
training/webapp latest 6fae60ef3446 11 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
各个选项说明:

REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。

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

runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@d77ccb2e5cca:/#
1
2
如果要使用版本为14.04的ubuntu系统镜像来运行容器时,命令如下:

runoob@runoob:~$ docker run -t -i ubuntu:14.04 /bin/bash
root@39e968165990:/#
1
2
如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。

获取一个新的镜像
当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。

Crunoob@runoob:~$ docker pull ubuntu:13.10
13.10: Pulling from library/ubuntu
6599cadaf950: Pull complete
23eda618d451: Pull complete
f0be3084efe9: Pull complete
52de432f084b: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:15b79a6654811c8d992ebacdfbd5152fcf3d165e374e264076aa435214a947a3
Status: Downloaded newer image for ubuntu:13.10
1
2
3
4
5
6
7
8
9
下载完成后,我们可以直接使用这个镜像来运行容器。

查找镜像
我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/

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

runoob@runoob:~$ docker search httpd
1

NAME:镜像仓库源的名称

DESCRIPTION:镜像的描述

OFFICIAL:是否docker官方发布

拖取镜像
我们决定使用上图中的httpd 官方版本的镜像,使用命令 docker pull 来下载镜像。

runoob@runoob:~$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
8b87079b7a06: Pulling fs layer
a3ed95caeb02: Download complete
0d62ec9c6a76: Download complete
a329d50397b9: Download complete
ea7c1f032b5c: Waiting
be44112b72c7: Waiting
1
2
3
4
5
6
7
8
9
下载完成后,我们就可以使用这个镜像了。

runoob@runoob:~$ docker run httpd
1
创建镜像
当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。

1.从已经创建的容器中更新镜像,并且提交这个镜像
2.使用 Dockerfile 指令来创建一个新的镜像
更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器。

runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/#
1
2
在运行的容器内使用 apt-get update 命令进行更新。

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

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

runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
1
2
各个参数说明:

-m:提交的描述信息
-a:指定镜像作者
e218edb10161:容器ID
runoob/ubuntu:v2:指定要创建的目标镜像名
我们可以使用 docker images 命令来查看我们的新镜像 runoob/ubuntu:v2:

runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/ubuntu v2 70bf1840fd7c 15 seconds ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
11
使用我们的新镜像 runoob/ubuntu 来启动一个容器

runoob@runoob:~$ docker run -t -i runoob/ubuntu:v2 /bin/bash
root@1a9fbdeb5da3:/#
1
2
构建镜像
我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。

runoob@runoob:~$ cat Dockerfile
FROM centos:6.7
MAINTAINER Fisher "[email protected]"

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
1
2
3
4
5
6
7
8
9
10
11
每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。

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

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

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

runoob@runoob:~$ docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "[email protected]"
---> Using cache
---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
---> Using cache
---> 0397ce2fbd0a
Step 4 : RUN useradd runoob
......
1
2
3
4
5
6
7
8
9
10
11
12
参数说明:

-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
使用docker images 查看创建的镜像已经在列表中存在,镜像ID为860c279d2fec

runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec About a minute ago 190.6 MB
runoob/ubuntu v2 70bf1840fd7c 17 hours ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB
php 5.6 f40e9e0f10c8 10 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
11
12
13
我们可以使用新的镜像来创建容器

runoob@runoob:~$ docker run -t -i runoob/centos:6.7 /bin/bash
[root@41c28d18b5fb /]# id runoob
uid=500(runoob) gid=500(runoob) groups=500(runoob)
1
2
3
从上面看到新镜像已经包含我们创建的用户runoob

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

runoob@runoob:~$ docker tag 860c279d2fec runoob/centos:dev
1
docker tag 镜像ID,这里是 860c279d2fec ,用户名称、镜像源名(repository name)和新的标签名(tag)。

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

runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec 5 hours ago 190.6 MB
runoob/centos dev 860c279d2fec 5 hours ago 190.6 MB
runoob/ubuntu v2 70bf1840fd7c 22 hours ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB
php 5.6 f40e9e0f10c8 10 days ago 444.8 MB
nginx latest 6f8d099c3adc 13 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Dcoker 容器的连接
前面我们实现了通过网络端口来访问运行在 docker 容器内的服务。下面我们来实现通过端口连接到一个 docker 容器

网络端口映射
我们创建了一个 python 应用的容器。

runoob@runoob:~$ docker run -d -P training/webapp python app.py
fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
1
2
另外,我们可以指定容器绑定的网络地址,比如绑定 127.0.0.1。

我们使用 -P 参数创建一个容器,使用 docker ps 可以看到容器端口 5000 绑定主机端口 32768。

runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fce072cc88ce training/webapp "python app.py" 4 minutes ago Up 4 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
我们也可以使用 -p 标识来指定容器端口绑定到主机端口。

两种方式的区别是:

-P :是容器内部端口随机映射到主机的高端口。
-p : 是容器内部端口绑定到指定的主机端口。
runoob@runoob:~$ docker run -d -p 5000:5000 training/webapp python app.py
33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33e4523d30aa training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" 8 minutes ago Up 8 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
4
5
6
另外,我们可以指定容器绑定的网络地址,比如绑定 127.0.0.1。

runoob@runoob:~$ docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95c6ceef88ca training/webapp "python app.py" 6 seconds ago Up 6 seconds 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" 10 minutes ago Up 10 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
4
5
6
7
这样我们就可以通过访问 127.0.0.1:5001 来访问容器的 5000 端口。

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

runoob@runoob:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6779686f06f6 training/webapp "python app.py" 4 seconds ago Up 2 seconds 5000/tcp, 127.0.0.1:5000->5000/udp drunk_visvesvaraya
95c6ceef88ca training/webapp "python app.py" 2 minutes ago Up 2 minutes 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" 12 minutes ago Up 12 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
4
5
6
7
8
docker port 命令可以让我们快捷地查看端口的绑定情况。

runoob@runoob:~$ docker port adoring_stonebraker 5000
127.0.0.1:5001
1
2
Docker容器连接
端口映射并不是唯一把 docker 连接到另一个容器的方法。

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

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

容器命名

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

runoob@runoob:~$ docker run -d -P --name runoob training/webapp python app.py
43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441
1
2
我们可以使用 docker ps 命令来查看容器名称。

runoob@runoob:~$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43780a6eabaa training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:32769->5000/tcp runoob
————————————————
版权声明:本文为CSDN博主「莫失莫忘c」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/TangXuZ/article/details/100082144

猜你喜欢

转载自www.cnblogs.com/fqnb001/p/12468226.html