欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker简介及常用命令

程序员文章站 2024-03-11 19:22:19
...

1、什么是Docker

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

2、应用场景

  • web应用的自动化打包和发布;
  • 自动化测试和持续集成、发布;
  • 在服务型环境中部署和调整数据库或其他的后台应用;
  • 从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境。

3、几个概念

1、Docker 镜像(Images)

Docker 镜像是用于创建 Docker 容器的模板。

2、Docker 容器(Container)

容器是独立运行的一个或一组应用。每个容器是相互隔离的。

容器与镜像的关系类似于面向对象编程中的对象与类。

Docker 面向对象
容器 对象
镜像

3、Docker 仓库(Registry)

Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。

4、Docker安装

目前,CentOS 仅发行版本中的内核支持 Docker。

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

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

centos6.8安装

yum install -y epel-release
yum install -y docker-io
//启动docker
service docker start

docker配置文件:/etc/sysconfig/docker

# /etc/sysconfig/docker
#
# Other arguments to pass to the docker daemon process
# These will be parsed by the sysv initscript and appended
# to the arguments list passed to docker -d
# 配置阿里云镜像
other_args="--registry-mirror=https://xxx.mirror.aliyuncs.com"
DOCKER_CERT_PATH=/etc/docker

# Resolves: rhbz#1176302 (docker issue #407)
DOCKER_NOWARN_KERNEL_VERSION=1

# Location used for temporary files, such as those created by
# # docker load and build operations. Default is /var/lib/docker/tmp
# # Can be overriden by setting the following environment variable.
# # DOCKER_TMPDIR=/var/tmp

5、镜像命令

1、列出本地镜像

docker images [OPTIONS]

OPTIONS说明:

  • -a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);
  • –digests :显示镜像的摘要信息;
  • –no-trunc :显示完整的镜像信息;
  • -q :只显示镜像ID。
>docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest              3535063d9957        2 weeks ago         1.848 kB
centos              latest              88ec626ba223        7 weeks ago         199.7 MB

>docker images -q
3535063d9957
88ec626ba223

选项说明:

REPOSITORY:表示镜像的仓库源

TAG:镜像的标签

IMAGE ID:镜像ID

CREATED:镜像创建时间

VIRTUAL SIZE:镜像大小

2、从Docker Hub查找镜像

docker search [OPTIONS] 镜像名字

OPTIONS说明:

  • –automated :只列出 automated build类型的镜像;
  • –no-trunc :显示完整的镜像描述;
  • -s :列出收藏数不小于指定值的镜像。
>docker search java
NAME                  DESCRIPTION                           STARS   OFFICIAL   AUTOMATED
java                  Java is a concurrent, class-based...   1037    [OK]       
anapsix/alpine-java   Oracle Java 8 (and 7) with GLIBC ...   115                [OK]
develar/java                                                 46                 [OK]
isuper/java-oracle    This repository contains all java...   38                 [OK]
lwieske/java-8        Oracle Java 8 Container - Full + ...   27                 [OK]
nimmis/java-centos    This is docker images of CentOS 7...   13                 [OK]
...

//找出java的starts数不小于100的镜像
>docker search -s 100 java
NAME                  DESCRIPTION                           STARS   OFFICIAL   AUTOMATED
java                  Java is a concurrent, class-based...   1037    [OK]       
anapsix/alpine-java   Oracle Java 8 (and 7) with GLIBC ...   115                [OK]

3、从镜像仓库中拉取或者更新指定镜像

docker pull 镜像名字:tag

tag可以不写,默认是latest

4、 删除本地一个或多少镜像。

docker rmi [OPTIONS] IMAGE [IMAGE…]

OPTIONS说明:

  • -f :强制删除;
//删除一个
docker rmi -f hello-world
//删除多个
docker rmi -f tomcat:8.0 centos:latest
//删除全部
docker rmi -f $(docker images -qa)

6、容器命令

1、新建并启动一个容器

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

OPTIONS说明:

  • -d: 后台运行容器,并返回容器ID;
  • -i: 以交互模式运行容器,通常与 -t 同时使用;
  • -p: 端口映射,格式为:主机(宿主)端口:容器端口
  • -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
  • –name=”nginx-lb”: 为容器指定一个名称;
//以交互式方式启动一个容器,在容器内执行/bin/bash命令
[[email protected] ~]# docker run -it --name "centos" centos
//会进入到docker容器中
[[email protected] /]# 
//以后台方式启动一个容器
[[email protected] ~]# docker run -d --name "mynginx" nginx

2、列出当前正在运行的容器

docker ps [OPTIONS]

OPTIONS说明:

  • -a :显示所有的容器,包括未运行的。
  • -l :显示最近创建的容器。
  • -n :列出最近创建的n个容器。
  • –no-trunc :不截断输出。
  • -q :静默模式,只显示容器编号。
[root@localhost ~]# docker ps
CONTAINER ID     IMAGE     COMMAND      CREATED        STATUS       PORTS        NAMES
fb0f5eaf9b29     centos   "/bin/bash"  7 minutes ago  Up 7 minutes              centos 

[root@localhost ~]# docker ps -q
fb0f5eaf9b29

3、退出容器

//exit 容器停止并退出
[root@fb0f5eaf9b29 /]# exit
exit
[root@localhost ~]#
//ctrl + P+Q 容器不停止退出

4、启动容器

docker start 容器名或容器ID

[root@localhost ~]# docker  start fb0f5eaf9b29
fb0f5eaf9b29

5、重启容器

docker restart 容器名或容器ID

[root@localhost ~]# docker restart centos
centos

6、停止容器

docker stop 容器名或容器ID

[root@localhost ~]# docker stop centos
centos

7、强制杀死一个正在运行的容器

docker kill 容器名或容器ID

[root@localhost ~]# docker kill fb5c756cf725
fb5c756cf725

8、删除容器

docker rm 容器ID

[root@localhost ~]# docker rm fb5c756cf725
fb5c756cf725
//删除所有容器
[root@localhost ~]# docker rm -f $(docker ps -a -q)
fb0f5eaf9b29
4bf6ac8b3fe9

9、获取容器的日志

docker logs [OPTIONS] CONTAINER

OPTIONS说明:

  • -f : 跟踪日志输出
  • -t : 显示时间戳
  • –tail :仅列出最新N条容器日志
[[email protected] ~] logs -f mynginx
192.168.239.1 - - [10/Jul/2016:16:53:33 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36" "-"
2016/07/10 16:53:33 [error] 5#5: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.239.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.239.130", referrer: "http://192.168.239.130/"
192.168.239.1 - - [10/Jul/2016:16:53:33 +0000] "GET /favicon.ico HTTP/1.1" 404 571 "http://192.168.239.130/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36" "-"
192.168.239.1 - - [10/Jul/2016:16:53:59 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36" "-"
...

10、查看容器内运行的进程信息

docker top [OPTIONS] CONTAINER [ps OPTIONS]
[root@localhost ~]# docker top b01c196de9e1
UID        PID        PPID        C         STIME        TTY         TIME         CMD
root       9887       3947        0         18:54        pts/3      00:00:00     /bin/bas

11、获取容器/镜像的元数据。

docker inspect [OPTIONS] NAME|ID [NAME|ID...]
[root@localhost ~]# docker inspect b01c196de9e1
[
{
    "Id": "b01c196de9e12519bf8a4f3a43605716c30f05de97ebe54b3184203b9d6ce7cf",
    "Created": "2018-07-26T10:54:09.02555467Z",
    "Path": "/bin/bash",
    "Args": [],
    "State": {
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 9887,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2018-07-26T10:54:09.676961208Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "88ec626ba223582f0fc2c0db20a80ee2d82f12e9e0ae0a0149e7ef2ad3d7c8bc",
    "NetworkSettings": {
        "Bridge": "",
        "EndpointID": "90f141c4715de5d2eb510f8e9c5408e46649e4142af9ec281228bfc4b0182286",
        "Gateway": "172.17.42.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "HairpinMode": false,
        "IPAddress": "172.17.0.7",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:11:00:07",
        "NetworkID": "0f0074c2dac04560904dd2834a180e01357f2d63eed724173fe6c8ec4f69b603",
        "PortMapping": null,
        "Ports": {},
        "SandboxKey": "/var/run/docker/netns/b01c196de9e1",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null
    },
    "ResolvConfPath": "/var/lib/docker/containers/b01c196de9e12519bf8a4f3a43605716c30f05de97ebe54b3184203b9d6ce7cf/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/b01c196de9e12519bf8a4f3a43605716c30f05de97ebe54b3184203b9d6ce7cf/hostname",
    "HostsPath": "/var/lib/docker/containers/b01c196de9e12519bf8a4f3a43605716c30f05de97ebe54b3184203b9d6ce7cf/hosts",
    "LogPath": "/var/lib/docker/containers/b01c196de9e12519bf8a4f3a43605716c30f05de97ebe54b3184203b9d6ce7cf/b01c196de9e12519bf8a4f3a43605716c30f05de97ebe54b3184203b9d6ce7cf-json.log",
    "Name": "/drunk_carson",
    "RestartCount": 0,
    "Driver": "devicemapper",
    "ExecDriver": "native-0.2",
    "MountLabel": "",
    "ProcessLabel": "",
    "Volumes": {},
    "VolumesRW": {},
    "AppArmorProfile": "",
    "ExecIDs": null,
    "HostConfig": {
        "Binds": null,
        "ContainerIDFile": "",
        "LxcConf": [],
        "Memory": 0,
        "MemorySwap": 0,
        "CpuShares": 0,
        "CpuPeriod": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "CpuQuota": 0,
        "BlkioWeight": 0,
        "OomKillDisable": false,
        "Privileged": false,
        "PortBindings": {},
        "Links": null,
        "PublishAllPorts": false,
        "Dns": null,
        "DnsSearch": null,
        "ExtraHosts": null,
        "VolumesFrom": null,
        "Devices": [],
        "NetworkMode": "bridge",
        "IpcMode": "",
        "PidMode": "",
        "UTSMode": "",
        "CapAdd": null,
        "CapDrop": null,
        "RestartPolicy": {
            "Name": "no",
            "MaximumRetryCount": 0
        },
        "SecurityOpt": null,
        "ReadonlyRootfs": false,
        "Ulimits": null,
        "LogConfig": {
            "Type": "json-file",
            "Config": {}
        },
        "CgroupParent": ""
    },
    "Config": {
        "Hostname": "b01c196de9e1",
        "Domainname": "",
        "User": "",
        "AttachStdin": true,
        "AttachStdout": true,
        "AttachStderr": true,
        "PortSpecs": null,
        "ExposedPorts": null,
        "Tty": true,
        "OpenStdin": true,
        "StdinOnce": true,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "Cmd": [
            "/bin/bash"
        ],
        "Image": "centos",
        "Volumes": null,
        "VolumeDriver": "",
        "WorkingDir": "",
        "Entrypoint": null,
        "NetworkDisabled": false,
        "MacAddress": "",
        "OnBuild": null,
        "Labels": {
            "org.label-schema.schema-version": "= 1.0     org.label-schema.name=CentOS Base Image     org.label-schema.vendor=CentOS     org.label-schema.license=GPLv2     org.label-schema.build-date=20180531"
        }
    }
}
]

12、进入正在运行的容器并以交互式方式运行

//exec
[root@localhost ~]# docker exec -t b01c196de9e1 /bin/bash
[root@b01c196de9e1 /]# 
//attach
[root@localhost ~]# docker attach b01c196de9e1
[root@b01c196de9e1 /]#

exec 和attch区别:

attch是直接进入到容器中并启动一个命令终端,不会启动新的进程

exec是在容器中运行一个命令,可以不进入容器,可以启动新的进程

//查看容器中/usr下所有文件
[root@localhost ~]# docker exec -t b01c196de9e1 ls -l /usr
total 64
dr-xr-xr-x.  2 root root 12288 May 31 18:03 bin
drwxr-xr-x.  2 root root  4096 Apr 11 04:59 etc
drwxr-xr-x.  2 root root  4096 Apr 11 04:59 games
drwxr-xr-x.  3 root root  4096 May 31 18:02 include
dr-xr-xr-x. 19 root root  4096 May 31 18:03 lib
dr-xr-xr-x. 24 root root 16384 May 31 18:03 lib64
drwxr-xr-x. 11 root root  4096 May 31 18:03 libexec
drwxr-xr-x. 12 root root  4096 May 31 18:02 local
dr-xr-xr-x.  2 root root  4096 May 31 18:03 sbin
drwxr-xr-x. 52 root root  4096 May 31 18:03 share
drwxr-xr-x.  4 root root  4096 May 31 18:02 src
lrwxrwxrwx.  1 root root    10 May 31 18:02 tmp -> ../var/tmp
[root@localhost ~]# 

13、容器和主机拷贝数据

//把容器中/opt/a.txt拷贝到主机/opt下
[root@localhost /]# docker cp b01c196de9e1:/opt/a.txt /opt/
相关标签: docker