Docker的安装
yum remove docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-engine
yum -y install gcc
yum -y install g++
yum install -y yum-utils
yum-config-manager --add-repo
https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast
6.安装Docker
yum -y install docker-ce docker-ce-cli containerd.io
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 24.0.7
API version: 1.43
Go version: go1.20.10
Git commit: afdd53b
Built: Thu Oct 26 09:09:18 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 24.0.7
API version: 1.43 (minimum version 1.12)
Go version: go1.20.10
Git commit: 311b9ff
Built: Thu Oct 26 09:08:20 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.24
GitCommit: 61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
runc:
Version: 1.1.9
GitCommit: v1.1.9-0-gccaecfc
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Docker安装完成
Docker的使用
常用命令
启动Docker
[root@localhost ~]# systemctl start docker
查看Docker的运行状态
[root@localhost ~]# systemctl status docker
重启Docker
[root@localhost ~]# systemctl restart docker
停止Docker服务
[root@localhost ~]# systemctl stop docker
查看命令相关信息
[root@localhost ~]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Aliases:
docker container cp, docker cp
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
-q, --quiet Suppress progress output during copy. Progress output is automatically suppressed if
no terminal is attached
镜像命令
列出本地主机上的镜像
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 6 months ago 13.3kB
redis 6.2.7 4b1123a829a1 11 months ago 113MB
apache/rocketmq 4.9.3 9a54e5b5b6c1 20 months ago 535MB
alpine latest c059bfaa849c 2 years ago 5.58MB
apacherocketmq/rocketmq-dashboard latest eae6c5db5d11 2 years ago 738MB
portainer/portainer latest 580c0e4e98b0 2 years ago 79.1MB
参数 | 含义 |
REPOSITORY | 表示镜像的仓库源 |
TAG |
本的镜像。
|
IMAGE ID | 镜像ID |
CREATED | 镜像创建时间 |
SIZE | 镜像大小 |
-a :
列出本地所有的镜像(含历史映像层)。
列出本地所有的镜像(含历史映像层)。
-q :
只显示镜像
ID
。
只显示镜像
ID
。
搜索某个镜像的名称
docker search XXX
[root@localhost ~]# docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 12498 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 61
redislabs/redisinsight RedisInsight - The GUI for Redis 94
redis/redis-stack-server redis-stack-server installs a Redis server w… 60
redis/redis-stack redis-stack installs a Redis server with add… 82
redislabs/rebloom A probablistic datatypes module for Redis 25 [OK]
redislabs/redis Clustered in-memory database engine compatib… 40
只列出N个镜像,默认为25个
[root@localhost ~]# docker search --limit 5 redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 12498 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 61
redislabs/redisinsight RedisInsight - The GUI for Redis 94
redis/redis-stack-server redis-stack-server installs a Redis server w… 60
redis/redis-stack redis-stack installs a Redis server with add… 82
拉取指定的镜像到本地
docker pull XXX 没带标签就拉取最新版本的镜像
docker pull XXX:6.1.10 拉取指定标签的镜像
[root@localhost ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
查看镜像/容器/数据卷所占的空间
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 9 8 3.063GB 112.7MB (3%)
Containers 15 5 10.4MB 1.993MB (19%)
Local Volumes 4 4 218.8kB 0B (0%)
Build Cache 0 0 0B 0B
删除某个镜像
docker rmi XXX
docker rmi 某个镜像的IMAGE ID
[root@localhost ~]# docker rmi redis
Untagged: redis:latest
Untagged: redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Deleted: sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631
Deleted: sha256:49c70179bc923a7d48583d58e2b6c21bde1787edf42ed1f8de9e9b96e2e88e65
Deleted: sha256:396e06df5d1120368a7a8a4fd1e5467cdc2dd4083660890df078c654596ddc1c
Deleted: sha256:434d118df2e9edb51238f6ba46e9efdfa21be68e88f54787531aa39a720a0740
Deleted: sha256:2047f09c412ff06f4e2ee8a25d105055e714d99000711e27a55072e640796294
Deleted: sha256:13d71c9ccb39b206211dd1900d06aa1984b0f5ab8abaa628c70b3eb733303a65
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
docker rmi -f XXX
docker rmi -f 镜像名1:TAG 镜像名2:TAG 可以删除多个镜像
容器命令
启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
OPTIONS | 含义 |
—name=”容器新名字” | 为容器指定一个名称; |
-d | 后台运行容器并返回容器ID,也即启动守护式容器(后台运行); |
-i | 以交互模式运行容器,通常与 -t 同时使用; -t: |
-t | |
-P | 随机端口映射,大写P |
-p | 指定端口映射,小写p |
列出正在运行的容器
docker ps
root@168fd40e5a27:/# [root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
168fd40e5a27 ubuntu "/bin/bash" About a minute ago Up About a minute jovial_einstein
e2592a496c60 portainer/portainer "/portainer" 7 weeks ago Up 34 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp portainer
2984b012f3be apacherocketmq/rocketmq-dashboard:latest "sh -c 'java $JAVA_O…" 7 weeks ago Up 34 minutes 0.0.0.0:8280->8080/tcp, :::8280->8080/tcp rocketmq-console
799a4159b6cf apache/rocketmq:4.9.3 "sh mqbroker -c /hom…" 7 weeks ago Restarting (255) 41 seconds ago rocketmq-broker
e7cc34338422 redis:6.2.7 "docker-entrypoint.s…" 2 months ago Up 34 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis6
12b28b13b6cd mysql:8.0.20 "docker-entrypoint.s…" 2 months ago Up 34 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
可以携带的参数:
退出容器
- exit 容器停止
- ctrl + p + q 容器不停止
启动已停止运行的容器
[root@localhost ~]# docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95cece2337ab ubuntu "/bin/bash" 24 seconds ago Up 23 seconds frosty_fermi
27f56882f7fe ubuntu "/bin/bash" 43 seconds ago Up 42 seconds charming_lamport
[root@localhost ~]# docker start 95cece2337ab
95cece2337ab
重启容器
[root@localhost ~]# docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95cece2337ab ubuntu "/bin/bash" About a minute ago Up About a minute frosty_fermi
27f56882f7fe ubuntu "/bin/bash" About a minute ago Up About a minute charming_lamport
cef340972b6b ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago eloquent_buck
[root@localhost ~]# docker restart cef340972b6b
cef340972b6b
停止容器
docker stop 容器ID或者容器名
[root@localhost ~]# docker stop cef340972b6b
cef340972b6b
强制停止容器
docker kill 容器ID或者容器名称
[root@localhost ~]# docker kill 95cece2337ab
95cece2337ab
删除已停止的容器
docker rm 容器ID
[root@localhost ~]# docker rm 95cece2337ab
95cece2337ab
删除正在运行的容器需要加上-f
docker rm -f XXX
启动守护式容器与进入容器
如果是前台运行
0365c86064ff2f9fdf46a6d90c12685bb43f1b56f5c5bc6560abfda3e1dbc3ac
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0365c86064ff redis "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 6379/tcp
查看容器日志
docker logs 容器ID
[root@localhost ~]# docker logs 0365c86064ff
1:C 25 Nov 2023 07:30:25.693 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 25 Nov 2023 07:30:25.693 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 25 Nov 2023 07:30:25.693 # 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 25 Nov 2023 07:30:25.694 * monotonic clock: POSIX clock_gettime
1:M 25 Nov 2023 07:30:25.698 * Running mode=standalone, port=6379.
1:M 25 Nov 2023 07:30:25.698 # Server initialized
1:M 25 Nov 2023 07:30:25.698 # 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 25 Nov 2023 07:30:25.698 * Ready to accept connections
查看容器内运行的进程
进入容器内部
docker exec -it 容器ID /bin/bash
[root@localhost ~]# docker exec -it 0365c86064ff /bin/bash
root@0365c86064ff:/data#
使用exit退出后容器依然在运行
也可以通过docker attach 容器ID的方式进入容器,但是exit退出会导致容器停止
从容器内拷贝文件到主机上
原文地址:https://blog.csdn.net/m0_72832574/article/details/134614057
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_6043.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。