一.Docker 鏡像,容器,倉(cāng)庫(kù)的簡(jiǎn)單介紹
通俗來(lái)講:鏡像相當(dāng)于VM虛擬機(jī)中的ios文件,容器相當(dāng)于虛擬機(jī)系統(tǒng),倉(cāng)庫(kù)相當(dāng)于系統(tǒng)中的進(jìn)程或者執(zhí)行文件,容器是通過(guò)鏡像創(chuàng)建的
?1.鏡像
Docker 鏡像就是一個(gè) Linux 的文件系統(tǒng)( Root FileSystem ),這個(gè)文件系統(tǒng)里面包含可以運(yùn)行在 Linux 內(nèi)核的程序以及相應(yīng)的數(shù)據(jù),這里要強(qiáng)調(diào)一下鏡像的兩個(gè)特征:
鏡像是分層(Layer)的????????即一個(gè)鏡像可以多個(gè)中間層組成,多個(gè)鏡像可以共享同一中間層,也可以通過(guò)在鏡像添加多一層來(lái)生成一個(gè)新的鏡像鏡像是只讀的(read-only)???????? 鏡像在構(gòu)建完成之后,便不可以再修改,而上面所說(shuō)的添加一層構(gòu)建新的鏡像,這中間實(shí)際是通過(guò)創(chuàng)建一個(gè)臨時(shí)的容器,在容器上增加或刪除文件,從而形成新的鏡像,因?yàn)槿萜魇强梢詣?dòng)態(tài)改變的
?2.容器
????????類(lèi)似 linux 系統(tǒng)環(huán)境, 運(yùn)行和隔離應(yīng)用 ,容器從鏡像啟動(dòng)的時(shí)候, docker 會(huì)在鏡像的最上一層創(chuàng)建一個(gè)可寫(xiě)層, 鏡像本身是只讀的 ,保持不變,容器與鏡像的關(guān)系,就如同面向編程中對(duì)象與類(lèi)之間的關(guān)系。????????因?yàn)?/span> 容器是通過(guò)鏡像來(lái)創(chuàng)建的 ,所以必須先有鏡像才能創(chuàng)建容器,而生成的容器是一個(gè)獨(dú)立于宿主機(jī)的隔離進(jìn)程,并且有屬于容器自己的網(wǎng)絡(luò)和命名空間。????????前面介紹過(guò),鏡像由多個(gè)中間層(layer )組成,生成的鏡像是只讀的,但容器卻是可讀可寫(xiě)的,這是因?yàn)槿萜魇窃阽R像上面添一層讀寫(xiě)層( writer/read layer )來(lái)實(shí)現(xiàn)的,如下圖所示:
3.倉(cāng)庫(kù)(Repository)?
倉(cāng)庫(kù)(Repository)是集中存儲(chǔ)鏡像的地方 ,這里有個(gè)概念要區(qū)分一下,那就是 倉(cāng)庫(kù) 與 倉(cāng)庫(kù)服務(wù)器 (Registry) 是兩回事,像上面說(shuō)的 Docker Hub ,就是 Docker 官方提供的一個(gè)倉(cāng)庫(kù)服務(wù)器,不過(guò)其實(shí)有時(shí)候不太需要太過(guò)區(qū)分這兩個(gè)概念
公共倉(cāng)庫(kù)
公共倉(cāng)庫(kù)一般是指 Docker Hub ,前面已經(jīng)多次介紹如何從 Docker Hub 獲取鏡像,除了獲取鏡像外,也可以將自己構(gòu)建的鏡像存放到 Docker Hub ,這樣,別人也可以使用我們構(gòu)建的鏡像,不過(guò)要將鏡像上傳到 Docker Hub ,必須先在 Docker 的官方網(wǎng)站上注冊(cè)一個(gè)賬號(hào),注冊(cè)界面如下,按要求填寫(xiě)必要的信息就可以注冊(cè)了
?私有倉(cāng)庫(kù)
有時(shí)候自己部門(mén)內(nèi)部有一些鏡像要共享時(shí),如果直接導(dǎo)出鏡像拿給別人又比較麻煩,使用像Docker Hub 這樣的公共倉(cāng)庫(kù)又不是很方便,這時(shí)候可以自己搭建屬于自己的私有倉(cāng)庫(kù)服務(wù),用于存儲(chǔ)和分布自己的鏡像
二.Docker 鏡像以及倉(cāng)庫(kù)?
Docker 鏡像就是一個(gè) Linux 的文件系統(tǒng)( Root FileSystem ),這個(gè)文件系統(tǒng)里面包含可以運(yùn)行在 Linux 內(nèi)核的程序以及相應(yīng)的數(shù)據(jù)
1.docker search 搜索鏡像
docker search centos
[root@localhost containerd]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
備注: -f STARS=3000 搜索 STARS 大于 3000 的鏡像搜索mysql? STARS 大于 3000的鏡像?
[root@localhost containerd]# docker search mysql -f STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14530 [OK]
mariadb MariaDB Server is a high performing open sou… 5550 [OK]
2.docker pull 下載鏡像
下載docker pull centos最新的鏡像
docker pull centos
[root@localhost /]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
下載指定的tag的鏡像:docker pull centos:8.3.2011
[root@localhost /]# docker pull centos:8.3.2011
8.3.2011: Pulling from library/centos
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:8.3.2011
docker.io/library/centos:8.3.2011
下載nginx鏡像: nginx鏡像中集成了一個(gè)linux操作系統(tǒng),在操作系統(tǒng)中又集成了nginx,然后打包成一個(gè)nginx鏡像,下載好nginx鏡像后,然后啟動(dòng)該服務(wù),就可以進(jìn)行web服務(wù)操作了,實(shí)際上在下載nignx鏡像的時(shí)候,其實(shí)不是一下子就下載好了的,而是一層一層地下載其他鏡像,然后打包成nginx鏡像
root@localhost zph]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:b4af4f8b6470febf45dc10f564551af682a802eda1743055a7dfc8332dffa595
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
3.鏡像結(jié)構(gòu)
鏡像結(jié)構(gòu):docker.io/library/centos:latest
[root@localhost containerd]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
4.docker images 查看本地鏡像
[root@localhost containerd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc649bab30d1 10 days ago 187MB
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
centos latest 5d0da3dc9764 2 years ago 231MB
5.docker tag 給鏡像打標(biāo)簽
比如:下載了一個(gè)鏡像,啟動(dòng)了這個(gè)容器,在這個(gè)容器中做了一些操作,然后就想把這個(gè)容器制作成自己的鏡像,那么就需要給這個(gè)鏡像打標(biāo)簽(相當(dāng)于發(fā)布這個(gè)鏡像,制作好這個(gè)鏡像后,就可以把它發(fā)布到遠(yuǎn)程倉(cāng)庫(kù)了),給鏡像打標(biāo)簽可以創(chuàng)建自己的鏡像, 自定義鏡像結(jié)構(gòu)命名規(guī)則如下 : registry name/repository name/image name:tag name ,例如:docker.io/library/centos:latest
[root@localhost zph]# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
?對(duì)上述說(shuō)明:
????????SOURCE_IMAGE 鏡像名?? [:TAG] 鏡像標(biāo)簽名
? ? ? ? TARGET_IMAGE 打包新的鏡像名 [:TAG] 新鏡像標(biāo)簽名
比如:把nginx鏡像打包成一個(gè)新的鏡像:
docker tag nginx ?docker.io/zph/nginx:last 或者把nginx替換成對(duì)應(yīng)的IMAGE ID,如:?
docker tag bc649bab30d1?docker.io/zph/nginx:v2
root@localhost zph]# docker tag nginx docker.io/zph/nginx:last
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc649bab30d1 10 days ago 187MB
zph/nginx last bc649bab30d1 10 days ago 187MB
6.把本地鏡像推送到 dockerHub 倉(cāng)庫(kù)?
(1).需要在dockerHub上面注冊(cè)一個(gè)賬戶(hù)
把打包好的鏡像發(fā)布到遠(yuǎn)程(hub.docker.com官網(wǎng))自己的賬號(hào)下,需要有一個(gè)docker賬號(hào)
(2).使用 docker login 本地登錄 dockerHub?
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have
a Docker ID, head over to https://hub.docker.com to create one.
Username: zph
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
登錄成功后,會(huì)把賬號(hào)信息保存到.docker/config.json中,查看保存的賬戶(hù)信息
[root@localhost ~]# cat .docker/config.json
{
"auths": {
"https://index.docker.io/v1/":
{
"auth": "aXR5aW5nOxxxzEyMzQ="
}
}
}
(3).docker push?鏡像名稱(chēng) 把本地鏡像推送到遠(yuǎn)程
下載一個(gè)alpine的鏡像演示,alpine是一個(gè)比較小的的 linux 鏡像
[root@localhost ~]# 下載alpine鏡像
[root@localhost ~]# docker pull alpine
[root@localhost ~]# 打包alpine鏡像
[root@localhost ~]# docker tag d4ff818577bc docker.io/itying/alpine:v1.0.1
[root@localhost ~]# docker tag d4ff818577bc docker.io/itying/alpine:latest
[root@localhost ~]# 查看alpine鏡像
[root@localhost ~]# docker images | grep alpine
itying/alpine v1.0.1 d4ff818577bc 4 weeks ago 5.6MB
alpine 3.14.0 d4ff818577bc 4 weeks ago 5.6MB
alpine latest d4ff818577bc 4 weeks ago 5.6MB
[root@localhost ~]# 發(fā)布apline鏡像到dockerHub
[root@localhost ~]# docker push docker.io/itying/alpine:v1.0.1
The push refers to repository [docker.io/itying/alpine]
72e830a4dff5: Mounted from library/alpine
v1.0.1: digest: sha256:1775bebec23e1f3ce4869xxx26497d82f2ffca9
d size: 528
7.docker rmi 刪除鏡像?
查看本地鏡像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc649bab30d1 10 days ago 187MB
zph/nginx last bc649bab30d1 10 days ago 187MB
zph/nginx v2 bc649bab30d1 10 days ago 187MB
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
centos latest 5d0da3dc9764 2 years ago 231MB
?刪除對(duì)應(yīng)的標(biāo)簽鏡像:docker rmi 鏡像名:版本,如下:
[root@localhost zph]# docker rmi zph/nginx:v2
Untagged: zph/nginx:v2
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc649bab30d1 10 days ago 187MB
zph/nginx last bc649bab30d1 10 days ago 187MB
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
centos latest 5d0da3dc9764 2 years ago 231MB
如果要?jiǎng)h除IMAGE ID的鏡像,則使用命令:docker rmi -f IMAGE ID, 如:
[root@localhost zph]# docker rmi -f bc649bab30d1
Untagged: nginx:latest
Untagged: nginx@sha256:b4af4f8b6470febf45dc10f564551af682a802eda1743055a7dfc8332dffa595
Untagged: zph/nginx:last
Deleted: sha256:bc649bab30d150c10a84031a7f54c99a8c31069c7bc324a7899d7125d59cc973
Deleted: sha256:c6f480996a203ed077606cce624f944b041449833e2db3f7d19fe22974fb965b
Deleted: sha256:e4347a01432c5f4350b041632f5703c3dd47de2ec68547b9339d11ea44708389
Deleted: sha256:9d40098fc19fdfff9c74fd3c2c0ff49bfda7d9d04b5d7806d0843d32055d769a
Deleted: sha256:165ae0ef2ddd33b6d5a7f206633b9b0c30cd94ff18a4ed5c3aeb59bf28388526
Deleted: sha256:06dabb44ac4d1f0b5544255e944f15a939178d77aff60a5b296e38bd8743efeb
Deleted: sha256:ee220599571f649e0fb74b40db1615a4c9c1355ac912f9e70087b695617af352
Deleted: sha256:cb4596cc145400fb1f2aa56d41516b39a366ecdee7bf3f9191116444aacd8c90
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
centos latest 5d0da3dc9764 2 years ago 231MB
這樣就會(huì)刪除IMAGE ID 為 bc649bab30d1的nginx 鏡像了
三.Docker 容器
類(lèi)似 linux 系統(tǒng)環(huán)境,運(yùn)行和隔離應(yīng)用, 容器從鏡像啟動(dòng)的 時(shí)候, docker 會(huì)在 鏡像的最上一層 創(chuàng)建一個(gè) 可寫(xiě)層 ,鏡像本身是 只讀的 ,保持不變,容器與鏡像的關(guān)系,就如同面向編程中對(duì)象與類(lèi)之間的關(guān)系。因?yàn)槿萜魇峭ㄟ^(guò)鏡像來(lái)創(chuàng)建的,所以必須先有鏡像才能創(chuàng)建容器,而生成的容器是一個(gè)獨(dú)立于宿主機(jī)的隔離進(jìn)程,并且有屬于容器自己的網(wǎng)絡(luò)和命名空間
1.查看所的容器?
查看所有運(yùn)行的容器
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
757e72f3b32e rabbitmq:3.7.16-management "docker-entrypoint.s…" 22 months ago Up 6 hours 0.0.0.0:4369->4369/tcp, :::4369->4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp, 0.0.0.0:25672->25672/tcp, :::25672->25672/tcp, 15671/tcp dockerlnmp_rabbitmq_1
e526c3df36f7 dockerlnmp_mysql "docker-entrypoint.s…" 22 months ago Up 6 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
?查看所有容器(既有運(yùn)行中的容器,也有被銷(xiāo)毀的容器)
[root@localhost zph]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8def313c4f6e hello-world "/hello" 5 hours ago Exited (0) 5 hours ago intelligent_johnson
59222227ec81 dockerlnmp_nginx "/usr/bin/supervisor…" 21 months ago Exited (0) 19 months ago dockerlnmp_nginx_1
ed99ccb1576f dockerlnmp_php "/usr/bin/supervisor…" 21 months ago Exited (0) 19 months ago dockerlnmp_php_1
9299363ef447 composer:latest "/docker-entrypoint.…" 21 months ago Exited (1) 19 months ago composer
b4791a6baf69 c6f915833fb6 "/bin/sh -c 'mkdir -…" 21 months ago Exited (1) 21 months ago
2.docker run 參數(shù)
[root@localhost zph]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
常用命令如下:docker run:????????創(chuàng)建一個(gè)新的容器并運(yùn)行一個(gè)命令????????docker run 是日常用的最頻繁用的命令之一,同樣也是較為復(fù)雜的命令之一命令格式:???????? docker run [OPTIONS] IMAGE [COMMAND] [ARG...]OPTIONS :選項(xiàng)????????-i :表示啟動(dòng)一個(gè)可交互的容器,并持續(xù)打開(kāi)標(biāo)準(zhǔn)輸入????????-t: 表示使用終端關(guān)聯(lián)到容器的標(biāo)準(zhǔn)輸入輸出上????????-d :表示容器放置后臺(tái)運(yùn)行????????--rm:退出后即刪除容器????????--name :表示定義容器唯一名稱(chēng)????????-p 映射端口????????-v 指定路徑掛載數(shù)據(jù)卷????????-e 運(yùn)行容器傳遞環(huán)境變量IMAGE :????????表示要運(yùn)行的鏡像COMMAND :????????表示啟動(dòng)容器時(shí)要運(yùn)行的命令
3.-it 啟動(dòng)一個(gè)交互式容器
docker run 啟動(dòng)一個(gè)交互式容器在容器內(nèi)執(zhí)行/bin/bash 命令:
docker run -it --name centos7 centos /bin/bash 或者 docker run -it??--name centos7 5d0da3dc9764? /bin/bash?
5d0da3dc9764? ?為centos鏡像的IMAGE ID, --name 給這個(gè)容器命名
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost zph]# docker run -it --name centos7 9c7a54a9a43c /bin/bash
[root@8780efa9cf48 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
可以在容器中執(zhí)行操作命令,如:ls cat,還可以在容器中安裝一些軟件,相當(dāng)于把容器當(dāng)作一個(gè)操作系統(tǒng),只不過(guò)這個(gè)操作系統(tǒng)是一個(gè)比較精簡(jiǎn)的操作系統(tǒng)而已?
[root@0f0b83575aa4 /]# 進(jìn)入容器后查看容器的IP地址
[root@0f0b83575aa4 /]# ifconfig
bash: ifconfig: command not found
#s說(shuō)明沒(méi)有這個(gè)命令,使用yum安裝命令操作
[root@0f0b83575aa4 /]# yum install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
...
#安裝成功后,使用ifconfig查看就可以了
[root@0f0b83575aa4 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 17779 bytes 32924504 (31.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8464 bytes 495661 (484.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?退出容器
????????exit: 容器停止并退出
????????ctrl+p+q:容器不停止退出
4.--rm 啟動(dòng)一個(gè)退出即刪除容器?
創(chuàng)建一個(gè)非交互式的容器用完就刪除,也就是退出容器后就銷(xiāo)毀
docker run --rm centos7 /bin/bash
創(chuàng)建一個(gè)交互式的容器用完就刪
docker run -it --rm centos7 /bin/bash
exit 退出后,執(zhí)行命令docker ps -a,發(fā)現(xiàn)上面的centos7已經(jīng)被銷(xiāo)毀了,沒(méi)有這個(gè)記錄
5.-d 啟動(dòng)一個(gè)后臺(tái)容器
開(kāi)啟一個(gè)后臺(tái)交互的容器,注意啟動(dòng)的時(shí)候加上-it,如果不加的話 docker 啟動(dòng)容器會(huì)自動(dòng)停
止,也就是說(shuō),加上-d后,就是啟動(dòng)這個(gè)容器,然后在后臺(tái)運(yùn)行,不進(jìn)入這個(gè)容器,eg:
# 啟動(dòng)一個(gè)centos容器,并命名,執(zhí)行/bin/bash,在后臺(tái)運(yùn)行,不進(jìn)入容器內(nèi)部
[root@localhost zph]# docker run -it --name centos7-bak -d eeb6ee3f44bd /bin/bash
# 該命令執(zhí)行后,出現(xiàn)下面字符串
48830cb9e3177724a977a0ba1d6f0a1d06ceca016a148a7e9358bab9da3fe652
[root@localhost zph]#
#查看容器:發(fā)現(xiàn)存在cents7-bak這個(gè)容器,說(shuō)明了上面docker run 命令的正確性
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48830cb9e317 eeb6ee3f44bd "/bin/bash" 15 seconds ago Up 13 seconds centos7-bak
?6.exec 進(jìn)入置為后臺(tái)已經(jīng)啟動(dòng)的容器
進(jìn)入一個(gè)容器可以使用 docker exec 或者 docker attac,用得最多的是docker exec這個(gè)命令,文檔如下:? ??
[root@localhost zph]# docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
?docker attach文檔:
[root@localhost zph]# docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
?兩個(gè)命令的區(qū)別:
????????docker exec: 進(jìn)入容器開(kāi)啟一個(gè)新的終端(常用) 執(zhí)行 exit 退出的時(shí)候不會(huì)停止容器,使用docker ps 查看時(shí),容器存在,不會(huì)被銷(xiāo)毀????????docker attach: 進(jìn)入容器正在執(zhí)行的終端 exit 退出會(huì)停止容器,docker ps查看時(shí),容器不存在,已經(jīng)被銷(xiāo)毀了
#查看正在運(yùn)行的容器
[]root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48830cb9e317 eeb6ee3f44bd "/bin/bash" 12 minutes ago Up 12 minutes centos7-bak
#進(jìn)入容器,開(kāi)啟一個(gè)終端
[root@localhost zph]# docker exec -it 48830cb9e317 /bin/bash
7.start 啟動(dòng) stop 停止 restart 重啟容器 exit 退出容器?
#docker ps -a: 查看所有容器(包括正在運(yùn)行的以及被銷(xiāo)毀的)
#docker start: 容器名/容器id 啟動(dòng)容器
#docker stop: 容器名/容器id 停止容器
#docker restart: 容器名/容器id 重啟容器
#退出容器
# exit: 容器停止并退出
# ctrl+p+q:容器不停止退出
8.刪除容器
#docker rm 容器名/容器id :刪除一個(gè)已經(jīng)停止的容器,如果刪除的容器在運(yùn)行中,會(huì)彈出一個(gè)警告
#docker rm -f 容器名/容器id :強(qiáng)制刪除一個(gè)容器,不管是否在運(yùn)行
#docker rm -f $(docker ps -q) :刪除所有容器()docker ps -q: 獲取容器id)
#docker rm $(docker ps -qf status=exited) :根據(jù)容器的狀態(tài),刪除 Exited 狀態(tài)的容器(docker ps -qf status=exited:篩選獲取status=exited的容器id)
9.docker ps講解
[root@localhost zph]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running):展示所有容器
-f, --filter filter Filter output based on conditions provided:篩選
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs:展示容器id
-s, --size Display total file sizes
10查看容器操作日志
#docker logs 幫助文檔
[root@localhost zph]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output: 跟蹤日志輸出
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) :顯示某個(gè)開(kāi)始時(shí)間的所有日志
-n, --tail string Number of lines to show from the end of the logs (default "all"):僅列出最新 N 條容器日
-t, --timestamps Show timestamps:顯示時(shí)間戳
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
#運(yùn)行容器,給容器取一個(gè)別名,并進(jìn)入容器的bash
[root@localhost zph]# docker run -it --name centos7 eeb6ee3f44bd /bin/bash
[root@63b354d40a1a /]#
[root@63b354d40a1a /]# cd /home/
[root@63b354d40a1a home]# ll
total 0
[root@63b354d40a1a home]# mkdir git
[root@63b354d40a1a home]# cd git/
[root@63b354d40a1a git]# touch text.txt
[root@63b354d40a1a git]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 24 15:26 text.txt
#執(zhí)行ctrl+q+p:退出不停止容器操作
[root@63b354d40a1a git]# ^P[root@localhost zph]#
#查看正在運(yùn)行的容器
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63b354d40a1a eeb6ee3f44bd "/bin/bash" About a minute ago Up About a minute centos7
#查看進(jìn)入容器后的操作日志
[root@localhost zph]# docker logs 63b354d40a1a
[root@63b354d40a1a /]#
[root@63b354d40a1a /]# cd /home/
[root@63b354d40a1a home]# ll
total 0
[root@63b354d40a1a home]# mkdir git
[root@63b354d40a1a home]# cd git/
[root@63b354d40a1a git]# touch text.txt
[root@63b354d40a1a git]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 24 15:26 text.txt
[root@localhost zph]#
11.docker commit 容器轉(zhuǎn)換為鏡像
鏡像是沒(méi)有寫(xiě)入權(quán)限的,但是可以修改容器把容器制作為鏡像,通過(guò)鏡像啟動(dòng)一個(gè)容器 給容器寫(xiě)入內(nèi)容,安裝相關(guān)的軟件,然后把這個(gè)容器轉(zhuǎn)換為鏡像,然后打包上傳到hub,別人就可以使用該鏡像了,以alpine為例:
#拉取alpine鏡像
[root@localhost zph]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
96526aa774ef: Pull complete
Digest: sha256:eece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
#查看是否存在alpine鏡像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 8ca4688f4f35 3 weeks ago 7.34MB
#運(yùn)行alpine鏡像,啟動(dòng)容器,并給容器命名,執(zhí)行/bin/bash命令
[root@localhost zph]# docker run -it --name myalpine 8ca4688f4f35 /bin/bash
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown.
#上面報(bào)錯(cuò)了: 提示沒(méi)有/bin/bash這個(gè)命令
#重新啟動(dòng)容器,執(zhí)行/bin/sh命令
[root@localhost zph]# docker run -it --name myalpine 8ca4688f4f35 /bin/sh
docker: Error response from daemon: Conflict. The container name "/myalpine" is already in use by container "959ece20098efb44b3c1744bb5df49a4d4caa78c362fd0eacc22fc53494f9f65". You have to remove (or rename) that container to be able to reuse that name.
#還是報(bào)錯(cuò)了:提示容器已經(jīng)存在,容器名稱(chēng)存在了
#通過(guò)docker ps -a 查看,果然剛才啟動(dòng)的容器存在,解決辦法:刪除這個(gè)容器
root@localhost zph]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
959ece20098e 8ca4688f4f35 "/bin/bash" 40 seconds ago Created myalpine
#刪除容器
[root@localhost zph]# docker rm 959ece20098e
959ece20098e
#重新執(zhí)行鏡像,啟動(dòng)容器,并執(zhí)行/bin/sh 命令,然后在容器中寫(xiě)入內(nèi)容,退出
[root@localhost zph]# docker run -it --name myalpine 8ca4688f4f35 /bin/sh
/ # cd root/
~ # ls
~ # echo test docker commit > test.txt
~ # ls
test.txt
~ # exit
#查看容器:發(fā)現(xiàn)myalpine已經(jīng)退出,因?yàn)閳?zhí)行了exit,沒(méi)問(wèn)題
[root@localhost zph]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37dce69c265 8ca4688f4f35 "/bin/sh" About a minute ago Exited (0) 22 seconds ago myalpine
#把myalpine制作成鏡像,使用docker commit 命令
#查看docker commit命令文檔: docker commit [參數(shù):-a,-c,...] 容器名/容器id 鏡像名[:版本]
[root@localhost zph]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
#制作鏡像
[root@localhost zph]# docker commit c37dce69c265 docker.io/library/myalpine:v1
sha256:dd14c18efe091413581205f414dacca80d954e85cfb28208e7e610e1fb3c4f6e
#查看是否制作鏡像成功
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myalpine v1 dd14c18efe09 14 seconds ago 7.34MB
#上面存在myalpine:v1,說(shuō)明制作鏡像成功了
#驗(yàn)證鏡像: 運(yùn)行鏡像,開(kāi)啟這個(gè)容器
[root@localhost zph]# docker run -it -d --name myalpine.v1 dd14c18efe09 /bin/sh
cde912eb360c67b8becd8d70562929b08205440f8ba552b6eea80e51ebb20f78
#查看這個(gè)容器是否啟動(dòng)
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cde912eb360c dd14c18efe09 "/bin/sh" 9 seconds ago Up 5 seconds myalpine.v1
#進(jìn)入容器驗(yàn)證
[root@localhost zph]# docker exec -it cde912eb360c /bin/sh
/ # cd root/
~ # ls
test.txt
~ # cat test.txt
test docker commit
~ # exit
#里面有創(chuàng)建的text.txt,說(shuō)明鏡像創(chuàng)建成功
12.鏡像的導(dǎo)入導(dǎo)出
可以把制作好的鏡像發(fā)布到 dockerHub上,也可以把制作好的鏡像導(dǎo)出發(fā)給別人
#查看所有鏡像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myalpine v1 dd14c18efe09 14 hours ago 7.34MB
#導(dǎo)出鏡像
[root@localhost zph]# docker save myalpine > myalpine.tar
[root@localhost zph]# ll
總用量 7464
drwxr-xr-x. 2 zph zph 6 12月 23 2021 公共
drwxr-xr-x. 2 zph zph 6 12月 23 2021 模板
drwxr-xr-x. 2 zph zph 6 12月 23 2021 視頻
drwxr-xr-x. 2 zph zph 6 12月 23 2021 圖片
drwxr-xr-x. 2 zph zph 6 12月 23 2021 文檔
drwxr-xr-x. 2 zph zph 6 12月 23 2021 下載
drwxr-xr-x. 2 zph zph 6 12月 23 2021 音樂(lè)
drwxr-xr-x. 2 zph zph 6 12月 23 2021 桌面
-rw-r--r-- 1 root root 7641600 10月 25 07:26 myalpine.tar
#刪除鏡像
[root@localhost zph]# docker rmi dd14c18efe09
#查看所有鏡像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
#導(dǎo)入鏡像
[root@localhost zph]# docker load < myalpine.tar
Loaded image: myalpine:v1
#查看所有鏡像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myalpine v1 dd14c18efe09 14 hours ago 7.34MB
#校驗(yàn)鏡像是否成功
[root@localhost zph]# docker run -it --name myalpine.v1 dd14c18efe09 /bin/bash
#不報(bào)錯(cuò)就成功了
13.docker cp 實(shí)現(xiàn)數(shù)據(jù)拷貝
docker cp :用于容器與主機(jī)之間的數(shù)據(jù)拷貝
[root@localhost zph]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
#查看目錄
[root@localhost zph]# pwd
/home/zph
#創(chuàng)建文件夾
[root@localhost zph]# mkdir www
[root@localhost zph]# cd www/
[root@localhost www]# ll
總用量 0
#創(chuàng)建文件并寫(xiě)入數(shù)據(jù)
[root@localhost www]# echo test.docker > test.txt
[root@localhost www]# ll
總用量 4
-rw-r--r-- 1 root root 12 10月 25 07:51 test.txt
[root@localhost www]# cd ..
#查看images
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 2 years ago 204MB
#查看運(yùn)行的容器
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63b354d40a1a eeb6ee3f44bd "/bin/bash" 23 hours ago Up 23 hours centos7
#進(jìn)入容器
[root@localhost zph]# docker exec -it 63b354d40a1a /bin/bash
#查看/下是否存在www目錄:發(fā)現(xiàn)不存在該目錄
[root@63b354d40a1a /]# ll
total 12
-rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Oct 24 15:25 dev
drwxr-xr-x 1 root root 66 Oct 24 15:24 etc
drwxr-xr-x 1 root root 17 Oct 24 15:26 home
lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x 2 root root 6 Apr 11 2018 media
drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x 255 root root 0 Oct 24 15:24 proc
dr-xr-x--- 2 root root 114 Nov 13 2020 root
drwxr-xr-x 11 root root 148 Nov 13 2020 run
lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Oct 24 15:24 sys
drwxrwxrwt 7 root root 132 Nov 13 2020 tmp
drwxr-xr-x 13 root root 155 Nov 13 2020 usr
drwxr-xr-x 18 root root 238 Nov 13 2020 var
#退出
[root@63b354d40a1a /]# exit
exit
#cp /home/zph/www/目錄下的所有文件到容器的/下
[root@localhost zph]# docker cp /home/zph/www/ 63b354d40a1a:/
#進(jìn)入容器查看是否cp成功
[root@localhost zph]# docker exec -it 63b354d40a1a /bin/bash
#ll查看:發(fā)現(xiàn)存在www,說(shuō)明cp成功了
[root@63b354d40a1a /]# ll
total 12
-rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Oct 24 15:25 dev
drwxr-xr-x 1 root root 66 Oct 24 15:24 etc
drwxr-xr-x 1 root root 17 Oct 24 15:26 home
lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x 2 root root 6 Apr 11 2018 media
drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x 255 root root 0 Oct 24 15:24 proc
dr-xr-x--- 1 root root 27 Oct 25 14:53 root
drwxr-xr-x 11 root root 148 Nov 13 2020 run
lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Oct 24 15:24 sys
drwxrwxrwt 7 root root 132 Nov 13 2020 tmp
drwxr-xr-x 13 root root 155 Nov 13 2020 usr
drwxr-xr-x 18 root root 238 Nov 13 2020 var
drwxr-xr-x 2 root root 22 Oct 25 14:51 www
[root@63b354d40a1a /]# cd www/
[root@63b354d40a1a www]# ll
total 4
-rw-r--r-- 1 root root 12 Oct 25 14:51 test.txt
#在www下新建文件并寫(xiě)入數(shù)據(jù),然后退出
[root@63b354d40a1a www]# echo test2.com > test2.txt
[root@63b354d40a1a www]# exit
exit
#cp容器中的www下的所有文件到系統(tǒng)的/var下
[root@localhost zph]# docker cp 63b354d40a1a:www/ /var
#查看是否cp成功:發(fā)現(xiàn)成功了
[root@localhost zph]# ll /var/www/
總用量 8
-rw-r--r-- 1 root root 10 10月 25 07:54 test2.txt
-rw-r--r-- 1 root root 12 10月 25 07:51 test.txt
[root@localhost zph]#
[上一節(jié)][Docker]一.Docker 簡(jiǎn)介與安裝文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-771964.html
[下一節(jié)][Docker]三.Docker 部署nginx,以及映射端口,掛載數(shù)據(jù)卷?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-771964.html
到了這里,關(guān)于[Docker]二.Docker 鏡像,倉(cāng)庫(kù),容器介紹以及詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!