docker基础用法

  • Post author:
  • Post category:其他


什么是docker

docker中的容器:

  • lxc –> libcontainer –> runC

OCI&OCF

OCI

Open Container-initiative

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

    • the Runtime Specification(runtime-spec)
    • the Image Specification(image-spec)

OCF

开放容器格式

  • runC 是一个 CLI 工具,用于根据 OCI 规范生成和运行容器
  • 容器作为 runC 的子进程启动,可以嵌入到各种其他系统中,而无需运行守护程序
  • runC 建立在 libcontainer 之上,libcontainer 是支持数百万个 Docker 引擎安装的相同容器技术

docker提供了一个专门容纳容器镜像的站点:

https://hub.docker.com

docker架构

docker镜像与镜像仓库

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

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

docker对象

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

镜像

  • 映像是一个只读模板,其中包含有关创建 docker 容器的说明。
  • 通常,一个映像基于另一个映像,并具有一些额外的自定义。
  • 您可以创建自己的映像,也可以只使用其他人创建并在注册表中发布的映像。

容器

  • 连接器是图像的可运行实例。
  • 您可以使用 Docker API 或 CLI 创建、运行、停止、移动或删除容器。
  • 可以将容器连接到一个或多个网络,将存储附加到该网络,甚至可以根据其当前状态创建新映像。

安装及使用docker

docker安装

//添加docker的yum库
[root@localhost yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:  0     0    0     0    0     0      0      0 --:--:-- --:100  1919  100  1919    0     0   1845      0  0:00:01  0:00:01 --:--:--  1845
[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  docker-ce.repo  redhat.repo
[root@localhost yum.repos.d]# 

//安装docker
[root@localhost ~]# yum -y install docker-ce

docker加速

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

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@localhost ~]# systemctl start docker  //需要先启动docker,才会生成/etc/docker
[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# ls
key.json
[root@localhost docker]# vim daemon.json

{
          "registry-mirrors": ["https://uemehekj.mirror.aliyuncs.com"]
}
[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker

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 event state


docker search

在 docker hub 仓库中搜索 指定 镜像

[root@localhost docker]#  docker search hello-world
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   1589      [OK]       
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   150                  
tutum/hello-world                          Image to test docker deployments. Has Apache…   87                   [OK]
dockercloud/hello-world                    Hello World!                                    19                   [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB          15                   [OK]
vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   5                    [OK]
arm32v7/hello-world                        Hello World! (an example of minimal Dockeriz…   3                    
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                    
souravpatnaik/hello-world-go               hello-world in Golang                           1                    
infrastructureascode/hello-world           A tiny "Hello World" web server with a healt…   1                    [OK]
thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   1                    
datawire/hello-world                       Hello World! Simple Hello World implementati…   1                    [OK]
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   1                    [OK]
markmnei/hello-world-java-docker           Hello-World-Java-docker                         1                    [OK]
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK]
garystafford/hello-world                   Simple hello-world Spring Boot service for t…   0                    [OK]
strimzi/hello-world-streams                                                                0                    
burdz/hello-world-k8s                      To provide a simple webserver that can have …   0                    [OK]
koudaiii/hello-world                                                                       0                    
businessgeeks00/hello-world-nodejs                                                         0                    
nirmata/hello-world                                                                        0                    [OK]
strimzi/hello-world-consumer                                                               0                    
freddiedevops/hello-world-spring-boot                                                      0                    
strimzi/hello-world-producer                                                               0                    
dandando/hello-world-dotnet                                                                0                    


docker pull

从指定位置拉取镜像

[root@localhost docker]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest


docker images

查看镜像列表

[root@localhost docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB


docker create

创建指定的容器(不运行)

[root@localhost ~]# docker create nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
eff15d958d66: Pull complete 
1e5351450a59: Pull complete 
2df63e6ce2be: Pull complete 
9171c7ae368c: Pull complete 
020f975acd28: Pull complete 
266f639b35ad: Pull complete 
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
23e547fb5cd48a94cc193364b8807644acf6fec05726bc4f4d13b86cd953b8e3


docker start

将指定的容器启动

[root@localhost ~]# docker start 23e547fb5cd48a94cc193364b8807644acf6fec05726bc4f4d13b86cd953b8e3
23e547fb5cd48a94cc193364b8807644acf6fec05726bc4f4d13b86cd953b8e3
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
23e547fb5cd4   nginx     "/docker-entrypoint.…"   2 minutes ago    Up 8 seconds   80/tcp    hopeful_kalam

[root@localhost ~]# 


docker run

将指定的容器创建并允许(若使用该命令时,镜像列表中没有镜像时,它会自动pull并将其创建、运行)

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    ea335eea17ab   13 days ago    141MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB


[root@localhost ~]# docker run httpd
Unable to find image 'httpd:latest' locally  //拉取新镜像
latest: Pulling from library/httpd
eff15d958d66: Already exists 
ba1caf8ba86c: Pull complete 
ab86dc02235d: Pull complete 
0d58b11d2867: Pull complete 
e88da7cb925c: Pull complete 
Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02
Status: Downloaded newer image for httpd:latest
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 01 10:05:20.782123 2021] [mpm_event:notice] [pid 1:tid 140162964778304] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 10:05:20.782256 2021] [core:notice] [pid 1:tid 140162964778304] AH00094: Command line: 'httpd -D FOREGROUND'


[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
httpd         latest    ad17c88403e2   12 days ago    143MB
nginx         latest    ea335eea17ab   13 days ago    141MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB


docker stop

停止指定容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                          PORTS     NAMES
ca661b022404   httpd     "httpd-foreground"       2 minutes ago    Exited (0) About a minute ago             epic_jemison
23e547fb5cd4   nginx     "/docker-entrypoint.…"   6 minutes ago    Up 3 minutes                    80/tcp    hopeful_kalam
[root@localhost ~]# docker stop 23e547fb5cd4
23e547fb5cd4
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                     PORTS     NAMES
ca661b022404   httpd     "httpd-foreground"       3 minutes ago    Exited (0) 2 minutes ago             epic_jemison
23e547fb5cd4   nginx     "/docker-entrypoint.…"   7 minutes ago    Exited (0) 2 seconds ago             hopeful_kalam


docker logs

查看指定容器的日志

[root@localhost ~]# docker logs 23e547fb5cd4
/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: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: 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: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/01 10:03:24 [notice] 1#1: using the "epoll" event method
2021/12/01 10:03:24 [notice] 1#1: nginx/1.21.4
2021/12/01 10:03:24 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/01 10:03:24 [notice] 1#1: OS: Linux 4.18.0-193.el8.x86_64
2021/12/01 10:03:24 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/01 10:03:24 [notice] 1#1: start worker processes
2021/12/01 10:03:24 [notice] 1#1: start worker process 32
2021/12/01 10:03:24 [notice] 1#1: start worker process 33
2021/12/01 10:03:24 [notice] 1#1: start worker process 34
2021/12/01 10:03:24 [notice] 1#1: start worker process 35
2021/12/01 10:08:21 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2021/12/01 10:08:21 [notice] 32#32: gracefully shutting down
2021/12/01 10:08:21 [notice] 33#33: gracefully shutting down
2021/12/01 10:08:21 [notice] 33#33: exiting
2021/12/01 10:08:21 [notice] 32#32: exiting
2021/12/01 10:08:21 [notice] 32#32: exit
2021/12/01 10:08:21 [notice] 33#33: exit
2021/12/01 10:08:21 [notice] 34#34: gracefully shutting down
2021/12/01 10:08:21 [notice] 34#34: exiting
2021/12/01 10:08:21 [notice] 35#35: gracefully shutting down
2021/12/01 10:08:21 [notice] 34#34: exit
2021/12/01 10:08:21 [notice] 35#35: exiting
2021/12/01 10:08:21 [notice] 35#35: exit
2021/12/01 10:08:21 [notice] 1#1: signal 17 (SIGCHLD) received from 35
2021/12/01 10:08:21 [notice] 1#1: worker process 33 exited with code 0
2021/12/01 10:08:21 [notice] 1#1: worker process 35 exited with code 0
2021/12/01 10:08:21 [notice] 1#1: signal 29 (SIGIO) received
2021/12/01 10:08:21 [notice] 1#1: signal 17 (SIGCHLD) received from 32
2021/12/01 10:08:21 [notice] 1#1: worker process 32 exited with code 0
2021/12/01 10:08:21 [notice] 1#1: worker process 34 exited with code 0
2021/12/01 10:08:21 [notice] 1#1: exit


docker rm

删除指定容器(正在运行的容器不可删除)

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
ca661b022404   httpd     "httpd-foreground"       10 minutes ago   Exited (0) 9 minutes ago              epic_jemison
377f8fc3fca8   nginx     "/docker-entrypoint.…"   10 minutes ago   Exited (0) 10 minutes ago             stoic_rosalind
23e547fb5cd4   nginx     "/docker-entrypoint.…"   14 minutes ago   Exited (0) 7 minutes ago              hopeful_kalam
[root@localhost ~]# docker stop 23e547fb5cd4
23e547fb5cd4
[root@localhost ~]# docker rm 23e547fb5cd4
23e547fb5cd4
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
ca661b022404   httpd     "httpd-foreground"       11 minutes ago   Exited (0) 10 minutes ago             epic_jemison
377f8fc3fca8   nginx     "/docker-entrypoint.…"   11 minutes ago   Exited (0) 11 minutes ago             stoic_rosalind


docker attach

进入正在运行的容器中(不能进行交互,会进入http容器进程的前台)

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
ca661b022404   httpd     "httpd-foreground"       11 minutes ago   Exited (0) 10 minutes ago             epic_jemison
377f8fc3fca8   nginx     "/docker-entrypoint.…"   12 minutes ago   Exited (0) 11 minutes ago             stoic_rosalind
[root@localhost ~]# docker attach ca661b022404
You cannot attach to a stopped container, start it first
[root@localhost ~]# docker start 377f8fc3fca8
377f8fc3fca8
[root@localhost ~]# docker attach 377f8fc3fca8


docker exec

进入指定容器中,并能够进行交互

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
ca661b022404   httpd     "httpd-foreground"       13 minutes ago   Exited (0) 12 minutes ago             epic_jemison
377f8fc3fca8   nginx     "/docker-entrypoint.…"   13 minutes ago   Exited (0) 3 seconds ago              stoic_rosalind
[root@localhost ~]# docker start 377f8fc3fca8
377f8fc3fca8
[root@localhost ~]# docker exec -it 377f8fc3fca8 /bin/bash
root@377f8fc3fca8:/# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@377f8fc3fca8:/# cd /etc/ 
root@377f8fc3fca8:/etc# ls
adduser.conf            cron.daily      fonts     host.conf  ld.so.cache    mke2fs.conf    pam.conf   rc2.d        security  subuid
alternatives            debconf.conf    fstab     hostname   ld.so.conf     motd           pam.d      rc3.d        selinux   systemd
apt                     debian_version  gai.conf  hosts      ld.so.conf.d   mtab           passwd     rc4.d        shadow    terminfo
bash.bashrc             default         group     init.d     libaudit.conf  netconfig      passwd-    rc5.d        shadow-   timezone
bindresvport.blacklist  deluser.conf    group-    inputrc    localtime      nginx          profile    rc6.d        shells    ucf.conf
ca-certificates         dpkg            gshadow   issue      login.defs     nsswitch.conf  profile.d  rcS.d        skel      update-motd.d
ca-certificates.conf    e2scrub.conf    gshadow-  issue.net  logrotate.d    opt            rc0.d      resolv.conf  ssl       xattr.conf
cron.d                  environment     gss       kernel     machine-id     os-release     rc1.d      rmt          subgid
root@377f8fc3fca8:/etc# 


docker info

查看docker详细信息

[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.9.0)

Server:
 Containers: 2
  Running: 1
  Paused: 0
  Stopped: 1
 Images: 4
 Server Version: 20.10.11
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 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: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-193.el8.x86_64
 Operating System: Red Hat Enterprise Linux 8.2 (Ootpa)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.664GiB
 Name: 192.168.75.142
 ID: PVYK:ATCY:YAQC:MGB4:UDPM:NNPW:KCNO:WXRA:LHEL:XMD6:P3GY:FK66
 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://uemehekj.mirror.aliyuncs.com/
 Live Restore Enabled: false

[root@localhost ~]# 



版权声明:本文为Yanghu4112原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。