日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Docker镜像与容器基本操作

發布時間:2024/3/24 编程问答 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Docker镜像与容器基本操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Docker鏡像與容器基本操作

    • 一、Docker基本命令
      • 1、查看docker版本
      • 2、搜索鏡像和dockerhub
      • 3、下載鏡像
      • 4、docker run
      • 5、查看所有狀態容器
    • 二、docker鏡像操作
      • 1、查看鏡像列表
      • 2、獲取鏡像信息
      • 3、添加鏡像標簽
      • 4、刪除鏡像
      • 5、鏡像導出與鏡像導入
    • 三、docker容器操作
      • 1、查詢容器
      • 2、創建容器
      • 3、啟動容器
      • 4、停止容器
      • 5、持續后臺運行
      • 6、進入容器
        • 6.1run
        • 6.2exec
      • 7、容器導出與容器導入
        • 7.1容器導出
        • 7.2容器導入
      • 8、批量刪除容器

一、Docker基本命令

1、查看docker版本

docker version && docker info

[root@localhost ~]#docker version Client: Docker Engine - CommunityVersion: 20.10.12API version: 1.41Go version: go1.16.12Git commit: e91ed57Built: Mon Dec 13 11:45:41 2021OS/Arch: linux/amd64Context: defaultExperimental: trueServer: Docker Engine - CommunityEngine:Version: 20.10.12API version: 1.41 (minimum version 1.12)Go version: go1.16.12Git commit: 459d0dfBuilt: Mon Dec 13 11:44:05 2021OS/Arch: linux/amd64Experimental: falsecontainerd:Version: 1.4.12GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5drunc:Version: 1.0.2GitCommit: v1.0.2-0-g52b36a2docker-init:Version: 0.19.0GitCommit: de40ad0[root@localhost ~]#docker info Client:Context: defaultDebug Mode: falsePlugins:app: Docker App (Docker Inc., v0.9.1-beta3)buildx: Docker Buildx (Docker Inc., v0.7.1-docker)scan: Docker Scan (Docker Inc., v0.12.0)Server:Containers: 1 ##鏡像數量Running: 1 ##運行中的Paused: 0 ##暫停的容器Stopped: 0 ##停止的Images: 2Server Version: 20.10.12Storage Driver: overlay2Backing Filesystem: xfsSupports d_type: trueNative Overlay Diff: trueuserxattr: falseLogging Driver: json-fileCgroup Driver: cgroupfsCgroup Version: 1Plugins:Volume: localNetwork: bridge host ipvlan macvlan null overlayLog: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslogSwarm: inactiveRuntimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runcDefault Runtime: runcInit Binary: docker-initcontainerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5drunc version: v1.0.2-0-g52b36a2init version: de40ad0Security Options:seccompProfile: defaultKernel Version: 3.10.0-693.el7.x86_64Operating System: CentOS Linux 7 (Core)OSType: linuxArchitecture: x86_64CPUs: 4Total Memory: 3.686GiBName: localhost.localdomainID: BXSB:URMW:4X4Q:YWTU:XCLJ:P6OG:CCIM:S4Q2:ZSB4:FWHB:QV64:J2TSDocker Root Dir: /var/lib/dockerDebug Mode: falseRegistry: https://index.docker.io/v1/Labels:Experimental: falseInsecure Registries:127.0.0.0/8Registry Mirrors:https://y9hsd18p.mirror.aliyuncs.com/Live Restore Enabled: false

2、搜索鏡像和dockerhub

docker search 鏡像名稱

[root@localhost ~]#docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 16136 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2108 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 820 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 312 linuxserver/nginx An Nginx container, brought to you by LinuxS… 160 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 151 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 150 [OK] alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 114 [OK] nginxdemos/hello NGINX webserver that serves a simple page co… 82 [OK] privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 61 [OK] nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 59 nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 57 nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 34 staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 25 [OK] nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 23 schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK] centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 16 centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 13 [OK] bitwarden/nginx The Bitwarden nginx web server acting as a r… 12 mailu/nginx Mailu nginx frontend 10 [OK] sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 3 [OK] wodby/nginx Generic nginx 1 [OK] arnau/nginx-gate Docker image with Nginx with Lua enabled on … 1 [OK]

3、下載鏡像

docker pull 鏡像名稱

默認是從docker hub上

[root@localhost ~]#docker pull nginx Using default tag: latest latest: Pulling from library/nginx a2abf6c4d29d: Pull complete a9edb18cadd1: Pull complete 589b7251471a: Pull complete 186b1aaa4aa6: Pull complete b4df32aa5a72: Pull complete a0bcbecc962e: Pull complete Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest

4、docker run

docker run 鏡像名稱

run —運行

檢測本地有沒有該鏡像(沒有的話直接到docker hub上下載)

②create(將鏡像創建為容器)+ start 將創建好的容器運行起來

[root@localhost ~]#docker run hello-world Unable to find image 'hello-world:latest' locally ##開始沒有helloworld latest: Pulling from library/hello-world 2db29710123e: Pull complete ##去庫中拉 Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f Status: Downloaded newer image for hello-world:latest ##下載最新版本Hello from Docker! This message shows that your installation appears to be working correctly. ##已經正常運行##run運行過程 To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon. ##客戶端和服務器建立連接2. The Docker daemon pulled the "hello-world" image from the Docker Hub. ##服務端守護進程去倉庫拉(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading. ##基于下載的鏡像去運行容器,運行的就是你看到的helloworld監本4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/

工作流程

①docker client客戶端連接到了服務端(服務端是以一個守護進程的形式跑在操作系統里面的) restful api典型的c/s架構
②由docker服務端的守護進程從docker hub 上下載了鏡像(Ps:服務端會先檢查本地系統是否有這個鏡像)
③服務端創建了一個新的容器,然后從拉去的這個鏡像啟動了一個容器,容器執行了腳本/可執行程序讓我們可以查看/使用(client)
④docker 服務端把這些信息流(傳遞)返回到客戶端并展示出來,(展示在終端上)
docker client可以是多種形式,比如"docker"命令工具所在的終端

5、查看所有狀態容器

docker ps -a

-a:顯示所有容器

[root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d611f425588 hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago beautiful_sanderson 1c165bc5beaa b0f53fa23af6 "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago zen_wright b66ea884146f b66755a4ef1c "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago gracious_keldysh c08cd97f28fe f7720be4ebf1 "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago gallant_shamir 43f5d77173c5 f7720be4ebf1 "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago wonderful_banzai 5f38c8aec4aa f7720be4ebf1 "/bin/sh -c './cmake…" 2 days ago Exited (126) 2 days ago gifted_lewin

注:

字段說明

CONTAINER ID:容器的ID號 IMAGE:加載的鏡像 COMMAND :運行的程序 CREATED :創建時間 STATUS:當前的狀態 PORTS:端口映射 NAMES:名稱

二、docker鏡像操作

1、查看鏡像列表

docker images—》查看當前docker下的下載鏡像信息

[root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> b0f53fa23af6 2 days ago 784MB <none> <none> b66755a4ef1c 2 days ago 784MB <none> <none> f7720be4ebf1 2 days ago 784MB nginx latest 605c77e624dd 2 weeks ago 141MB centos 7 eeb6ee3f44bd 4 months ago 204MB [root@localhost ~]#

2、獲取鏡像信息

docker inspect 鏡像ID

[root@localhost ~]#docker inspect eeb6ee3f44bd

3、添加鏡像標簽

docker tag 舊標簽 新標簽

[root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> b0f53fa23af6 2 days ago 784MB <none> <none> b66755a4ef1c 2 days ago 784MB <none> <none> f7720be4ebf1 2 days ago 784MB nginx latest 605c77e624dd 2 weeks ago 141MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos 7 eeb6ee3f44bd 4 months ago 204MB [root@localhost ~]#docker tag hello-world:latest hello-world:ding [root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> b0f53fa23af6 2 days ago 784MB <none> <none> b66755a4ef1c 2 days ago 784MB <none> <none> f7720be4ebf1 2 days ago 784MB nginx latest 605c77e624dd 2 weeks ago 141MB hello-world ding feb5d9fea6a5 3 months ago 13.3kB hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos 7 eeb6ee3f44bd 4 months ago 204MB

4、刪除鏡像

docker rmi 鏡像名稱 :當一個鏡像有多個標簽時,只是刪除其中指定的標簽

docker rmi 鏡像ID:會徹底刪除該鏡像

批量刪除:docker rmi ``docker images -q

注:

rmi 是 rm image 的簡寫

不能刪除運行中的容器,想要刪除運行中的容器的加:-f

-f:強制刪除

[root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 3 months ago 13.3kB [root@localhost ~]#docker rmi hello-world:latest Untagged: hello-world:latest Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359 [root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE[root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 3 months ago 13.3kB [root@localhost ~]#docker rmi feb5d9fea6a5 Untagged: hello-world:latest Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359 [root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE

5、鏡像導出與鏡像導入

導出:docker save -o 文件名 鏡像名

導入:docker load < 導出的文件

[root@localhost ~]#docker save -o nginx_1.21 nginx:latest ##鏡像導出 [root@localhost ~]#ls anaconda-ks.cfg nginx_1.21 模板 圖片 下載 桌面 initial-setup-ks.cfg 公共 視頻 文檔 音樂[root@localhost ~]#docker rmi nginx:latest ##刪除已經導出的鏡像 Untagged: nginx:latest Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85 Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5 Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14 Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4 Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5 Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f [root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos 7 eeb6ee3f44bd 4 months ago 204MB[root@localhost ~]#ls anaconda-ks.cfg nginx_1.21 模板 圖片 下載 桌面 initial-setup-ks.cfg 公共 視頻 文檔 音樂 [root@localhost ~]#docker load < nginx_1.21 ##鏡像導入 2edcec3590a4: Loading layer 83.86MB/83.86MB e379e8aedd4d: Loading layer 62MB/62MB b8d6e692a25e: Loading layer 3.072kB/3.072kB f1db227348d0: Loading layer 4.096kB/4.096kB 32ce5f6a5106: Loading layer 3.584kB/3.584kB d874fd2bc83b: Loading layer 7.168kB/7.168kB Loaded image: nginx:latest [root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 2 weeks ago 141MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos 7 eeb6ee3f44bd 4 months ago 204MB

三、docker容器操作

1、查詢容器

docker ps -a—查詢容器的所有狀態

[root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 461b9ca23242 nginx:latest "/docker-entrypoint.…" 27 seconds ago Exited (0) 2 seconds ago nostalgic_sinoussi

2、創建容器

docker create 選項 鏡像名稱 /bin/bash

注:/bin/bash–給予一個終端環境支持

選項:-i–讓容器的輸入保持打開 -t–分配一個偽終端 -d–后臺守護進程的方式運行

[root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 2 weeks ago 141MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos 7 eeb6ee3f44bd 4 months ago 204MB [root@localhost ~]#docker create -it nginx:latest /bin/bash f47cceadb2c027f86af1387399f070aa33761507ec2cef6b9292cfb7f2d1bbf5 [root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 7 seconds ago Created inspiring_sammet ##容器狀態:Created

3、啟動容器

docker start 容器ID

[root@localhost ~]#docker start f47cceadb2c0 f47cceadb2c0 [root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 seconds 80/tcp inspiring_sammet ##容器狀態:Up 4 seconds ##參數 CONTAINER ID:容器ID IMAGE :容器使用的鏡像 COMMAND :命令 CREATED :創建的時間/已經創建的時間 STATUS:目前的專狀態PORTS :容器內部暴露的端口NAMES:容器本身的的名稱

創建并啟動容器:docker run -d 鏡像名稱 /usr/bin/bash -c ls / (一次性執行)

**注:**run包含了create與start

[root@localhost ~]#docker run centos:7 /usr/bin/bash -c ls / anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60b518e6723c centos:7 "/usr/bin/bash -c ls…" 18 seconds ago Exited (0) 17 seconds ago wonderful_banzai f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 15 minutes ago Up 10 minutes 80/tcp inspiring_sammet

4、停止容器

docker stop 容器ID

[root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 18 minutes ago Up 13 minutes 80/tcp inspiring_sammet [root@localhost ~]#docker stop f47cceadb2c0 f47cceadb2c0 [root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 18 minutes ago Exited (137) 3 seconds ago inspiring_sammet

5、持續后臺運行

執行后不退出,以守護進程方式執行持續性任務

docker run -it 鏡像名字 /bin/bash -c “while true;do echo hello;done”

“while true;do echo hello;done” --賦予一個字循環

[root@localhost ~]#docker run -d centos:7 /bin/bash -c "while true;do echo hello;done" ae3edb4f9876dfa3fb84a78c86236f2b132a3b68e8804276e1a260a3ae98cfda [root@localhost ~]#docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ae3edb4f9876 centos:7 "/bin/bash -c 'while…" 9 seconds ago Up 8 seconds loving_tereshkova 669a0bfb5290 centos:7 "/usr/bin/bash -c ls…" 2 minutes ago Exited (0) 2 minutes ago reverent_borg d79bdefa9ad5 nginx:latest "/docker-entrypoint.…" 3 minutes ago Exited (137) 2 minutes ago loving_lamarr

6、進入容器

6.1run

run–一次性進入

docker run -it 鏡像名稱 /bin/bash

6.2exec

exec–永久性進入

docker exec -it 容器ID /bin/bash

**注:**容器必須為開啟狀態

Ps:

docker run -it會創建前臺進程,但是會在輸入exit后終止進程,

docker attach 會通過連接stdin,連接到容器內輸入輸出流,會在輸入exit后終止容器進程.

docker exec -it 會連接到容器,可以像sSH一樣進入容器內部,進行操作,可以通過exit退出容器,不影響容器運行。

7、容器導出與容器導入

7.1容器導出

docker export 容器ID > 文件名

[root@localhost ~]#docker export d0b521872a42 > centos_7 [root@localhost ~]#ls anaconda-ks.cfg centos_7 initial-setup-ks.cfg 公共 模板 視頻 圖片 文檔

7.2容器導入

docker inport 容器ID > 導出的文件名(容器)指定鏡像名稱

[root@localhost ~]#ls anaconda-ks.cfg centos_7 initial-setup-ks.cfg 公共 模板 視頻 圖片 文檔 下載 音樂 桌面 [root@localhost ~]#docker import centos_7 centos:lnmp sha256:5b6c2cdb58d692112fb592d80e976816bb407fe6c9a059cb6a9b749c64cfa884 [root@localhost ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos lnmp 5b6c2cdb58d6 12 seconds ago 204MB centos 7 eeb6ee3f44bd 4 months ago 204MB

8、批量刪除容器

docker rm 選項

1.#不能刪除運行狀態的容器,只能-f強制刪除,或者先停止再刪除 docker rm 3224eb0448792.#已經退出的容器,可以直接刪除 docker rm 1270a6791069 3.#基于名稱匹配的方式刪除 docker rm -f distracted_panini4.#刪除所有運行狀態的容器 docker rm -f `docker ps -q`5.#刪除所有容器 docker rm -f `docker ps -aq`6.#有選擇性的批量刪除 (正則匹配) docker ps -a l awk ' {print "docker rm "$1}'l bash7.#刪除退出狀態的容器 for i in `dockef ps -a l grep -i exit / awk '{print $1}' '; do docker rm -f $i;done 5b6c2cdb58d6 12 seconds ago 204MB

centos 7 eeb6ee3f44bd 4 months ago 204MB

### 8、批量刪除容器docker rm 選項 ```bash 1.#不能刪除運行狀態的容器,只能-f強制刪除,或者先停止再刪除 docker rm 3224eb0448792.#已經退出的容器,可以直接刪除 docker rm 1270a6791069 3.#基于名稱匹配的方式刪除 docker rm -f distracted_panini4.#刪除所有運行狀態的容器 docker rm -f `docker ps -q`5.#刪除所有容器 docker rm -f `docker ps -aq`6.#有選擇性的批量刪除 (正則匹配) docker ps -a l awk ' {print "docker rm "$1}'l bash7.#刪除退出狀態的容器 for i in `dockef ps -a l grep -i exit / awk '{print $1}' '; do docker rm -f $i;done

總結

以上是生活随笔為你收集整理的Docker镜像与容器基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。