docker基础用法
docker基础用法什么是dockerDocker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。Docker的核心思想就是来自集装箱;集装箱的概念是隔离货物,所以docker的核心就是隔离机制。同时docker使用可移植镜像所以部署以及运维极
docker基础用法
什么是docker
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
Docker的核心思想就是来自集装箱;集装箱的概念是隔离货物,所以docker的核心就是隔离机制。同时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
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引擎组成:
docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
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安装
方式1:
安装依赖包
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
配置docker的yum源
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
docker安装版本查看
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
安装最新版docker
[root@localhost ~]# yum install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io -y
方式2:
配置docker源
[root@localhost ~]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
安装docker-ce以及docker组件
[root@localhost ~]# yum -y install docker-ce
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
设置docker hub阿里云镜像加速
[root@localhost ~]# systemctl start docker
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://in3617d8.mirror.aliyuncs.com"]
}
EOF
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl daemon-reload
docker常用操作
命令 | 功能 |
---|---|
docker search | Search the Docker Hub for images 从Docker Hub镜像仓库上查找镜像 |
docker pull | Pull an image or a repository from a registry 从Docker Hub镜像仓库上拉取镜像 |
docker images | List images 列出本地的镜像 |
docker create | Create a new conntainer 创建一个新的容器(与run命令不同,使用create命令只能创建容器而并不启动) |
docker start | Start one or more stopped containers 启动容器 |
docker run | Run a command in a new container 根据镜像新建并启动容器 |
docker attach | Attach to a runninng container 进入到一个正在运行容器内(使用exit命令退出后,容器会停止) |
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 进入到一个正在运行容器内(使用exit命令退出后,容器会继续运行) |
docker info | Display system-wide information 显示当前系统信息、docker容器、镜像个数、设置等信息 |
docker inspect | Return low-level information on Docker objects 用于以JSON格式显示容器与镜像的详细信息 |
命令演示
docker version命令
查看docker的版本信息
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:36:58 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:35:20 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
docker info命令
查看docker的详细信息
[root@localhost ~]# docker info
Client: #docker客户端信息
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: #docker服务器端信息
Containers: 0 #容器数量
Running: 0 #正在运行的数量
Paused: 0 #暂停的数量
Stopped: 0 #已停止的数量
Images: 0 #镜像数量
Server Version: 20.10.11 #docker服务器版本
Storage Driver: overlay2 #docker存储驱动程序
Backing Filesystem: xfs #文件系统
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file #日志驱动程序
Cgroup Driver: cgroupfs #Cgroup驱动程序
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 #Swarm状态
Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2 #runtimes信息
Default Runtime: runc #默认runtime
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 #linux内核版本
Operating System: Red Hat Enterprise Linux 8.2 (Ootpa) #linux操作系统
OSType: linux #操作系统类型
Architecture: x86_64
CPUs: 2 #宿主机CPU数量
Total Memory: 3.664GiB #宿主机内存
Name: localhost.localdomain #宿主机名称
ID: P6PW:JNW6:BKLA:OQBN:GASX:MTK7:75GM:XVRR:IJC6:5A57:ADVX:RVHX
Docker Root Dir: /var/lib/docker #docker根目录
Debug Mode: false
Registry: https://index.docker.io/v1/ #镜像仓库
Labels:
Experimental: false
Insecure Registries: #非安全镜像仓库
127.0.0.0/8
Registry Mirrors: #镜像加速器
https://in3617d8.mirror.aliyuncs.com/
Live Restore Enabled: false
docker inspect命令
用于以JSON格式显示容器与镜像的详细信息
[root@localhost ~]# docker inspect centos01
......
"Config": {
"Hostname": "05de12ebccae",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"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,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e63a84adb2295beed207ce3ad234074c66d47d4463a79b5852ed27086f326c07",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/e63a84adb229",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "fb025130964ebc0dd35f4eb40c6c29de76f01a7eac1405c9ba1a144c166ff06a",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "5960e7e679b1d5aac7116e1a93034809f34cd7fe7087de8df5d037a0f6d6c15f",
"EndpointID": "fb025130964ebc0dd35f4eb40c6c29de76f01a7eac1405c9ba1a144c166ff06a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
......
docker search命令
从Docker Hub镜像仓库上查找centos镜像
[root@localhost ~]# 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
jdeathe/centos-ssh-apache-php Apache PHP - CentOS. 31 [OK]
kinogmt/centos-ssh CentOS with SSH 29 [OK]
guyton/centos6 From official centos6 container with full up… 10 [OK]
nathonfowlie/centos-jre Latest CentOS image with the JRE pre-install… 8 [OK]
centos/tools Docker image that has systems administration… 7 [OK]
drecom/centos-ruby centos ruby 6 [OK]
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
dokken/centos-7 CentOS 7 image for kitchen-dokken 2
miko2u/centos6 CentOS6 日本語環境 2 [OK]
amd64/centos The official build of CentOS. 2
mcnaughton/centos-base centos base image 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
smartentry/centos centos with smartentry 0 [OK]
starlabio/centos-native-build Our CentOS image for native builds 0 [OK]
docker pull命令
从Docker Hub镜像仓库上拉取centos镜像(默认最新版)
[root@localhost ~]# docker pull centos #centos后不跟任何版本,代表默认拉取最新版latest
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
从Docker Hub镜像仓库上拉取指定版本centos系统镜像
[root@localhost ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
docker image命令
删除本地的镜像
[root@localhost ~]# docker image rm nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Deleted: sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291
Deleted: sha256:cc284e9b1cbed75793782165a07a0c2139d8ec0116d1d562c0e2e504ed586238
Deleted: sha256:6207e091bef7f1c94a109cb455ba163d53d7c2c641de65e71d3a0f33c0ebd8ae
Deleted: sha256:97a18ff8c6973f64d763f004cad932319a1428e0502c0ec3e671e78b2f14256b
Deleted: sha256:319130834f01416a2e8f9a4f2b2fa082c702ac21f16e0e2a206e23d53a0a3bae
Deleted: sha256:1bc375f72973dc110c9629a694bc7476bf878d244287c0214e6436afd6a9d1b0
Deleted: sha256:e1bbcf243d0e7387fbfe5116a485426f90d3ddeb0b1738dca4e3502b6743b325
docker images命令
列出本地的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 2 months ago 204MB
centos latest 5d0da3dc9764 2 months ago 231MB
docker create命令
创建一个新的容器centos01
[root@localhost ~]# docker create --name centos01 centos
3e485339765890eb0b6c803159cae7d3a2076bff2c5e574fe1c10e411b521ae9
创建一个新的容器centos02,并指定shell
[root@localhost ~]# docker create -it --name centos02 centos /bin/bash
8d3b7f0c2de3b26dac399b9cbc620b9949b0d924aec19f1dd7759570339de60e
docker start命令
启动容器centos01
[root@localhost ~]# docker start centos01
centos01
启动容器centos02
[root@localhost ~]# docker start centos02
centos02
同时启动多个容器
[root@localhost ~]# docker start centos01 centos02
centos01
centos02
docker ps命令
列出当前所有正在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05de12ebccae centos "/bin/bash" 2 minutes ago Up 2 minutes centos01
8d3b7f0c2de3 centos "/bin/bash" 20 minutes ago Up 10 minutes centos02
列出所有容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05de12ebccae centos "/bin/bash" 3 minutes ago Up 3 minutes centos01
8d3b7f0c2de3 centos "/bin/bash" 20 minutes ago Exited (0) 2 seconds ago centos02
列出正在运行的容器的id
[root@localhost ~]# docker ps -q
05de12ebccae
列出所有容器的id
[root@localhost ~]# docker ps -aq
05de12ebccae
8d3b7f0c2de3
docker stop命令
停止正在运行的容器
[root@localhost ~]# docker stop centos01
centos01
[root@localhost ~]# docker stop centos02
centos02
停止多个正在运行的容器
[root@localhost ~]# docker stop centos01 centos02
centos01
centos02
根据id停止所有运行的容器
[root@localhost ~]# docker stop `docker ps -q`
8044c20df9ec
4b9a8e1c0a2b
2762a93193ae
05de12ebccae
8d3b7f0c2de3
docker restart命令
重新启动容器
[root@localhost ~]# docker restart centos01
centos01
[root@localhost ~]# docker restart centos02
centos02
重新启动多个容器
[root@localhost ~]# docker restart centos01 centos02
centos01
centos02
根据id同时启动多个容器
[root@localhost ~]# docker restart `docker ps -aq`
653889632b0d
8044c20df9ec
docker kill命令
杀死正在运行的容器
[root@localhost ~]# docker kill centos01
centos01
杀死所有正在运行的容器
[root@localhost ~]# docker kill `docker ps -aq`
653889632b0d
8044c20df9ec
05de12ebccae
8d3b7f0c2de3
docker rm命令
删除容器
[root@localhost ~]# docker rm centos01
centos01
删除多个容器
[root@localhost ~]# docker rm centos01 centos02
centos01
centos02
根据id同时删除多个容器
[root@localhost ~]# docker rm `docker ps -aq`
653889632b0d
8044c20df9ec
docker rmi命令
删除镜像
[root@localhost ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Deleted: sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291
Deleted: sha256:cc284e9b1cbed75793782165a07a0c2139d8ec0116d1d562c0e2e504ed586238
Deleted: sha256:6207e091bef7f1c94a109cb455ba163d53d7c2c641de65e71d3a0f33c0ebd8ae
Deleted: sha256:97a18ff8c6973f64d763f004cad932319a1428e0502c0ec3e671e78b2f14256b
Deleted: sha256:319130834f01416a2e8f9a4f2b2fa082c702ac21f16e0e2a206e23d53a0a3bae
Deleted: sha256:1bc375f72973dc110c9629a694bc7476bf878d244287c0214e6436afd6a9d1b0
Deleted: sha256:e1bbcf243d0e7387fbfe5116a485426f90d3ddeb0b1738dca4e3502b6743b325
docker run命令
选项-d为后台运行 -it指定shell -p端口映射(本机端口:容器端口) -v目录映射(本地目录:容器目录)
根据镜像新建并启动容器centos03
[root@localhost ~]# docker run -it --name centos03 -d centos /bin/bash
361ba464398351e0878969791a711c12bbddf3b19efaccd387c31ff3008f6d62
根据镜像新建并启动容器nginx(后台运行)
[root@localhost ~]# docker run --name nginx -d nginx:latest
b6f025427e2b2e22d2b76e22ec45c6a4294292286a80e08fadadbe3d670db40b
根据镜像新建并启动容器nginx01(端口映射)
[root@localhost ~]# docker run --name nginx01 -p 80:8080/tcp -d nginx
2762a93193aef882632cd69db0cba4c3be6f63982d7ef3aa7c0fbe9bf5c25748
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2762a93193ae nginx "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 80/tcp, 0.0.0.0:80->8080/tcp, :::80->8080/tcp nginx01
根据镜像新建并启动容器nginx02(目录映射)
[root@localhost ~]# docker run --name nginx02 -v /mnt/:/data -d nginx
4b9a8e1c0a2b374fb3196ec11e522c711de0160484001e6d02dcf8c828f27f20
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b9a8e1c0a2b nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp nginx02
测试目录映射
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
hgfs
[root@localhost mnt]# touch 123
[root@localhost mnt]# ls
123 hgfs
[root@localhost ~]# docker exec -it nginx02 /bin/bash
root@4b9a8e1c0a2b:/# ls /data/
123 hgfs
docker attach命令
进入到一个正在运行容器内
[root@localhost ~]# docker attach centos01
[root@05de12ebccae /]#
docker exec命令
进入到一个在在运行容器
[root@localhost ~]# docker exec -it centos01 /bin/bash
[root@05de12ebccae /]#
不进入容器执行命令
[root@localhost ~]# docker exec centos01 ls /root/
anaconda-ks.cfg
anaconda-post.log
original-ks.cfg
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)