Common commands for Docker images and containers
Practice environment:
Operating system: CentOS Linux release 7.5.1804 (Core)
Virtual tool: VMware
Application Container engine: Docker version 1.13.1
one。 Mirror image
The container actually adds a read-write layer on top of the image, and any file changes made in the running container will be written to this read-write layer. If the container is deleted, the top read and write will be deleted and the changes will be lost.
I lost it.
1. Configure as a domestic mirror image
Method 1: docker official
# vi / etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com/"]"
}
Method 2: Ali
[root@docker /] # cat / etc/docker/daemon.json
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]"
}
two。 Mirror common command operations
[root@docker /] # systemctl restart docker
# download the specified version image
[root@docker /] # docker pull nginx:1.12
# Show all mirrors
[root@docker /] # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Busybox v2 6e9545b1e2a2 17 minutes ago 1.22 MB
26795fdb6ee8 18 minutes ago 1.22 MB
Docker.io/tomcat latest 4e7840b49fad 6 days ago 529 MB
Docker.io/nginx latest a1523e859360 6 days ago 127 MB
Docker.io/python 3.5 0320ef7199ca 6 days ago 909 MB
Docker.io/mysql latest c8ad2be69a22 6 days ago 465 MB
Docker.io/php latest e66ae809d99a 6 days ago 405 MB
Docker.io/httpd latest c5a012f9cf45 7 days ago 165 MB
Docker.io/mongo latest bcef5fd2979d 11 days ago 386 MB
Docker.io/ubuntu latest 72300a873c2c 11 days ago 64.2 MB
Docker.io/centos latest 470671670cac 6 weeks ago 237 MB
Docker.io/busybox latest 6d5fcfe5ff17 2 months ago 1.22 MB
# Delete all unnamed images
[root@docker overlay2] # docker rmi $(docker images-f "dangling=true"-Q)
# View the image calendar
[root@docker overlay2] # docker image history centos
IMAGE CREATED CREATED BY SIZE COMMENT
470671670cac 6 weeks ago / bin/sh-c # (nop) CMD ["/ bin/bash"] 0 B
6 weeks ago / bin/sh-c # (nop) LABEL org.label-schema.... 0 B
7 weeks ago / bin/sh-c # (nop) ADD file:aa54047c80ba300... 237 MB
# display image details
[root@docker /] # docker image inspect nginx
.
[root@docker /] # docker image pull nginx:1.11
.
# Delete the specified image
[root@docker /] # docker image rm nginx:1.12
Untagged: nginx:1.12
Untagged: docker.io/nginx@sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Deleted: sha256:19f98a1ae65cc06a1f35e86ff4a7894c7f8fab2313a35503166233d470ff3752
Deleted: sha256:d626a8ad97a1f9c1f2c4db3814751ada64f60aed927764a3f994fcd88363b659
# Mark the reference image
[root@docker /] # docker tag nginx:1.11 nginx:v1
[root@docker /] # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Docker.io/nginx 1.11 5766334bdaa0 2 years ago 183 MB
Nginx v1 5766334bdaa0 2 years ago 183 MB
# Save the image file
[root@docker /] # docker image save nginx:1.11 > nginx1.11.tar
[root@docker /] # du-sh nginx1.11.tar
182M nginx1.11.tar
# load image file
[root@docker /] # docker load sky9899.tar
[root@docker /] # du-sh sky9899.tar
1.4M sky9899.tar
# Import Container File
[root@docker /] # docker image import sky9899.tar sky9899:self
Sha256:1302c27299d6326c256baeca85504fabb30b92abd480c234284fc567458fcac7
[root@docker /] # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Sky9899 self 1302c27299d6 4 seconds ago 1.22 MB
3. Common container operation commands
# run the container. If it does not exist, it will be downloaded by default
[root@docker sky9890] # docker container run-itd-name bs busybox
Unable to find image 'busybox:latest' locally
Trying to pull repository docker.io/library/busybox...
Latest: Pulling from docker.io/library/busybox
Bdbbaa22dec6: Pull complete
Digest: sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
Status: Downloaded newer image for docker.io/busybox:latest
3c28790c0a4187063324aafed93caf6d5589d17c9b458001c7b6232205a97907
# View the containers in use
[root@docker sky9890] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c28790c0a41 busybox "sh" 12 seconds ago Up 10 seconds bs
# execute bs container
[root@docker sky9890] # docker container attach bs
/ # ls
Bin dev etc home proc root run sys tmp usr var
/ # ls
Bin dev etc home proc root run sys tmp usr var
/ # ps-ef
PID USER TIME COMMAND
1 root 0:00 sh
8 root 0:00 ps-ef
/ # ifconfig
Eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
Inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
Inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
Collisions:0 txqueuelen:0
RX bytes:1296 (1.2 KiB) TX bytes:648 (648.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.0B) TX bytes:0 (0.0B)
/ # exit # after exiting, the container stops
[root@docker sky9890] # docker container start bs
Bs
# execute container bs
[root@docker sky9890] # docker container attach bs
/ # exit # after exiting, the container is running
[root@docker sky9890] # docker container run-itd-e astat123-- name bs1 busybox
B14e43e4cbc3924af1f8425edc2cce43bf8682e1f7b3436f06bfa02cb5db0071
[root@docker sky9890] # docker exec-it bs1 sh
/ # ls
Bin dev etc home proc root run sys tmp usr var
/ # echo $a
one hundred and twenty three
/ # exit
# specify container port
[root@docker sky9890] # docker container run-itd-p 8080 itd 80-- name nginx01 nginx
2b6992269e6b9716c101d6cd8f11462b9bb05facbd7bae2b0b656baed2ff027d
[root@docker sky9890] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b6992269e6b nginx "nginx-g'daemon..." 43 seconds ago Up 41 seconds 0.0.0.0 daemon 80-> 80/tcp nginx01
B14e43e4cbc3 busybox "sh" 4 minutes ago Up 4 minutes
# obtain nginx01 container log
[root@docker sky9890] # docker logs nginx01
192.168.153.1-[04/Mar/2020:07:25:14 + 0000] "GET / HTTP/1.1" 200612 "-" Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
Like Gecko "-"
07:25:15 on 2020-03-04 [error] 6: * 3 open () "/ usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client:
192.168.153.1, server: localhost, request: "GET / favicon.ico HTTP/1.1", host: "192.168.153.141 purl 8080"
192.168.153.1-[04/Mar/2020:07:25:15 + 0000] "GET / favicon.ico HTTP/1.1" 404 555 "-" Mozilla/4.0 (compatible; MSIE 7.0; Windows NT)
6.0; Trident/4.0) ""-"
[root@docker sky9890] # ls / var/lib/docker/containers/
# limit CPU and memory resources
[root@docker sky9890] # docker container run-itd-cpus 1-memory 512m-name nginx02 nginx
B66722038a14775cb47b9d4f954c27373433cf89a6e0a544630903d90aba1a08
# Container Resource usage Statistics
[root@docker sky9890] # docker container stats nginx02
CONTAINER CPU% MEM USAGE / LIMIT MEM% NET I PIDS O BLOCK I Bank O PIDS
Nginx02 0.005% 1.375 MiB / 512 MiB 0.27% 648B / 648B 0 B / 0 B 2
# list containers
[root@docker sky9890] # docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
B66722038a14 nginx "nginx-g'daemon..." 8 minutes ago Up 8 minutes 80/tcp nginx02
2b6992269e6b nginx "nginx-g'daemon..." 23 minutes ago Up 23 minutes 0.0.0.0 daemon 80-> 80/tcp nginx01
B14e43e4cbc3 busybox "sh" 27 minutes ago Up 27 minutes bs1
# display container details
[root@docker sky9890] # docker container inspect b66722038a14
[root@docker sky9890] # docker exec-it nginx01 bash
Root@2b6992269e6b:/#
[root@docker sky9890] # docker commit bs1 busybox:v2
Sha256:6e9545b1e2a2254243ef2599bce5337aa5f67d57cef1d4aa7aa1622e3f1a4921
[root@docker sky9890] # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Busybox v2 6e9545b1e2a2 8 seconds ago 1.22 MB
Docker.io/mongo latest bcef5fd2979d 11 days ago 386 MB
Docker.io/ubuntu latest 72300a873c2c 11 days ago 64.2 MB
Docker.io/centos latest 470671670cac 6 weeks ago 237 MB
Docker.io/busybox latest 6d5fcfe5ff17 2 months ago 1.22 MB
# copy files to the bs1 container
[root@docker /] # docker container cp sky9899.zip bs1:/root
[root@docker /] # docker container exec bs1 ls / root
Sky9899.zip
# restart the container
[root@docker /] # docker restart bs1
Bs1
[root@docker /] # docker container exec bs1 ls / root
Sky9899.zip
# View the container log
[root@docker /] # docker logs nginx01
[root@docker /] # docker port nginx01
80/tcp-> 0.0.0.0For 8080
[root@docker /] # docker stats nginx02
# start the container
[root@docker /] # docker start b66722038a14
# stop the container
[root@docker /] # docker stop b66722038a14
# Delete a container
[root@docker /] # docker rm b66722038a14