1. 安装
1、依赖安装
yum install -y yum-utils device-mapper-persistent-data lvm2
2、添加docker软件包源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
3、查看包
yum list docker-ce --showduplicates | sort -r
4、安装
yum install docker-ce
5.查看
[root@localhost certs.d]# docker version
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:46:54 2020
OS/Arch: linux/amd64
6、启动
systemctl start docker
systemctl enable docker
systemctl status docker
2.镜像
2.1 操作
1. 搜索镜像
docker search mysql
2. 获取镜像
docker pull mysql
3.已拉取的镜像
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 0d64f46acfd1 3 weeks ago 544MB
4、删除镜像
docker image rm [imagename]
docker image rm -f qqc_centos # 强制删除
# rmi 指定镜像id
5.导出镜像到本地文件
docker save mysql > /home/docker_mysql.tar.gz
6.导入镜像(文件导入到本机镜像库)
docker load < /home/centos.tar.gz
7. 镜像添加标签
docker tag IMAGE_ID REPOSITORY:TAG
2.2 创建镜像
2.2.1 在已有的镜像上更新出新镜像
流程:
在centos这个镜像上开启容器,进入容器创建一个文件夹,提交这个容器中的更改,指定一个新的镜像名qqc_centos
1、到容器中创建一个文件
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker run -t -i centos /bin/bash
[root@ae45b3c74dc0 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@ae45b3c74dc0 /]# mkdir qqc_da
2、查看容器状态,提交容器副本,查看镜像列表
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae45b3c74dc0 centos "/bin/bash" 10 minutes ago Up 10 minutes xenodochi
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker commit -m="创建了一个文件qqc_da" -a"qqc" ae45b3c74dc0 qqc_centos
sha256:ba85485414c6999aa275c33685cb3968f748d2b87535dbd296ffdfc5cdb04554
# 注:-m 提交描述信息; -a 指定镜像作者; ae45b3c74dc0 容器id; qqc_centos 要创建的镜像名
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
qqc_centos latest ba85485414c6 25 seconds ago 215MB
centos latest 0d120b6ccaa8 3 weeks ago 215MB
httpd latest a6ea92c35c43 4 weeks ago 166MB
3、用新的镜像(qqc_centos)开启容器,查看变更
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker run -itd qqc_centos /bin/bash
e6d10494c28d156bb4e9fe63d1a107c14e8bf63d24fc432f23a79e76e2ca4806
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6d10494c28d qqc_centos "/bin/bash" 21 seconds ago Up 20 seconds lucid_haibt
ae45b3c74dc0 centos "/bin/bash" 23 minutes ago Up 23 minutes xenodochi
[root@e6d10494c28d /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc qqc_da root run sbin srv sys tmp usr var
2.2.1 Dockerfile指令来创建一个新的镜像
1、编辑文件
[root@iZuf6fy2kg5mx828krkhcuZ ~]# cat Dockerfile
# This my first centos Cockerfile
# Version 1.0
# 基础镜像
FROM centos
# 维护者信息
MAINTAINER qqc
#将宿主机中的biji.doc文件拷贝到镜像机器的tmp目录下
ADD biji.doc /tmp
# 切换至工作目录
WORKDIR /opt
RUN mkdir new_file.txt
RUN useradd test_name
2、构建镜像
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker build -t centos_v1.0 .
Sending build context to Docker daemon 1.033GB
Step 1/6 : FROM centos
---> 0d120b6ccaa8
Step 2/6 : MAINTAINER qqc
---> Running in 4f6c15dcf3a0
Removing intermediate container 4f6c15dcf3a0
---> d7bc4339995f
Step 3/6 : ADD biji.doc /tmp
---> 889d5c33de6c
Step 4/6 : WORKDIR /opt
Removing intermediate container 4f0f60b5cd5e
---> d2fda63ecef7
Step 5/6 : RUN mkdir new_file.txt
---> Running in ed4e09a43028
Removing intermediate container ed4e09a43028
---> fbb09918c085
Step 6/6 : RUN useradd test_name
---> Running in f4da5ab52bf7
Removing intermediate container f4da5ab52bf7
---> 5ef3d025171f
Successfully built 5ef3d025171f
Successfully tagged centos_v1.0:latest
# 注 -t 指定镜像名; -f指定dockerfile 目录;
. 匹配当前目录下的dockerfile文件
3、查看
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_v1.0 latest 5ef3d025171f 6 minutes ago 215MB
qqc_centos latest ba85485414c6 4 hours ago 215MB
centos latest 0d120b6ccaa8 3 weeks ago 215MB
httpd latest a6ea92c35c43 4 weeks ago 166MB
[root@iZuf6fy2kg5mx828krkhcuZ ~]# docker run -it centos_v1.0
[root@e2fc128de3f2 opt]# ls
new_file.txt
[root@e2fc128de3f2 tmp]# ls
biji.doc ks-script-2n9owwnh ks-script-xm1o5azb
[root@e2fc128de3f2 tmp]# cat /etc/passwd | grep test
test_name:x:1000:1000::/home/test_name:/bin/bash
3. 容器
1、用centos这个镜像做一个容器,在容器里执行echo命令
[root@localhost ~]# docker run centos /bin/echo "Hello"
Hello
2、进入容器终端进行交互
[root@localhost ~]# docker run -i -t centos
[root@f90dc76d94ab /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
注:
# -t: 在新容器内指定一个伪终端或终端。
# -i: 允许你对容器内的标准输入 (STDIN) 进行交互
# exit 退出当前容器
3、宿主机中查看容器状态和日志
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dce502c529e0 centos "/bin/bash" 6 minutes ago Up 6 minutes compassionate_ellis
查看容器中输出日志:
docker logs dce502c529e0
4.停止容器
[root@localhost ~]# docker stop dce502c529e0
dce502c529e0
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5、启动容器
查看所有容器(已停止的容器)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
099885a37bb9 centos "/bin/sh -c ls" About a minute ago Exited (0) About a minute ago confident_nash
afa9fd950912 centos "/bin/sh" 2 minutes ago Exited (0) 2 minutes ago lucid_varahamihira
启动:
docker start <容器 ID>
重启:
docker restart <容器 ID>
后台启动:
docker run -itd centos
6、进入容器
docker attach <容器 ID>
# 终端退出会导致容器停止
docker exec -it <容器 ID> (推荐使用)
# 终端退出会导致容器不会停止
7、删除容器
docker rm -f 1e560fca3906
# 删除所有终止状态的容器
docker container prune
8、宿主机文件拷贝到容器
# 主机中的/qqc_data/Python-3.7.0.tgz 拷贝到容器名为zealous_mclean的/opt目录下
docker cp /qqc_data/Python-3.7.0.tgz zealous_mclean:/opt
# 注:容器中的new_file文件拷贝到主机/root目录中
docker cp zealous_mclean:/opt/new_file.txt /root
9、mysql 启动
[root@localhost ~]# ps -aux|grep 8001
root 55432 0.0 0.0 112668 968 pts/4 S+ 17:20 0:00 grep --color=auto 8001
[root@localhost ~]# docker run -itd -p 8001:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6.49
d6f21e33da20e6e3fbcf6dd9f4700eda3d16338aa1fac9c6070093adf2c3fe4f
[root@localhost ~]# docker exec -it d6f21e33da20e6e3fbcf6dd9f4700eda3d16338aa1fac9c6070093adf2c3fe4f /bin/bash
root@d6f21e33da20:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.49 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>