搭建vmware虚拟机
操作系统:windows10企业版
↓
vmware版本:16.0.0
↓
centos镜像版本:CentOS-7-x86_64-DVD-2009.iso
↓
使用vmware新建虚拟机没有ip地址
1.编辑/etc/sysconfig/network-scripts/ifcfg-ens33文件,将其中的ONBOOT设置为yes
2.用service network restart重启网络
↓
搭建docker环境
设置base源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
设置epel源
yum install epel-release -y
安装工具包
yum install -y yum-utils
↓
设置远程仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
↓
安装 yum install docker-ce
可以用yum list docker-ce --show-dumplcates先查看可用版本
↓
配置文件
vim /etc/docker/daemon.json
{
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["quay.io"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip":"172.152.128.1/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
mkdir -p /data/docker
systemctl enable docker
启动 systemctl start docker
↓
查看是否启动正常docker info
查看版本 docker version
↓
校验 docker run hello-world,出现如下信息就说明docker安装成功!
[root@localhost docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for hello-world:latest
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.
docker基本操作
一、镜像
1.docker login docker.io
#用户名密码需要到dockerhub官网上注册
2.docker search alpine
3.docker pull alpine
4.docker images
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
5.docker tag
[root@localhost docker]# docker tag 7731472c3f2a docker.io/library/alpine:v3.10.3
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
alpine v3.10.3 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
6.docker push
[root@localhost docker]# docker tag 7731472c3f2a docker.io/shenzy2021/alpine:v.3.10.3
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
alpine v3.10.3 7731472c3f2a 5 weeks ago 5.61MB
shenzy2021/alpine v.3.10.3 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
[root@localhost docker]# docker push docker.io/shenzy2021/alpine:v.3.10.3
The push refers to repository [docker.io/shenzy2021/alpine]
c04d1437198b: Mounted from library/alpine
v.3.10.3: digest: sha256:d0710affa17fad5f466a70159cc458227bd25d4afb39514ef662ead3e6c99515 size: 528
7.docker rmi
[root@localhost docker]# docker rmi -f 7731472c3f2a
Untagged: alpine:latest
Untagged: alpine:v3.10.3
Untagged: alpine@sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378
Untagged: shenzy2021/alpine:v.3.10.3
Untagged: shenzy2021/alpine@sha256:d0710affa17fad5f466a70159cc458227bd25d4afb39514ef662ead3e6c99515
Deleted: sha256:7731472c3f2a25edbb9c085c78f42ec71259f2b83485aa60648276d408865839
Deleted: sha256:c04d1437198bc178b49fdb9a90a9c0e362ffbd63c4be5d19fec406d1c9d6a03c
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 13 months ago 13.3kB
二、容器
1.docker ps -a
查看本地容器进程
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01be67535b82 hello-world "/hello" 41 minutes ago Exited (0) 41 minutes ago affectionate_maxwell
2.启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG..]
OPTIONS选项:
-i : 启动一个交互式的容器,并持续打开标准输入
-t : 使用终端关联到容器的标准输入输出上
-d : 将容器放置到后台运行
--rm : 退出后即删除容器
--name : 定义容器的唯一名称
[root@localhost docker]# docker run -it alpine:latest /bin/sh
/ # ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:98:80:02 brd ff:ff:ff:ff:ff:ff
inet 172.152.128.2/24 brd 172.152.128.255 scope global eth0
valid_lft forever preferred_lft forever
/ # cat /etc/issue
Welcome to Alpine Linux 3.13
Kernel \r on an \m (\l)
/ # exit
[root@localhost docker]# docker run --rm alpine:latest /bin/echo hello
hello
[root@localhost docker]# docker run -d --name myalpine alpine:latest
e676a230622eceaf3eb719ac266241b2e5e805e82007faad98b68a3a7a74647b
[root@localhost docker]# docker run -d --name myalpine1 alpine:latest /bin/sleep 300
b4207ad50f93f6d9cd55ae4614a475c9adc27bca06c35025b4a645013a3e1038
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4207ad50f93 alpine:latest "/bin/sleep 300" 4 seconds ago Up 3 seconds myalpine1
e676a230622e alpine:latest "/bin/sh" About a minute ago Exited (0) About a minute ago myalpine
6d95395bad56 alpine:latest "/bin/sh" 5 minutes ago Exited (0) 3 minutes ago naughty_burnell
01be67535b82 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago affectionate_maxwell
3.查看宿主机进程
[root@localhost docker]# ps aux|grep sleep |grep -v grep
root 16655 0.0 0.0 1568 248 ? Ss 11:20 0:00 /bin/sleep 300
4.进入容器
[root@localhost docker]# docker exec -ti 6e763856a350 /bin/sh
/ # ps -a
PID USER TIME COMMAND
1 root 0:00 /bin/sleep 300
7 root 0:00 /bin/sh
13 root 0:00 ps -a
/ # exit
5.启停容器
[root@localhost docker]# docker stop 6e763856a350
6e763856a350
[root@localhost docker]# docker start 6e763856a350
6e763856a350
[root@localhost docker]# docker restart 6e763856a350
6e763856a350
6.删除容器
6e763856a350
[root@localhost docker]# docker rm b4207ad50f93
b4207ad50f93
[root@localhost docker]# docker rm myalpine
myalpine
[root@localhost docker]# docker rm -f myalpine2
myalpine2
[root@localhost docker]# for i in `docker ps -a|grep -i exit|sed '1d'|awk '{print $1}'`;do docker rm -f $i;done
01be67535b82
7.提交/修改容器
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
[root@localhost docker]# docker run -d --name myalpine alpine:latest /bin/sleep 300
1e5304c74ebc034778ffb43ea44d0946ccaa04b966beb2e36d3d8c67dc90e3c2
[root@localhost docker]# docker exec -ti myalpine /bin/sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # echo hello>1.txt
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # exit
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e5304c74ebc alpine:latest "/bin/sleep 300" About a minute ago Up About a minute myalpine
[root@localhost docker]# docker commit -p myalpine alpine:3.10.3_with_1.txt
sha256:e459545351027405a3286f417736c990421f550265c3d94eefa131ed34555d04
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.10.3_with_1.txt e45954535102 5 seconds ago 5.61MB
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
8.导出/导入容器
[root@localhost docker]# docker save e45954535102 > alpine:3.10.3_with_1.txt.tar
[root@localhost docker]# ls
alpine:3.10.3_with_1.txt.tar buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
[root@localhost docker]# docker rmi -f alpine:3.10.3_with_1.txt
Untagged: alpine:3.10.3_with_1.txt
Deleted: sha256:e459545351027405a3286f417736c990421f550265c3d94eefa131ed34555d04
Deleted: sha256:880621b71d1d945e002ec832ef04684b09d82385c5f64fad43d457770a2c4ac4
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
[root@localhost docker]# docker load < alpine\:3.10.3_with_1.txt.tar
d2302c958420: Loading layer [==================================================>] 3.584kB/3.584kB
Loaded image ID: sha256:e459545351027405a3286f417736c990421f550265c3d94eefa131ed34555d04
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> e45954535102 5 minutes ago 5.61MB
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
[root@localhost docker]# docker tag e45954535102 myalpine:3.10.3_with_1.txt
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myalpine 3.10.3_with_1.txt e45954535102 6 minutes ago 5.61MB
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
9.查看容器日志
[root@localhost docker]# docker run hello-world 2>&1 >>/dev/null
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aec01b5a34e1 hello-world "/hello" 8 seconds ago Exited (0) 6 seconds ago strange_williams
1e5304c74ebc alpine:latest "/bin/sleep 300" 16 minutes ago Exited (0) 11 minutes ago myalpine
[root@localhost docker]# docker logs aec01b5a34e1
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.
三、docker容器的高级操作
1.映射端口
docker run -p容器外端口:容器内端口
[root@localhost docker]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete
e3eaf3d87fe0: Pull complete
38cb13c1e4c9: Pull complete
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
docker.io/library/nginx:1.12.2
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myalpine 3.10.3_with_1.txt e45954535102 42 hours ago 5.61MB
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
nginx 1.12.2 4037a5562b03 2 years ago 108MB
[root@localhost docker]# docker tag 4037a5562b03 shenzy2021/nginx:v1.12.2
[root@localhost docker]# docker run --rm --name mynginx -d -p81:80 shenzy2021/nginx:v1.12.2
f6cddb0be8869e6be230e962c13afededebed2cfce208017ab8810839f6eb892
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6cddb0be886 shenzy2021/nginx:v1.12.2 "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:81->80/tcp mynginx
aec01b5a34e1 hello-world "/hello" 42 hours ago Exited (0) 42 hours ago strange_williams
1e5304c74ebc alpine:latest "/bin/sleep 300" 42 hours ago Exited (0) 42 hours ago myalpine
[root@localhost docker]# netstat -luntp|grep 81
tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 25059/docker-proxy
[root@localhost docker]# curl 127.0.0.1:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
2.挂在数据卷
docker run -v 容器外目录:容器内目录
[root@localhost html]# wget www.baidu.com
--2021-02-22 15:07:44-- http://www.baidu.com/
正在解析主机 www.baidu.com (www.baidu.com)... 180.101.49.11, 180.101.49.12
正在连接 www.baidu.com (www.baidu.com)|180.101.49.11|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2381 (2.3K) [text/html]
正在保存至: “index.html”
100%[==================================================================================================================================================================================================>] 2,381 --.-K/s 用时 0s
2021-02-22 15:07:44 (104 MB/s) - 已保存 “index.html” [2381/2381])
[root@localhost html]# ll
总用量 4
-rw-r--r--. 1 root root 2381 2月 22 15:07 index.html
[root@localhost ~]# docker run --rm -d --name mynginx_with_baidu -p82:80 -v/root/html:/usr/share/nginx/html shenzy2021/nginx:v1.12.2
51c4eeb798cd791e1560ee1a45d61d6ebfc88803a3356cdd01d2e25b691db302
[root@localhost ~]# docker inspect mynginx_with_baidu
[
{
"Id": "51c4eeb798cd791e1560ee1a45d61d6ebfc88803a3356cdd01d2e25b691db302",
"Created": "2021-02-22T07:13:37.192013643Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
3.传递环境变量
docker run -e 环境变量key=环境变量value
[root@localhost ~]# docker run --rm -e E_OPTS=adf alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=3d81dd19ac2b
E_OPTS=adf
HOME=/root
[root@localhost ~]# docker run --rm -e E_OPTS=adf -e JAVA_HOME=/root alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2dc6a715cb7c
E_OPTS=adf
JAVA_HOME=/root
HOME=/root
4.容器内安装软件(工具)等
yum/apt-get/apt等
root@51c4eeb798cd:/# tee /etc/apt/sources.list << EOF
> deb http://mirrors.163.com/debian/ jessie main non-free contrib
> deb http://mirrors.163.com/debian/ jessie-update main non-free contrib
> EOF
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-update main non-free contrib
root@51c4eeb798cd:/# apt-get update
Ign:1 http://mirrors.163.com/debian jessie InRelease
Ign:2 http://mirrors.163.com/debian jessie-update InRelease
Get:3 http://mirrors.163.com/debian jessie Release [77.3 kB]
...
root@51c4eeb798cd:/# apt-get install curl -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
root@51c4eeb798cd:/# curl -k https://www.baidu.com
<!DOCTYPE html>
...
[root@localhost ~]# docker commit -p 51c4eeb798cd shenzy2021/nginx:curl
sha256:feff1fcfeff914dce944877238944d777c4d963a110122eb8119ba090c4fc6a6
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
shenzy2021/nginx curl feff1fcfeff9 8 seconds ago 136MB
myalpine 3.10.3_with_1.txt e45954535102 2 days ago 5.61MB
alpine latest 7731472c3f2a 5 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
nginx 1.12.2 4037a5562b03 2 years ago 108MB
shenzy2021/nginx v1.12.2 4037a5562b03 2 years ago 108MB
[root@localhost ~]# docker push docker.io/shenzy2021/nginx:curl
The push refers to repository [docker.io/shenzy2021/nginx]
4b152d34b795: Pushed
4258832b2570: Mounted from library/nginx
683a28d1d7fd: Mounted from library/nginx
d626a8ad97a1: Pushed
curl: digest: sha256:b3d1ada2a0586b165728f7612250d8996a5a6b7a10a347c663869a405d01b47b size: 1160
容器的生命周期
1.检查本地是否存在镜像,如果不存在即从远端仓库检索
2.利用镜像启动容器
3.分配一个文件系统,并在只读的镜像层外挂载一层可读写层
4.从宿主机配置的网桥接口钟桥接一个虚拟接口到容器
5.从地址池配置一个ip地址给容器
6.执行用户指定的指令
7.执行完毕后容器终止
Docker镜像制作的途径
docker commit
Dockerfile
About Dockerfile
Dockerfile is nothing but the source code for building Docker image.
1.Docker can build images automatically by reading the instructions from Dockerfile
2.A Dockerfile is a text document that contains all the conmands a user could call on the command line to assemble an image.
3.Using docker build,users can create an automated build that executes several command line instructions in succession.
4组核心的Dockerfile指令
USER/WORKDIR指令
ADD/EXPOSE指令
RUN/ENV指令
CMD/ENTRYPOINT指令
[root@localhost dockerfile]# tee /data/dockerfile/Dockerfile <<EOF
> FROM docker.io/shenzy2021/nginx:v1.12.2
> USER nginx
> WORKDIR /usr/share/nginx/html
> EOF
FROM docker.io/shenzy2021/nginx:v1.12.2
USER nginx
WORKDIR /usr/share/nginx/html
[root@localhost dockerfile]# docker build . -t shenzy2021/nginx:v1.12.2_with_dockerfile
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM docker.io/shenzy2021/nginx:v1.12.2
---> 4037a5562b03
Step 2/3 : USER nginx
---> Running in 2e3216f5ba82
Removing intermediate container 2e3216f5ba82
---> ef7eb46d9905
Step 3/3 : WORKDIR /usr/share/nginx/html
---> Running in 31553d12c541
Removing intermediate container 31553d12c541
---> 927011eab2f9
Successfully built 927011eab2f9
Successfully tagged shenzy2021/nginx:v1.12.2_with_dockerfile
[root@localhost dockerfile]#
实验
[root@localhost dockerfile]# vim Dockerfile
FROM shenzy2021/nginx:v1.12.2
USER root
ENV WWW /usr/share/nginx/html
ENV CONF /etc/nginx/conf.d
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
WORKDIR $WWW
ADD index.html $WWW/index.html
ADD demo.od.com.conf $CONF/demo.od.com.conf
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
[root@localhost dockerfile]# vim demo.od.com.conf
server {
listen 80;
server_name demo.od.com;
root /usr/share/nginx/html;
}
Docker网络模型
1.NAT
2.None
3.Host
4.联合网络