Docker 基本命令
【使用示例】
[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
#查看镜像结构
[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 ~]# ls
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
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
#快速删除所有镜像
【容器常用操作】
/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
##在后台运行
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
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
0bfc7b1f6b47208dee5d674d9b73df6f917bad17999718513c89f61c7c018b92
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
【容器网络】
NETWORK ID NAME DRIVER SCOPE
bcef9685697c bridge bridge local #桥接
0bb7a7894546 host host local #直连
882e4785cc3b none null local #none
/ # 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)
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
/ # 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 # 删除所有停止的容器
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
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
#强制删除
container5
108ba95b88024bd08aff0110ebdb26bc416efb2129184f08ebc9157a55c06dba
Error response from daemon: container 108ba95b88024bd08aff0110ebdb26bc416efb2129184f08ebc9157a55c06dba is not running
container1
[root@docker ~]# docker exec -it container1 bash
root@108ba95b8802:/#
root@108ba95b8802:/#
root@108ba95b8802:/# exit
exit
[root@docker ~]#
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
108ba95b8802 nginx "/docker-entrypoint.…" 22 minutes ago Up About a minute 80/tcp container1
##删除在运行状态的容器 -f
container1
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#
/ # [root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4b39f73d829 busybox "sh" 58 seconds ago Up 57 seconds container3
/ #
/ #
/ #
/ # [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)
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)
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
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
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#
/ # 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
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
删除容器后,容器里面创建的文件也会消失。
/ # 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
IMAGE CREATED CREATED BY SIZE COMMENT
65ad0d468eb1 15 months ago BusyBox 1.36.1 (glibc), Debian 12 4.26MB
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
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
【docker中的文件传输】
/ # 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
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
/ #
/ # 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 ~]#