前言
学习Docker,入门知识。
一、Docker安装
这里我们在centos7上进行安装为例。
官方的安装地址和方法(我们不使用docker的地址):
$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
这里我们配置阿里云的地址:
# 安装必要的一些系统工具
# yum-utils软件包 提供yum-config-manager命令 可配置稳定存储库
yum install -y yum-utils
# 添加软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io
# 安装必要的一些系统工具
# yum-utils软件包 提供yum-config-manager命令 可配置稳定存储库
yum install -y yum-utils
# 添加软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io
安装完成后我们输入指令: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
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
接下来我们运行第一个hello-world程序:输入指令:docker pull hello-world
docker pull 从远程仓库拉取镜像
[root@chedxstudy test]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@chedxstudy test]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
查看我们拉下来的镜像,输入指令:docker images
可以看到我们刚才拉下来的镜像就在这里。
[root@chedxstudy test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
[root@chedxstudy test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
运行hello-world镜像,输入指令docker run hello-world
[root@chedxstudy test]# docker run hello-world
Hello from Docker! #看到这句话说明我们运行成功了。
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@chedxstudy test]# docker run hello-world
Hello from Docker! #看到这句话说明我们运行成功了。
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
恭喜你,你的第一个docker镜像运行成功了。嘻嘻。?
Tips:这里我们在配置下阿里的镜像加速服务。官网链接:https://cr.console.aliyun.com/cn-beijing/instances/mirrors 我们选择镜像加速器,里边有详细的指令。
我们按照指令执行即可:享受非一般的速度哈哈。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <{
"registry-mirrors": ["https://你自己的镜像加速地址.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <{
"registry-mirrors": ["https://你自己的镜像加速地址.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
二、Docker基本指令
1、镜像命令
docker pull [OPTIONS] NAME[:TAG|@DIGEST] #拉取镜像可以指定镜像的一些参数
##我们以拉取tomcat为例
docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
a59da3a7d0e7: Pull complete
9c0f1dffe039: Pull complete
576e3c6f47f8: Pull complete
c7e1b6c3ef84: Pull complete
d1b8a428acdc: Pull complete
7251ae448a6d: Pull complete
Digest: sha256:5b17d5de9c75c9da638c28186c19423b610e7eab3b6f6b975bf47383d12ed0a9
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest ##拉取成功
docker pull [OPTIONS] NAME[:TAG|@DIGEST] #拉取镜像可以指定镜像的一些参数
##我们以拉取tomcat为例
docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
a59da3a7d0e7: Pull complete
9c0f1dffe039: Pull complete
576e3c6f47f8: Pull complete
c7e1b6c3ef84: Pull complete
d1b8a428acdc: Pull complete
7251ae448a6d: Pull complete
Digest: sha256:5b17d5de9c75c9da638c28186c19423b610e7eab3b6f6b975bf47383d12ed0a9
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest ##拉取成功
[root@chedxstudy test]# docker run -d -p 6379:8080 --name mytomact tomcat
a9e647d66f60abfd481993c6c58dc7642e46be7483f4dddf940d44562a22330a
-d 后台运行
-p 小p是指定端口映射,我们机器的6379映射到容器内的8080
--name 给我们启动的容器取个名字
[root@chedxstudy test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9e647d66f60 tomcat "catalina.sh run" 4 seconds ago Up 4 seconds 0.0.0.0:6379->8080/tcp mytomact
[root@chedxstudy test]# docker run -d -p 6379:8080 --name mytomact tomcat
a9e647d66f60abfd481993c6c58dc7642e46be7483f4dddf940d44562a22330a
-d 后台运行
-p 小p是指定端口映射,我们机器的6379映射到容器内的8080
--name 给我们启动的容器取个名字
[root@chedxstudy test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9e647d66f60 tomcat "catalina.sh run" 4 seconds ago Up 4 seconds 0.0.0.0:6379->8080/tcp mytomact
docker ps 查看正在运行的容器
docker ps 查看正在运行的容器
docker exec -it a9e647d66f60 /bin/bash
root@a9e647d66f60:/usr/local/tomcat#
#进入运行的容器
root@a9e647d66f60:/usr/local/tomcat# exit
#exit 从容器中退出
docker exec -it a9e647d66f60 /bin/bash
root@a9e647d66f60:/usr/local/tomcat#
#进入运行的容器
root@a9e647d66f60:/usr/local/tomcat# exit
#exit 从容器中退出
docker images #列出下载的镜像
docker images #列出下载的镜像
docker rm 容器id # 删除容器
docker rmi 镜像名称 # 删除镜像
docker rmi $(docker images) #删除所有镜像
docker rm 容器id # 删除容器
docker rmi 镜像名称 # 删除镜像
docker rmi $(docker images) #删除所有镜像
2、容器命令
docker start 容器id #启动停止的容器
docker stop 容器id #停止容器
docker ps #查看正在运行的容器
docker ps -a #查看所有容器
docker logs 容器id #查看容器日志
docker start 容器id #启动停止的容器
docker stop 容器id #停止容器
docker ps #查看正在运行的容器
docker ps -a #查看所有容器
docker logs 容器id #查看容器日志
docker attach 和 docker exec
使用docker exec和docker attach命令时,应注意:docker attach命令进入交互式环境时,
使用exit命令退出之后,容器即可终止,而docker exec命令则不会!所以这两种命令,
更加推荐使用docker exec!
docker attach 和 docker exec
使用docker exec和docker attach命令时,应注意:docker attach命令进入交互式环境时,
使用exit命令退出之后,容器即可终止,而docker exec命令则不会!所以这两种命令,
更加推荐使用docker exec!
三、Docker容器卷的使用
「匿名挂载」
docker run -d -it --name centos01 -v /test centos
docker run -d -it --name centos01 -v /test centos
查看挂载路径:docker inspect id
"Mounts": [
{
"Type": "volume",
"Name": "34e6e7c8269e37ccb2..0f6638",
"Source": "/var/lib/docker/volumes/34e...54880d/_data",#主机路径
"Destination": "/test", #容器路径
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
"Mounts": [
{
"Type": "volume",
"Name": "34e6e7c8269e37ccb2..0f6638",
"Source": "/var/lib/docker/volumes/34e...54880d/_data",#主机路径
"Destination": "/test", #容器路径
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
「具体路径挂载」
docker run -d -it --name centos02 -v /test:/test centos
docker run -d -it --name centos02 -v /test:/test centos
查看容器信息:docker inspect id
"Mounts": [
{
"Type": "bind",
"Source": "/test",
"Destination": "/test",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
"Mounts": [
{
"Type": "bind",
"Source": "/test",
"Destination": "/test",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
「具名挂载」
docker run -d -it --name centos03 -v juming-centos:/test centos
docker run -d -it --name centos03 -v juming-centos:/test centos
查看下容器信息
"Mounts": [
{
"Type": "volume",
"Name": "juming-centos",
"Source": "/var/lib/docker/volumes/juming-centos/_data",
"Destination": "/test",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
],
"Mounts": [
{
"Type": "volume",
"Name": "juming-centos",
"Source": "/var/lib/docker/volumes/juming-centos/_data",
"Destination": "/test",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
],
可以看出所有在不指定主机路径的情况下,都是在/var/lib/docker/volumes/xxxx/_data
小结一下挂载方式
-v 容器 内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载
-v 容器 内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载
挂载有什么用呢?我们以具体路径挂在为例看一下。首先我们进入容器内部,创建一个hello.java文件
[root@chedxstudy /]# docker exec -it centos02 /bin/bash
[root@a7b090fd8b39 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test tmp usr var
[root@a7b090fd8b39 /]# cd test
[root@a7b090fd8b39 test]# touch hell.java
[root@chedxstudy /]# docker exec -it centos02 /bin/bash
[root@a7b090fd8b39 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test tmp usr var
[root@a7b090fd8b39 /]# cd test
[root@a7b090fd8b39 test]# touch hell.java
然后我们查看我们的主机路径/test
下。已经看到相同的文件已经在主机里创建了。
[root@chedxstudy test]# ls
hell.java
[root@chedxstudy test]# pwd
/test
[root@chedxstudy test]#
[root@chedxstudy test]# ls
hell.java
[root@chedxstudy test]# pwd
/test
[root@chedxstudy test]#
接下来我们将centos02的容器彻底删除,看看挂载在主机的文件会变化吗?
docker rm -f centos02
docker rm -f centos02
可以看到/test
下的文件没有丢失。
到这里你应该对挂载有了一定的认识了。
四、Dockerfile
这里我只想简单介绍下Dockerfile。
Dockerfile是用来构建Docker镜像的文件,命令参数脚本。常用的指令:
docker build -t XXXX .
「一般的构建步骤:」
- 编写一个Dockerfile文件
- docker build 构造一个镜像
- docker run 运行镜像
- docker push 发布一个镜像(发布到阿里云镜像仓库或者Dockerhub)
下边这张图,说的很清楚了。
五、小小Dockerfile实战
从Dockerhub上pull下来的centos是阉割版本的,一些工具是不存在,接下来我们要创建我们自己的centos。看一下我们Dockerfile文件
[root@chedxstudy centos]# cat Dockerfile
FROM centos
MAINTAINER chedx<794481112@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end-----"
CMD /bin/bash
[root@chedxstudy centos]# cat Dockerfile
FROM centos
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end-----"
CMD /bin/bash
接下来进行build,这我直接build是因为文件名字叫Dockerfile,如果是其他的名字,那么请加上 docker build -f fileName -t XXXX .
,还有最后的小点 . 不要忘了。
docker build -t mycentos:0.1 .
........
Successfully built 351d07177b78
Successfully tagged mycentos:0.1
docker build -t mycentos:0.1 .
........
Successfully built 351d07177b78
Successfully tagged mycentos:0.1
成功以后我们查看我们的镜像。
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 351d07177b78 54 seconds ago 295MB
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 351d07177b78 54 seconds ago 295MB
运行一下。
[root@chedxstudy centos]# docker run -it --name mycentos02 mycentos:0.1 /bin/bash
[root@6ba1217f33c8 local]# ls
bin etc games include lib lib64 libexec sbin share src
[root@6ba1217f33c8 local]# pwd
/usr/local #可以看到我们的工作目录是/usr/local,WORKDIR
[root@chedxstudy centos]# docker run -it --name mycentos02 mycentos:0.1 /bin/bash
[root@6ba1217f33c8 local]# ls
bin etc games include lib lib64 libexec sbin share src
[root@6ba1217f33c8 local]# pwd
/usr/local #可以看到我们的工作目录是/usr/local,WORKDIR
看看安装的指令是否生效。vim hello.java
vim指令已经生效。
总结
能看到这里,相信你对Docker应该有了一些体会。下一节的实战练习,希望会带你入门Docker的世界。