文章目录
安装 Docker
我们首先安装 yum-utils 软件包(提供 yum-config-manager 实用程序)并设置稳定的存储库。
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新 yum 软件包索引
yum makecache fast
安装最新版本的 Docker Engine 和容器
yum install docker-ce docker-ce-cli containerd.io
启动 docker
systemctl start docker
然后我们使用 docker version 查看是否安装成功
docker version
通过运行 hello-world 映像来验证是否正确安装了 Docker Engine
docker run hello-world
当执行 docker run 时,docker 会在本机上寻找镜像,如果本机上有这个镜像则直接使用这个镜像运行,如果本机上没有镜像就会去 DockerHub 上下载,如果 DockerHub 没有此镜像则报错,有镜像就下载到本地并运行。
可以看到结果如下:
查看已经下载的镜像
[root@VM-0-4-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
重启服务
[root@VM-0-4-centos ~]# systemctl daemon-reload
[root@VM-0-4-centos ~]# systemctl restart docker
帮助命令
显示 docker 的版本信息
[root@VM-0-4-centos ~]# docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
显示 docker 的系统信息,包括镜像和容器的数量
[root@VM-0-4-centos ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 3
Running: 0
Paused: 0
Stopped: 3
Images: 1
Server Version: 19.03.13
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1127.19.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.795GiB
Name: VM-0-4-centos
ID: B5FM:L7NR:SG4G:CLVW:5VB2:BG3C:3SAB:XQ6S:TDP6:UDDX:KIAQ:NHG2
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://mirror.ccs.tencentyun.com/
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
帮助命令
如果我们想查看 docker 命令的帮助命令,可以执行以下命令
docker 命令 --help
镜像命令
查看所有本地主机上的镜像
[root@VM-0-4-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
含义如下:
- REPOSITORY:镜像的仓库源
- TAG:版本,lastest 表示最新版本
- IMAGE ID:镜像的 id
- CREATED:镜像的创建时间
- SIZE:镜像的大小
如果我们想列出所有镜像的 id,可以加上可选项。
[root@VM-0-4-centos ~]# docker images -aq
bf756fb1ae65
搜索镜像
[root@VM-0-4-centos ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10130 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3727 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 740 [OK]
percona Percona Server is a fork of the MySQL relati… 511 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 84
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 77
centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK]
bitnami/mysql Bitnami MySQL Docker Image 46 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
prom/mysqld-exporter 31 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 31
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 26
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
circleci/mysql MySQL is a widely used, open-source relation… 19
mysql/mysql-router MySQL Router provides transparent routing be… 17
arey/mysql-client Run a MySQL client from a docker container 15 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 10 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
下载镜像
[root@VM-0-4-centos ~]# docker pull tomcat:8
8: Pulling from library/tomcat
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
a59da3a7d0e7: Pull complete
5ad32ac1e527: Pull complete
27c13473e3bd: Pull complete
717370f5d395: Pull complete
3440cc50e284: Pull complete
d82d6bb87a27: Pull complete
Digest: sha256:671a61a35fe17b889f144c0b57cf1e4773048972e4d00303757c051074ba8c77
Status: Downloaded newer image for tomcat:8
docker.io/library/tomcat:8
下载镜像需要执行以下命令,如果不写 tag,默认就是 latest
docker pull 镜像名[:tag]
删除镜像
我们可以删除指定 id 的镜像
[root@VM-0-4-centos ~]# docker rmi -f 0eed806285b5
Untagged: tomcat:8
Untagged: tomcat@sha256:671a61a35fe17b889f144c0b57cf1e4773048972e4d00303757c051074ba8c77
Deleted: sha256:0eed806285b5a981e313368b82b9cf39a426638710d5973da1ac3b104c78c63a
Deleted: sha256:dfc07aa9149f5df7f97dc836761adb134bc7f2713b4bb8f985f717b16104eac4
Deleted: sha256:0eb558ae39acff2e0b511f29315ef6d596da1efa1fda644c01bd6328e15c0f4c
Deleted: sha256:24fdd84b4adf4b922767e2b9d336c96fb31cab90c84fb34f9164f6587473e565
Deleted: sha256:984e8d35fe94a287a0bc4a45e8757c75e17e80c3f4fed0b9f9b0ec6b4b457ebb
Deleted: sha256:254cf71da09bbc16e304f925dee4270ac9069649dee8576423a6a77186ee0061
Deleted: sha256:3383e45b22b2b665b1a2604e375c1e7dc007c540e77d6367b1d7ba79ef0030d8
Deleted: sha256:d5d618196ec30fcdba1d210f135b4911b9dbdc79fdd729a9a9c3683a6a9b9308
Deleted: sha256:4bb57adf9037e4da45f2dbc0f84a7e9d8da6847bef3e63d09c8a9d8465f4d2c6
Deleted: sha256:5173011923d00f0fe606ecceda93ea88f17dc77e0b026df802c9aeb31bc1eac6
Deleted: sha256:9780f6d83e45878749497a6297ed9906c19ee0cc48cc88dc63827564bb8768fd
容器命令
新建容器并启动
docker run [可选参数] image | docker container run [可选参数] image
部分参数说明:
- –name=“Name” 可以使用容器名字 tomcat01 tomcat02 用来区分容器
- -d 后台方式运行
- -it 使用交互方式运行,进入容器查看内容
- -p 指定容器的端口 例子:-p ip:主机端口:容器端口 / -p 主机端口:容器端口(常用) / -p 容器端口
运行 tomcat
[root@VM-0-4-centos ~]# docker run -d tomcat
列出所有运行的容器
[root@VM-0-4-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1145c4f9eedf tomcat "catalina.sh run" About a minute ago Up About a minute 8080/tcp magical_lamarr
3231cf0eca17 tomcat "catalina.sh run" 4 minutes ago Exited (129) 3 minutes ago angry_wiles
9fff8610c213 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago kind_agnesi
5bf705ea0f4d hello-world "/hello" 2 hours ago Exited (0) 2 hours ago nostalgic_noyce
fbc898265f57 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago inspiring_mcclintock
使用 -a 可选项在列出当前正在运行的容器的同时,也会列出历史运行过的容器
退出容器
如果我们想容器直接退出,可以执行以下这条命令:
exit
如果我们想容器不停止退出,执行 ctrl +P +Q 即可。
删除容器
注意,该指令用于删除指定的容器,但是不能删除正在运行的容器,如果要强制删除需要使用 rm -f
docker rm 容器id
例如,我们想删除一个正在运行的容器
[root@VM-0-4-centos ~]# docker rm -f da5a6001b536
da5a6001b536
如果我们需要删除所有容器
[root@VM-0-4-centos ~]# docker rm -f $(docker ps -aq)
2d29c6716090
abc649248186
1145c4f9eedf
3231cf0eca17
9fff8610c213
5bf705ea0f4d
fbc898265f57
启动和停止容器的操作
停止当前正在运行的容器
[root@VM-0-4-centos ~]# docker stop b1733f2a5764
b1733f2a5764
启动容器
[root@VM-0-4-centos ~]# docker start b1733f2a5764
b1733f2a5764
重启容器
[root@VM-0-4-centos ~]# docker restart b1733f2a5764
b1733f2a5764
强制停止当前容器
[root@VM-0-4-centos ~]# docker kill b1733f2a5764
b1733f2a5764
一些其他命令
查看日志
显示全部日志信息
docker logs -ft 容器id
查看n行日志
docker logs -t --tail n 容器id
查看容器中进程信息
[root@VM-0-4-centos ~]# docker top ea08b79321f3
UID PID PPID C STIME TTY TIME CMD
root 9041 9025 8 15:01 ? 00:00:01 /usr/local/openjdk-11/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
进入当前正在运行的容器
通常容器都是使用后台方式运行的,有时我们需要进入容器,修改一些配置,那么我们可以使用下面两种方法的其中一种。
[root@VM-0-4-centos ~]# docker exec -it 5f0c82a3927b /bin/bash
[root@VM-0-4-centos ~]# docker attach 5f0c82a3927b
这两种方法的区别在于,docker exec 会在进入当前容器后开启一个新的终端,而 docker attach 会进入容器正在执行的终端。
从容器内拷贝到主机上
docker cp 容器id:容器内路径 主机目的路径
查看 docker 容器使用内存情况
docker stats
commit 镜像
我们可以提交容器让其成为一个新的镜像。
docker commit -m="描述信息" -a="作者" 容器id 目标镜像名:[版本TAG]
例子如下:
[root@VM-0-4-centos ~]# docker commit -a="tony" -m="test" 5f0c82a3927b mytomcat:1.0