【使用示例】

[root@docker ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
Digest: sha256:9ae97d36d26566ff84e8893c64a6dc4fe8ca6d1144bf5b87b2b85a32def253c7
Status: Image is up to date for busybox:latest
docker.io/library/busybox:latest

[root@docker ~]# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?   ###遇到错误,多半是配置文件 /etc/docker/daemon.json 的问题
[root@docker ~]# 
[root@docker ~]# vim /etc/docker/daemon.json
[root@docker ~]# systemctl restart docker

#查看镜像信息
docker image inspect nginx:1.26-alpine

#查看镜像结构

[root@docker ~]# docker history nginx:latest  
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
5ef79149e0ec   13 days ago   CMD ["nginx" "-g" "daemon off;"]                0B        buildkit.dockerfile.v0……
<missing>      2 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      2 weeks ago   /bin/sh -c #(nop) ADD file:3d9897cfe027ecc7c…   74.8MB

#导出镜像
[root@docker ~]# docker image save -o nginx-test.tar.gz nginx:latest
[root@docker ~]# ls
[root@docker ~]# du -sh nginx-test.tar.gz 
183M    nginx-test.tar.gz
#删除镜像
[root@docker ~]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
nginx                       latest    5ef79149e0ec   13 days ago     188MB
registry                    latest    cfb4d9904335   11 months ago   25.4MB
nginx                       1.23      a7be6198544f   15 months ago   142MB
busybox                     latest    65ad0d468eb1   15 months ago   4.26MB
172.25.254.5:5000/busybox   latest    65ad0d468eb1   15 months ago   4.26MB
172.25.254.6:5000/busybox   latest    65ad0d468eb1   15 months ago   4.26MB
timinglee/game2048          latest    19299002fdbe   7 years ago     55.5MB
timinglee/mario             latest    9a35a9e43e8c   8 years ago     198MB
[root@docker ~]# docker rmi nginx:1.23 
Untagged: nginx:1.23
Deleted: sha256:a7be6198544f09a75b26e6376459b47c5b9972e7aa742af9f356b540fe852cd4
Deleted: sha256:b142903ff5d25e779c293678ce1bb71604778bc243cda8c26180675454fbf11c
Deleted: sha256:185fa8597cdc03f83ab9ec3fe21d8ac6fbe49fa65cf8422f05d1d07b06b25fce
Deleted: sha256:1e3de4dfc3a245258917d9cd0860bd30969e1b430ecba95b1eaf2666d8882d24
Deleted: sha256:95b14e2b8329c1fcec4e1df001aac4874bc2247281ae96cdfe355847faf4caa9
Deleted: sha256:8566a5cb57a5d27b0eba5d952429bc542c05853014dc2c8f362540e7533fbff9
Deleted: sha256:8cbe4b54fa88d8fc0198ea0cc3a5432aea41573e6a0ee26eca8c79f9fbfa40e3
[root@docker ~]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
nginx                       latest    5ef79149e0ec   13 days ago     188MB
registry                    latest    cfb4d9904335   11 months ago   25.4MB
172.25.254.5:5000/busybox   latest    65ad0d468eb1   15 months ago   4.26MB
172.25.254.6:5000/busybox   latest    65ad0d468eb1   15 months ago   4.26MB
busybox                     latest    65ad0d468eb1   15 months ago   4.26MB
timinglee/game2048          latest    19299002fdbe   7 years ago     55.5MB
timinglee/mario             latest    9a35a9e43e8c   8 years ago     198MB
#导入镜像
[root@docker ~]# docker load -i nginx-1.23.tar.gz 
8cbe4b54fa88: Loading layer [==================================================>]  84.01MB/84.01MB
5dd6bfd241b4: Loading layer [==================================================>]  62.51MB/62.51MB
043198f57be0: Loading layer [==================================================>]  3.584kB/3.584kB
2731b5cfb616: Loading layer [==================================================>]  4.608kB/4.608kB
6791458b3942: Loading layer [==================================================>]  3.584kB/3.584kB
4d33db9fdf22: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:1.23
[root@docker ~]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
nginx                       latest    5ef79149e0ec   13 days ago     188MB
registry                    latest    cfb4d9904335   11 months ago   25.4MB
nginx                       1.23      a7be6198544f   15 months ago   142MB
172.25.254.5:5000/busybox   latest    65ad0d468eb1   15 months ago   4.26MB
172.25.254.6:5000/busybox   latest    65ad0d468eb1   15 months ago   4.26MB
busybox                     latest    65ad0d468eb1   15 months ago   4.26MB
timinglee/game2048          latest    19299002fdbe   7 years ago     55.5MB
timinglee/mario             latest    9a35a9e43e8c   8 years ago     198MB
 
#快速 保存所有镜像
[root@docker ~]# docker save `docker images | awk 'NR>1{print $1":"$2}'` -o images.tar.gz
#快速删除所有镜像
[root@docker ~]# docker rmi `docker images | awk 'NR>1{print $1":"$2}'`  #有容器正在使用的镜像无法删除

【容器常用操作】

运行/运行容器
参数
-d # 后台运行
-i # 交互式运行
-t # 打开一个终端
--name # 指定容器名称
-p # 端口映射 -p 80 8080 把容器 8080 端口映射到本机 80 端口
--rm # 容器停止自动删除容器
--network # 指定容器使用的网络
##不加 -d 会占用终端
[root@docker ~]# docker run --rm --name container1 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: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/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
2024/08/28 19:55:34 [notice] 1#1: using the "epoll" event method
2024/08/28 19:55:34 [notice] 1#1: nginx/1.27.1
2024/08/28 19:55:34 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2024/08/28 19:55:34 [notice] 1#1: OS: Linux 5.14.0-70.13.1.el9_0.x86_64
2024/08/28 19:55:34 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1073741816:1073741816
2024/08/28 19:55:34 [notice] 1#1: start worker processes
2024/08/28 19:55:34 [notice] 1#1: start worker process 30

##在后台运行
[root@docker ~]# docker run -d --rm --name container1 nginx
d9c8ebe2a9a00454c44b0eba15a0cacb5ca3381e559e057e3c215c064d8f2ec7
[root@docker ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
d9c8ebe2a9a0   nginx     "/docker-entrypoint.…"   37 seconds ago   Up 35 seconds   80/tcp    container1
#打开一个终端交替式运行
[root@docker ~]# docker run -i -t --name test1 nginx:latest
 
#端口号
##在后台运行的容器如何查看
[root@docker ~]# docker run -d --name test busybox
968595a42bac6e0cc7b6b0b65b3b452a84f5137ab7f11dc9ea35233f4203dc5c
[root@docker ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS                     PORTS     NAMES
968595a42bac   busybox   "sh"      9 seconds ago   Exited (0) 7 seconds ago             test
[root@docker ~]# docker rm test
test
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
##容器停止时自动删除容器
[root@docker ~]# docker run --rm -d --name test busybox
0bfc7b1f6b47208dee5d674d9b73df6f917bad17999718513c89f61c7c018b92
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
 

【容器网络】

##docker默认网络
[root@docker ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
bcef9685697c   bridge    bridge    local  #桥接
0bb7a7894546   host      host      local   #直连
882e4785cc3b   none      null      local   #none
##默认用的桥接接口
[root@docker ~]# docker run -it --name test busybox
/ # ifconfig               
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr: 172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:17 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2202 (2.1 KiB)  TX bytes:0 (0.0 B)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # exit
##指定none表示没有网络
[root@docker ~]# docker run -it --name test1 --network none busybox
/ # ifconfig
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 

【容器操作:启动、删除.....】

[root@docker ~] # docker stop busybox # 停止容器
[root@docker ~] # docker kill busybox # 杀死容器,可以使用信号
[root@docker ~] # docker start busybox # 开启停止的容器
[root@docker ~] # docker rm centos7 # 删除停止的容器
[root@docker ~] # docker rm -f busybox # 删除运行的容器
[root@docker ~] # docker container prune -f # 删除所有停止的容器
[root@docker ~]# docker run -d --name container5 nginx
d51a33569754a9b415bccb0106d18f5b8b802e24a84e6fdd4c91ad2531e92cdc
[root@docker ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
d51a33569754   nginx     "/docker-entrypoint.…"   12 seconds ago   Up 11 seconds   80/tcp    container5
[root@docker ~]# docker stop container5 
container5
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                     PORTS     NAMES
d51a33569754   nginx     "/docker-entrypoint.…"   49 seconds ago   Exited (0) 6 seconds ago             container5
[root@docker ~]# docker start container5 
container5
[root@docker ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS         PORTS     NAMES
d51a33569754   nginx     "/docker-entrypoint.…"   About a minute ago   Up 8 seconds   80/tcp    container5
#正在运行的删不了
[root@docker ~]# docker start container5 
container5
[root@docker ~]# docker rm container5
Error response from daemon: cannot remove container "/container5": container is running: stop the container before removing or force remove
#强制删除
[root@docker ~]# docker rm -f container5
container5
 
使用示例:
##运行一个容器
[root@docker ~]# docker run -d --name container1 nginx    
108ba95b88024bd08aff0110ebdb26bc416efb2129184f08ebc9157a55c06dba
##
[root@docker ~]# docker exec -it container1 bash
Error response from daemon: container 108ba95b88024bd08aff0110ebdb26bc416efb2129184f08ebc9157a55c06dba is not running
## 开启停止的容器
[root@docker ~]# docker start container1  
container1
[root@docker ~]# docker exec -it container1 bash
root@108ba95b8802:/# 
root@108ba95b8802:/# 
root@108ba95b8802:/# exit
exit
[root@docker ~]#
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS              PORTS     NAMES
108ba95b8802   nginx     "/docker-entrypoint.…"   22 minutes ago   Up About a minute   80/tcp    container1
##删除在运行状态的容器 -f
[root@docker ~]# docker rm -f container1 
container1
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker ~]#
##使用 Ctrl+PQ 键退出交互式,不会删除容器
[root@docker ~]# docker run -it --rm --name container3 busybox
/ # [root@docker ~]#  docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
b4b39f73d829   busybox   "sh"      58 seconds ago   Up 57 seconds             container3
##进入容器
[root@docker ~]# docker attach container3   
/ # 
/ # 
/ # 
##
[root@docker ~]# docker run -it --rm --name container4 busybox
/ # [root@docker ~]# 
[root@docker ~]# docker exec -it container4 ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2342 (2.2 KiB)  TX bytes:0 (0.0 B)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
##查看容器信息
## docker inspect + 容器名称
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
209089c426a2   busybox   "sh"      2 minutes ago   Up 2 minutes             container4
[root@docker ~]# 
[root@docker ~]# docker inspect container4 
 
##删除所有停止的容器
[root@docker ~]# docker run -d --name test1 busybox
6366886727a9702c14159cbcd898d9cac0c79dd38a7039c857d3ba82bf19682d
[root@docker ~]# docker run -d --name test2 busybox
baff9801e3160bf08840370cde91abd32f30553936480cac93f8c180ed0aec3b
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                      PORTS     NAMES
baff9801e316   busybox   "sh"      18 seconds ago   Exited (0) 16 seconds ago             test2
6366886727a9   busybox   "sh"      24 seconds ago   Exited (0) 21 seconds ago             test1
[root@docker ~]# 
[root@docker ~]# docker container prune -f
Deleted Containers:
baff9801e3160bf08840370cde91abd32f30553936480cac93f8c180ed0aec3b
6366886727a9702c14159cbcd898d9cac0c79dd38a7039c857d3ba82bf19682d
Total reclaimed space: 0B
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker ~]# 
 
容器内容提交
[root@docker ~]# docker run -it --name test busybox
/ # ls
bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var
/ # touch examplefile
/ # ls
bin          etc          home         lib64        root         tmp          var
dev          examplefile  lib          proc         sys          usr
/ # exit
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS                          PORTS     NAMES
83d51c73d6b0   busybox   "sh"      2 minutes ago   Exited (0) About a minute ago             test
[root@docker ~]# docker rm test 
test
删除容器后,容器里面创建的文件也会消失。
若想保存,需要提交:
[root@docker ~]# docker run -it --name test busybox
/ # touch examplefile
/ # ls
bin          etc          home         lib64        root         tmp          var
dev           examplefile  lib          proc         sys          usr
/ # [root@docker ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
94e9f911c31a   busybox   "sh"      41 seconds ago   Up 40 seconds             test
[root@docker ~]# docker commit -m "add examplefile" test busybox:test
sha256:535afd41e3e97d392c290ef762e6624a4043402846ae188a02c7fd76b422cdcf
[root@docker ~]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED          SIZE
busybox                     test      535afd41e3e9   11 seconds ago   4.26MB
nginx                       latest    5ef79149e0ec   2 weeks ago      188MB
registry                    latest    cfb4d9904335   11 months ago    25.4MB
nginx                       1.23      a7be6198544f   15 months ago    142MB
busybox                     latest    65ad0d468eb1   15 months ago    4.26MB
172.25.254.5:5000/busybox   latest    65ad0d468eb1   15 months ago    4.26MB
172.25.254.6:5000/busybox   latest    65ad0d468eb1   15 months ago    4.26MB
timinglee/game2048          latest    19299002fdbe   7 years ago      55.5MB
timinglee/mario             latest    9a35a9e43e8c   8 years ago      198MB
[root@docker ~]# 
[root@docker ~]# docker rm -f test 
test
[root@docker ~]# docker run -it --name test busybox:test
/ # ls
bin          etc          home         lib64        root         tmp          var
dev           examplefile  lib          proc         sys          usr
/ #exit
##docker镜像:分层结构
##原本的是一层,我们又提交了一层
##没提交一次,又多一层
[root@docker ~]# docker history busybox:latest
IMAGE          CREATED         CREATED BY                          SIZE      COMMENT
65ad0d468eb1   15 months ago   BusyBox 1.36.1 (glibc), Debian 12   4.26MB 
[root@docker ~]# docker history busybox:test
IMAGE          CREATED         CREATED BY                          SIZE      COMMENT
535afd41e3e9   4 minutes ago   sh                                  21B       add examplefile
65ad0d468eb1   15 months ago   BusyBox 1.36.1 (glibc), Debian 12   4.26MB    
[root@docker ~]# docker image history busybox:test
IMAGE          CREATED          CREATED BY                          SIZE      COMMENT
535afd41e3e9   13 minutes ago   sh                                  21B       add examplefile
65ad0d468eb1   15 months ago    BusyBox 1.36.1 (glibc), Debian 12   4.26MB
21B--->文件源数据
examplefile  11个  10+11

【docker中的文件传输】

#示例:容器中有一个文件,我们想要那个文件,该怎么做呢?
[root@docker ~]# docker run -it --name test busybox:test 
/ # ls
bin          etc          home         lib64        root         tmp          var
dev          examplefile  lib          proc         sys          usr
/ # [root@docker ~]# docker ps
CONTAINER ID   IMAGE          COMMAND   CREATED          STATUS          PORTS     NAMES
027f34e9498d   busybox:test   "sh"      33 seconds ago   Up 33 seconds             test
[root@docker ~]# docker cp test:/examplefile /mnt/
Successfully copied 1.54kB to /mnt/
[root@docker ~]# ls /mnt/
examplefile  hgfs
#示例:如何把本机的文件传到容器中?
[root@docker ~]# docker cp /etc/passwd test:/
Successfully copied 4.1kB to test:/
[root@docker ~]# docker attach test 
/ # 
/ # 
/ # ls
bin          etc          home         lib64        proc         sys          usr
dev          examplefile  lib          passwd       root         tmp          var
/ # 
 
【查询容器内部日志】
[root@docker ~]# docker logs test 
/ # ls
bin          etc          home         lib64        root         tmp          var
dev          examplefile  lib          proc         sys          usr
/ # 
/ # 
/ # ls
bin          etc          home         lib64        proc         sys          usr
dev          examplefile  lib          passwd       root         tmp          var
/ # exit
[root@docker ~]# 

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部