2

Docker容器管理命令(一)

 2 years ago
source link: https://blog.51cto.com/215687833/5166011
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Docker容器管理命令(一)

原创

青衫解衣 2022-03-31 10:42:02 博主文章分类:Docker/K8S ©著作权

文章标签 docker Docker容器管理命令 文章分类 Linux 系统/运维 阅读数203

​查看当前主机本地docker镜像:​

启动容器必须依赖镜像,所以要获取到镜像的唯一标识

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 62f1d3402b78 8 days ago 104MB
nginx 1.14 295c7be07902 19 months ago 109MB

​docker容器管理命令​

[root@localhost ~]# docker container

Usage: docker container COMMAND

Manage containers

Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.

​查看当前已经在运行的容器

​注意:stop和exit已退出的看不到​

[root@localhost <sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d512a1f155b nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp boring_archimedes
[root@localhost </sub>]#

​查看所有容器-a参数​

注意:退出和执行失败的都可以看到

[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d512a1f155b nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp boring_archimedes

​查看docker容器的的详细信息​

[root@localhost ~]# docker inspect 5d512a1f155b
[
{
"Id": "5d512a1f155beed492c36d9b83fe611e7785029a923174edb9309e8d556db524",
"Created": "2020-11-05T08:14:08.955848311Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"bash"
],

​交互式启动容器或者启动工具类容器 -it参数​

[root@localhost ~]# docker run -it --name "redis" 62f1d3402b78
1:C 06 Nov 2020 03:06:15.677 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 Nov 2020 03:06:15.677 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 Nov 2020 03:06:15.677 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.9 (00000000/0) 64 bit
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379

​删除docker容器:​

删除正在运行的docker 容器需要强制删除参数-f。

[root@harbor <sub>]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 3 months ago 141MB
redis latest 7614ae9453d1 3 months ago 113MB
centos 7 eeb6ee3f44bd 6 months ago 204MB
[root@harbor </sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a897ce1045a2 nginx:latest "/docker-entrypoint.…" 2 weeks ago Up 9 minutes 80/tcp nginx
[root@harbor <sub>]# docker rm nginx
Error response from daemon: You cannot remove a running container a897ce1045a2085de0f8fea36315287571c4bb824b5f624ff0d8fa3db247e891. Stop the container before attempting removal or force remove
[root@harbor </sub>]# docker rm -f nginx
nginx
[root@harbor ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

​--rm 参数​

注意:启动交互式容器或者工具类容器,退出后会自动清理废弃容器

[root@harbor ~]# docker run -it --name redis --rm redis
1:C 30 Mar 2022 02:23:08.761 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 30 Mar 2022 02:23:08.761 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 30 Mar 2022 02:23:08.761 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 30 Mar 2022 02:23:08.762 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1

​docker容器端口映射​

-p参数端口映射,将docker的80端口映射到宿主机的0.0.0.0:8080
[root@localhost <sub>]# docker run -d -p8080:80 --name "redis" --rm 62f1d3402b78
57c8d95ec1a54176f5e79c620f6ee907f8ecc5a7de41464ee6b9aad93df4b9ae
[root@localhost </sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57c8d95ec1a5 62f1d3402b78 "docker-entrypoint.s…" 2 seconds ago Up 1 second 6379/tcp, 0.0.0.0:8080->80/tcp redis

​启动docker容器时加入-i 参数直接和docker容器交互,进入docker容器:​

[root@localhost ~]# docker start -i redis
1:C 06 Nov 2020 06:05:45.593 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 Nov 2020 06:05:45.593 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 Nov 2020 06:05:45.593 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 06 Nov 2020 06:05:45.594 * Running mode=standalone, port=6379.
1:M 06 Nov 2020 06:05:45.594 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 06 Nov 2020 06:05:45.594 # Server initialized
1:M 06 Nov 2020 06:05:45.594 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 06 Nov 2020 06:05:45.594 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 06 Nov 2020 06:05:45.594 * Loading RDB produced by version 6.0.9
1:M 06 Nov 2020 06:05:45.594 * RDB age 8 seconds

​pause暂停一个容器​

[root@node1 ~]# docker pause redis-test2
redis-test2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32eeffcb8cf6 redis:latest "docker-entrypoint.s…" About a minute ago Up About a minute (Paused) 6379/tcp redis-test2

​unpause容器取消暂停​

[root@node1 ~]# docker unpause redis-test2
redis-test2

​查看容器的端口映射​

[root@node1 ~]# docker port nginx
8080/tcp -> 0.0.0.0:80
8443/tcp -> 0.0.0.0:443

​查看redis3容器的消耗资源状态,动态显示​

[root@harbor harbor]# docker stats redis3

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
016b84d11ea4 redis3 0.01% 6.848MiB / 3.701GiB 0.18% 656B / 0B 0B / 0B 5

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
016b84d11ea4 redis3 0.01% 6.848MiB / 3.701GiB 0.18% 656B / 0B 0B / 0B 5

​查看所有的容器消耗资源状态(不指定容器):​

[root@harbor harbor]# docker stats

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
016b84d11ea4 redis3 0.04% 6.848MiB / 3.701GiB 0.18% 656B / 0B 0B / 0B 5
5adc24955f08 harbor-jobservice 0.55% 24.91MiB / 3.701GiB 0.66% 2.01MB / 25.9MB 0B / 0B 9
d190911d7ab7 nginx 0.28% 4.031MiB / 3.701GiB 0.11% 902kB / 922kB 0B / 0B 3
69aeb2e74000 harbor-core 0.65% 48.99MiB / 3.701GiB 1.29% 669kB / 586kB 0B / 0B 9
7ec42fb32059 harbor-portal 0.27% 2.598MiB / 3.701GiB 0.07% 173kB / 1MB 4.96MB / 0B 3
78a35fd6fb07 registryctl 0.28% 15.82MiB / 3.701GiB 0.42% 104kB / 79.5kB 0B / 0B 7
74e09fb58a02 harbor-db 0.36% 49.97MiB / 3.701GiB 1.32% 121kB / 106kB 836kB / 217kB 9
904613fe03b2 redis 0.55% 7.734MiB / 3.701GiB 0.20% 25.8MB / 1.95MB 0B / 144kB 5
d00315016a39 registry 0.28% 13.68MiB / 3.701GiB 0.36% 46.7kB / 33.4kB 0B / 0B 6
3627b54c505b harbor-log 0.28% 4.066MiB / 3.701GiB 0.11% 257kB / 76kB 991kB / 23.6kB 11

​容器内部执行命令:​

[root@harbor harbor]# docker exec redis cat /etc/issue
Welcome to Photon 4.0 (\m) - Kernel \r (\l)
[root@harbor harbor]# docker exec redis echo "hello world"
hello world

​容器自启动:​

官网:https://docs.docker.com/engine/reference/commandline/run/
--restart=always 随着docker服务启动容器

  • no,默认策略,在容器退出时不重启容器
  • on-failure,在容器非正常退出时(退出状态非0),才会重启容器
  • on-failure:3,在容器非正常退出时重启容器,最多重启3次
  • always,在容器退出时总是重启容器
  • unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器

创建容器时不加--restart=always参数,重启docker服务,发现redis3容器没有启动。

[root@harbor harbor]# docker ps | grep redis3
016b84d11ea4 redis:latest "docker-entrypoint.s…" 30 minutes ago Up 7 seconds 6379/tcp redis3
[root@harbor harbor]# systemctl restart docker
[root@harbor harbor]# docker ps | grep redis3

创建容器时,加--restart=always参数

[root@harbor harbor]# docker run -d --name redis5 --restart=always redis:latest
26aa47120c470458fe01a9ab0870a719ea4005ceb3e6ca6c7350ea81d91164ac
[root@harbor harbor]# docker ps | grep redis5
26aa47120c47 redis:latest "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 6379/tcp redis5
[root@harbor harbor]# systemctl restart docker
[root@harbor harbor]# docker ps | grep redis5
26aa47120c47 redis:latest "docker-entrypoint.s…" 46 seconds ago Up 21 seconds 6379/tcp redis5

​容器添加解析​

启动容器添加参数:--add-host hostname:IP

[root@harbor harbor]# docker run -d --name redis6 --add-host www.example.com:192.168.1.110 --restart=always redis:latest
3388c5b71875531a8b6ab9851a4029fe2d30210833abe88b1da39d7c0aa31bc1
[root@harbor harbor]# docker exec redis6 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.110 www.example.com
172.17.0.3 3388c5b71875

Docker容器管理命令(一)_docker


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK