Docker之docker基础
docker架构
docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
docker对象
使用 Docker 时,您正在创建和使用映像、容器、网络、卷、插件和其他对象。
- 图像
映像是一个只读模板,其中包含有关创建 docker 容器的说明。
通常,一个映像基于另一个映像,并具有一些额外的自定义。
您可以创建自己的映像,也可以只使用其他人创建并在注册表中发布的映像。 - 容器
连接器是图像的可运行实例。
您可以使用 Docker API 或 CLI 创建、运行、停止、移动或删除容器。
可以将容器连接到一个或多个网络,将存储附加到该网络,甚至可以根据其当前状态创建新映像。
安装及使用docker
docker安装
[root@docker ~]# cd /etc/yum.repos.d/
[root@docker yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@docker yum.repos.d]# dnf -y install docker-ce
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://6yrl18rf.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
[root@docker ~]# 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: 0
Running: 0
Paused: 0
Stopped: 0
Images: 2
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-257.el8.x86_64
Operating System: CentOS Stream 8
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.622GiB
Name: docker
ID: E7SQ:STVB:F6F4:7DIZ:776P:K3E7:LNQJ:7RK2:43YX:BAZX:ZQYJ:64XI
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://6yrl18rf.mirror.aliyuncs.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 search 搜索镜像
[root@docker ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6904 [OK]
ansible/centos7-ansible Ansible on Centos7 135 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 131 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 121 [OK]
centos/systemd systemd enabled base container. 105 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 48
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 40
kinogmt/centos-ssh CentOS with SSH 29 [OK]
guyton/centos6 From official centos6 container with full up… 10 [OK]
centos/tools Docker image that has systems administration… 7 [OK]
drecom/centos-ruby centos ruby 6 [OK]
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
amd64/centos The official build of CentOS. 2
dokken/centos-7 CentOS 7 image for kitchen-dokken 2
miko2u/centos6 CentOS6 日本語環境 2 [OK]
ovirtguestagent/centos7-atomic The oVirt Guest Agent for Centos 7 Atomic Ho… 2
mcnaughton/centos-base centos base image 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
starlabio/centos-native-build Our CentOS image for native builds 0 [OK]
smartentry/centos centos with smartentry 0 [OK]
king019/centos centos 0
docker pull 拉取镜像
[root@docker ~]# docker pull httpd
Using default tag: latest
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
docker.io/library/httpd:latest
docker images 查看本地镜像列表
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest ad17c88403e2 12 days ago 143MB
nginx latest ea335eea17ab 2 weeks ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
linlusama/hello-world 01 d1165f221234 9 months ago 13.3kB
docker create 创建容器
[root@docker ~]# docker create nginx
5940a4144569b89aa0ce1adc7cfb82af0a8bde417c4e2456c5ad1c19d67c115e
docker start 启动容器
[root@docker ~]# docker start 5940a4144569b89aa0ce1adc7cfb82af0a8bde417c4e2456c5ad1c19d67c115e
5940a4144569b89aa0ce1adc7cfb82af0a8bde417c4e2456c5ad1c19d67c115e
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5940a4144569 nginx "/docker-entrypoint.…" About a minute ago Up 26 seconds 80/tcp agitated_brahmagupta
docker run 运行容器
[root@docker ~]# docker run nginx
/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:51:09 [notice] 1#1: using the "epoll" event method
2021/12/01 10:51:09 [notice] 1#1: nginx/1.21.4
2021/12/01 10:51:09 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/12/01 10:51:09 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/01 10:51:09 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/01 10:51:09 [notice] 1#1: start worker processes
2021/12/01 10:51:09 [notice] 1#1: start worker process 31
2021/12/01 10:51:09 [notice] 1#1: start worker process 32
2021/12/01 10:51:09 [notice] 1#1: start worker process 33
//创建容器后运行容器
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
linlusama/hello-world 01 d1165f221234 9 months ago 13.3kB
[root@docker ~]# docker run --name httpd -it 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.4. 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.4. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 01 10:57:45.099144 2021] [mpm_event:notice] [pid 1:tid 140147397553472] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 10:57:45.099301 2021] [core:notice] [pid 1:tid 140147397553472] AH00094: Command line: 'httpd -D FOREGROUND'
^C[Wed Dec 01 10:58:24.359265 2021] [mpm_event:notice] [pid 1:tid 140147397553472] AH00491: caught SIGTERM, shutting down
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b35fd9f8dc1 httpd "httpd-foreground" 49 seconds ago Exited (0) 9 seconds ago httpd
d759cf6f4022 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp elastic_ellis
8dfa29f60fa3 nginx "/docker-entrypoint.…" 5 minutes ago Exited (0) 5 minutes ago cranky_tereshkova
fd11b65473e0 nginx "/docker-entrypoint.…" 7 minutes ago Exited (0) 6 minutes ago musing_elbakyan
5940a4144569 nginx "/docker-entrypoint.…" 9 minutes ago Up 8 minutes 80/tcp agitated_brahmagupta
ae319d6902ed centos "/bin/bash" 7 hours ago Exited (0) 7 hours ago competent_liskov
//后台运行
[root@docker ~]# docker run -itd nginx
d759cf6f4022ffc8c1348f4018f6f605682ef65e750d23450855521112bc5899
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d759cf6f4022 nginx "/docker-entrypoint.…" 16 seconds ago Up 15 seconds 80/tcp elastic_ellis
5940a4144569 nginx "/docker-entrypoint.…" 4 minutes ago Up 3 minutes 80/tcp agitated_brahmagupta
docker attach 连接容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d759cf6f4022 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp elastic_ellis
5940a4144569 nginx "/docker-entrypoint.…" 10 minutes ago Up 9 minutes 80/tcp agitated_brahmagupta
[root@docker ~]# docker attach d759cf6f4022
2021/12/01 11:00:09 [notice] 33#33: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 31#31: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 32#32: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 1#1: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 34#34: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 31#31: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 33#33: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 34#34: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 1#1: signal 28 (SIGWINCH) received
2021/12/01 11:00:09 [notice] 32#32: signal 28 (SIGWINCH) received
docker ps 列出容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5940a4144569 nginx "/docker-entrypoint.…" 11 minutes ago Up 10 minutes 80/tcp agitated_brahmagupta
//列出没有运行的
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b35fd9f8dc1 httpd "httpd-foreground" 3 minutes ago Exited (0) 2 minutes ago httpd
d759cf6f4022 nginx "/docker-entrypoint.…" 7 minutes ago Exited (0) 27 seconds ago elastic_ellis
8dfa29f60fa3 nginx "/docker-entrypoint.…" 8 minutes ago Exited (0) 7 minutes ago cranky_tereshkova
fd11b65473e0 nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 8 minutes ago musing_elbakyan
5940a4144569 nginx "/docker-entrypoint.…" 11 minutes ago Up 10 minutes 80/tcp agitated_brahmagupta
ae319d6902ed centos "/bin/bash" 7 hours ago Exited (0) 7 hours ago
docker logs 查看容器日志
[root@docker ~]# docker logs 5940a4144569
/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:50:03 [notice] 1#1: using the "epoll" event method
2021/12/01 10:50:03 [notice] 1#1: nginx/1.21.4
2021/12/01 10:50:03 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/12/01 10:50:03 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/01 10:50:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/01 10:50:03 [notice] 1#1: start worker processes
2021/12/01 10:50:03 [notice] 1#1: start worker process 31
2021/12/01 10:50:03 [notice] 1#1: start worker process 32
2021/12/01 10:50:03 [notice] 1#1: start worker process 33
2021/12/01 10:50:03 [notice] 1#1: start worker process 34
docker restart 重启容器
[root@docker ~]# docker restart 5940a4144569
5940a4144569
docker stop 停止容器
[root@docker ~]# docker stop 5940a4144569
5940a4144569
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker kill 杀死运行的容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c47c6c51aee ea335eea17ab "/docker-entrypoint.…" 2 seconds ago Up 2 seconds 80/tcp sweet_goldstine
[root@docker ~]# docker kill 4c47c6c51aee
4c47c6c51aee
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker rm 删除容器
root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c47c6c51aee ea335eea17ab "/docker-entrypoint.…" 46 seconds ago Exited (137) 29 seconds ago sweet_goldstine
b053940ff366 ea335eea17ab "/docker-entrypoint.…" About a minute ago Exited (0) 59 seconds ago infallible_mendeleev
8b35fd9f8dc1 httpd "httpd-foreground" 9 minutes ago Exited (0) 9 minutes ago httpd
d759cf6f4022 nginx "/docker-entrypoint.…" 14 minutes ago Exited (0) 7 minutes ago elastic_ellis
8dfa29f60fa3 nginx "/docker-entrypoint.…" 14 minutes ago Exited (0) 14 minutes ago cranky_tereshkova
fd11b65473e0 nginx "/docker-entrypoint.…" 16 minutes ago Exited (0) 15 minutes ago musing_elbakyan
5940a4144569 nginx "/docker-entrypoint.…" 18 minutes ago Exited (0) 4 minutes ago agitated_brahmagupta
ae319d6902ed centos "/bin/bash" 7 hours ago Exited (0) 7 hours ago competent_liskov
[root@docker ~]# docker rm 4c47c6c51aee
4c47c6c51aee
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b053940ff366 ea335eea17ab "/docker-entrypoint.…" About a minute ago Exited (0) About a minute ago infallible_mendeleev
8b35fd9f8dc1 httpd "httpd-foreground" 10 minutes ago Exited (0) 9 minutes ago httpd
d759cf6f4022 nginx "/docker-entrypoint.…" 14 minutes ago Exited (0) 7 minutes ago elastic_ellis
8dfa29f60fa3 nginx "/docker-entrypoint.…" 15 minutes ago Exited (0) 14 minutes ago cranky_tereshkova
fd11b65473e0 nginx "/docker-entrypoint.…" 16 minutes ago Exited (0) 15 minutes ago musing_elbakyan
5940a4144569 nginx "/docker-entrypoint.…" 18 minutes ago Exited (0) 4 minutes ago agitated_brahmagupta
ae319d6902ed centos "/bin/bash" 7 hours ago Exited (0) 7 hours ago competent_liskov
//批量删除
[rot@docker ~]# docker rm $(docker ps -qa)
b053940ff366
8b35fd9f8dc1
d759cf6f4022
8dfa29f60fa3
fd11b65473e0
5940a4144569
ae319d6902ed
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker exec 进入容器交互式
[root@docker ~]# docker run -itd ad17c88403e2
b05e7e77301ef9e08697b7b4d7b5f3d1ab35c11871b3d687f5e65041c91a963e
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b05e7e77301e ad17c88403e2 "httpd-foreground" 17 seconds ago Up 16 seconds 80/tcp lucid_volhard
[root@docker ~]# docker exec -it b05e7e77301e /bin/bash
root@b05e7e77301e:/usr/local/apache2#
root@b05e7e77301e:/usr/local/apache2#
root@b05e7e77301e:/usr/local/apache2#
root@b05e7e77301e:/usr/local/apache2# ls
bin cgi-bin error icons logs
build conf htdocs include modules
root@b05e7e77301e:/usr/local/apache2#