docker基础用法

docker基础用法

文章目录

  • docker基础用法
    • 什么是docker
    • OCI&OCF
      • OCI
      • OCF
    • docker架构
    • docker镜像与镜像仓库
    • docker对象
    • 安装及使用docker
      • docker安装
      • docker加速
      • docker常用操作
      • docker event state

什么是docker

docker中的容器:

  • lxc --> libcontainer --> runC
    docker基础用法_第1张图片

OCI&OCF

OCI

Open Container-initiative

  • 由Linux基金会主导于2015年6月创立
  • 旨在围绕容器格式和运行时制定一个开放的工业化标准
  • contains two specifications
    • the Runtime Specification(runtime-spec)
    • the Image Specification(image-spec)

OCF

Open Container Format

runC is a CLI tool for spawning and running containers according to the OCI specification

  • Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon
  • runC is built on libcontainer, the same container technology powering millions of Docker Engine installations

docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com

docker架构

docker基础用法_第2张图片

docker镜像与镜像仓库

为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。

docker基础用法_第3张图片

镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。

docker对象

When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.

  • IMAGES
    • An image is a read-only template with instructions for creating a docker container.
    • Often, an image is based on another image, with some additional customization.
    • You might create your own images or you might only use those created by others and published in a registry.
  • CONTAINERS
    • A conntainer is a runnable instance of an image.
    • You can create, run, stop, move, or delete a container using the docker API or CLI.
    • You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

安装及使用docker

docker安装

[root@localhost ~]# yum install -y yum-utils
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@localhost ~]# yum clean all && yum makecache fast
[root@localhost ~]# yum -y install docker-ce docker-ce-cli containerd.io
[root@localhost ~]# systemctl enable --now docker

docker加速

docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

docker的加速有多种方式:

  • 网易:https://hub-mirror.c.163.com/
  • 阿里云:https://<你的ID>.mirror.aliyuncs.com(需要阿里云账号)
  • 中科大:https://docker.mirrors.ustc.edu.cn
[root@localhost ~]# vim /etc/docker/daemon.json
//添加以下内容
{
     
  "registry-mirrors": ["https://hub-mirror.c.163.com/"]
}

[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl restart docker

//可以使用docker info查看信息
[root@localhost ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.12
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-1127.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 972.3MiB
 Name: localhost.localdomain
 ID: S4BD:L74S:I27T:NTTE:SPCV:SZGS:TNBP:7N7V:2VGC:7MM7:FJE6:XY6N
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://registry.docker-cn.com/    //这里就是加速地址
 Live Restore Enabled: false

docker常用操作

可参考菜鸟教程

命令 功能
docker search Search the Docker Hub for images
docker pull Pull an image or a repository from a registry
docker images List images
docker create Create a new conntainer
docker start Start one or more stopped containers
docker run Run a command in a new container
docker attach Attach to a runninng container
docker ps List containers
docker logs Fetch the logs of a container
docker restart Restart a container
docker stop Stop one or more running containers
docker kill Kill one or more running containers
docker rm Remove onne or more containers
docker exec Run a command in a running container
docker info Display system-wide information
docker inspect Return low-level information on Docker objects
  • 从Docker Hub查找镜像
[root@localhost ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13662               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1866                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   782                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   127                                     
bitnami/nginx                      Bitnami nginx Docker Image                      89                                      [OK]
  • 从镜像仓库中拉取或者更新指定镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bf5952930446: Pull complete 
cb9a6de05e5a: Pull complete 
9513ea0afb93: Pull complete 
b49ea07d2e93: Pull complete 
a5e4a503d449: Pull complete 
Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
  • 列出本地镜像
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4bb46517cac3        12 days ago         133MB
  • 创建一个新的容器但不启动它
[root@localhost ~]# docker create  --name t1  nginx:latest
e2caa3f5f3382fc3bb0dbf67fc2556d74a8fe9cb498c6e2d104b7c2fd87dca41
  • 启动容器
[root@localhost ~]# docker start t1
t1
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
e2caa3f5f338        nginx:latest        "/docker-entrypoint.…"   About a minute ago   Up 2 seconds        80/tcp              t1
  • 创建一个新的容器并运行一个命令
[root@localhost ~]# docker run -it nginx:latest /bin/bash
root@e7ae58242760:/# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
  • 连接到正在运行中的容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
e7ae58242760        nginx:latest        "/docker-entrypoint.…"   6 minutes ago       Up 6 minutes        80/tcp              musing_saha
[root@localhost ~]# docker attach e7ae58242760
root@e7ae58242760:/# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
  • 列出容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
25622c5bff24        nginx:latest        "/docker-entrypoint.…"   6 seconds ago       Up 5 seconds        80/tcp              infallible_thompson
  • 获取容器的日志
[root@localhost ~]# docker logs t1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
  • 杀掉一个运行中的容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
25622c5bff24        nginx:latest        "/docker-entrypoint.…"   4 minutes ago       Up 4 minutes        80/tcp              infallible_thompson
[root@localhost ~]# docker kill 25622c5bff24
25622c5bff24
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • 删除一个或多个容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
25622c5bff24        nginx:latest        "/docker-entrypoint.…"   6 minutes ago       Exited (137) About a minute ago                       infallible_thompson
e7ae58242760        nginx:latest        "/docker-entrypoint.…"   14 minutes ago      Exited (0) 7 minutes ago                              musing_saha
e2caa3f5f338        nginx:latest        "/docker-entrypoint.…"   17 minutes ago      Exited (0) 8 minutes ago                              t1
[root@localhost ~]# docker rm e2caa3f5f338
e2caa3f5f338
[root@localhost ~]# docker rm e7ae58242760
e7ae58242760
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
25622c5bff24        nginx:latest        "/docker-entrypoint.…"   6 minutes ago       Exited (137) 2 minutes ago                       infallible_thompson
  • 在运行的容器中执行命令
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
25622c5bff24        nginx:latest        "/docker-entrypoint.…"   9 minutes ago       Up 24 seconds       80/tcp              infallible_thompson
[root@localhost ~]# docker exec -it 25622c5bff24 /bin/bash
root@25622c5bff24:/# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
  • 获取容器/镜像的元数据
[root@localhost ~]# docker inspect nginx:latest 
[
    {
     
        "Id": "sha256:4bb46517cac397bdb0bab6eba09b0e1f8e90ddd17cf99662997c3253531136f8",
        "RepoTags": [
            "nginx:latest"
        ],
       		...此处内容略
       		...
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:e046ee8113f48aaab8b7326ab6aed674eead374224b513de0684c2802ed2f3b9",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
     
                "maintainer": "NGINX Docker Maintainers "
            },
            "StopSignal": "SIGTERM"
        },
                ...此处内容略
            	...
            ]
        },
        "Metadata": {
     
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

docker event state

docker基础用法_第4张图片

你可能感兴趣的:(docker,linux,docker)