一、前言
目前跟着视频学习,本文是记录一下其中使用的docker 命令。如果错误,感谢指正。
本文仅自我记录使用。
1. 基本概念
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
一、搭建
[root@centos ~]# uname -r ==》查看内核版本
[root@centos ~]# yum list installed | grep docker ==》查看是否安装了docker
[root@centos ~]# yum -y install docker ==》 安装docker
[root@centos ~]# service docker start ==》 启动docker服务
[root@centos ~]# docker run hello-world ==》 拉取 hello word 镜像,测试搭建是否成功
[root@VM-0-17-centos ~]# docker version ==》 查看docker 版本
[root@VM-0-17-centos ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo ==》 切换 阿里云镜像
二、启动 、停止
// 服务启动
[root@VM-0-17-centos ~]# service docker start
Redirecting to /bin/systemctl start docker.service
// 查看服务版本
[root@VM-0-17-centos ~]# docker --version
Docker version 1.13.1, build 64e9980/1.13.1
// 停止服务
[root@VM-0-17-centos ~]# service docker stop
Redirecting to /bin/systemctl stop docker.service
三、镜像操作
// 查看镜像
[root@VM-0-17-centos ~]# docker images
// 搜索镜像
[root@VM-0-17-centos ~]# docker search hello
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/hello-world Hello World! (an example of minimal Docker... 1329 [OK]
// 拉取镜像
[root@VM-0-17-centos ~]# docker pull docker.io/dockercloud/hello-world
// 删除镜像
[root@VM-0-17-centos ~]# docker rmi docker.io/dockercloud/hello-world
四、容器操作
// 创建容器
[root@VM-0-17-centos ~]# docker run -d -p 8888:8080 --name tomcate-test registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
77ad45e42f182737ad0a5b49dea1f30f0d068b6538e57b521008f7383c176948
// 查询所有运行中的容器
[root@VM-0-17-centos ~]# docker ps
// 查询所有容器
[root@VM-0-17-centos ~]# docker ps -a
// 按照 tom 关键字查询
[root@VM-0-17-centos ~]# docker ps -a|grep tom
// 进入容器。-it 后可以跟容器id 或者 容器名称
[root@VM-0-17-centos ~]# docker exec -it 77ad45e42f18 /bin/bash
root@77ad45e42f18:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
// 退出容器
root@77ad45e42f18:/# exit
// 从宿主机拷贝文件 index.html 到 容器名为tomcate-test 的容器的 /usr/tomcat/webapps 目录
[root@VM-0-17-centos ~]# docker cp index.html tomcate-test:/usr/tomcat/webapps
// 下面都可以通过 容器 id 或者 容器名称来操作
// 停止容器
[root@VM-0-17-centos ~]# docker stop 77ad45e42f18
77ad45e42f18
// 启动容器
[root@VM-0-17-centos ~]# docker start 77ad45e42f18
77ad45e42f18
// 删除容器
[root@VM-0-17-centos ~]# docker rm 77ad45e42f18
docker run -d(后端运行) -p (端口映射) 8888(宿主机的端口):8080(容器暴露的端口) --name tyomcat-test(容器名称) tomcat(镜像名称)
五、网络模式
https://www.jianshu.com/p/22a7032bb7bd
Docker网络模式 | 配置 说明 |
---|---|
host模式 | –net=host 容器和宿主机共享Network namespace。 |
container模式 | –net=container:NAME_or_ID 容器和另外一个容器共享Network namespace。 kubernetes中的pod就是多个容器共享一个Network namespace。 |
none模式 | –net=none 容器有独立的Network namespace,但并没有对其进行任何网络设置,如分配veth pair 和网桥连接,配置IP等。 |
bridge模式 | –net=bridge (默认为该模式)。每次启动重新分配一个ip |
六、容器设置固定ip
// 查询 docker0 的虚拟网络
[root@VM-0-17-centos ~]# ifconfig
// 创建三个 容器
[root@VM-0-17-centos ~]# docker run -d --name tomcate1 registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
67fae9c2bb456944240c15748b99b82e00fe121b7b30ba0ebe49b50f86d8e75b
[root@VM-0-17-centos ~]# docker run -d --name tomcate2 registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
60387f274165b10642ab9e312a41ef55ba0d27a74db4e76f87909c359db25e0f
[root@VM-0-17-centos ~]# docker run -d --name tomcate3 registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
7e1ea5cfcb2dfe3fbeb203cd429fc723aa1ecae3d660c91a2289907ba26d956c
// 这里会增加三个虚拟ip
[root@VM-0-17-centos ~]# ifconfig
//根据容器id 查看指定容器的所有信息
[root@VM-0-17-centos ~]# docker inspect 67fae9c2bb456944240c15748b99b82e00fe121b7b30ba0ebe49b50f86d8e75b
// 查看所有容器的 ip
[root@VM-0-17-centos ~]# docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
/tomcate3 - 172.18.0.4
/tomcate2 - 172.18.0.3
/tomcate1 - 172.18.0.2
// 创建一个网段
[root@VM-0-17-centos ~]# docker network create --subnet=172.19.0.0/16 mynet
f43361e4901c399ac32438879638da7f01b00cebd541593062bd43b96264fcdb
// 创建三个基于该网段的 指定ip的容器
[root@VM-0-17-centos ~]# docker run -d -it --net mynet --ip 172.19.0.2 --name tomcate4 registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
6accb363a54edf0ca3a8c502c36af86f2208f74405221250c46b2ebe29f77369
[root@VM-0-17-centos ~]# docker run -d -it --net mynet --ip 172.19.0.3 --name tomcate5 registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
52c0840a5442fa0ff06265fc4dcd6f38b8396e7d09f4c76617529d8062fbb869
[root@VM-0-17-centos ~]# docker run -d -it --net mynet --ip 172.19.0.4 --name tomcate6 registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
7313e9af8dd5522a3c55b4ca9f3c1345bd7715888e8f964dcef8b80c30cf6454
七、镜像制作
利用 Dockerfile 制作镜像。
Dockerfile 内置命令 :
FROM : 依赖的底层镜像
MAINTAINER : 指定镜像创建者
ENV : 设置环境变量
RUN : 运行shell命令
COPY : 将编译机本地文件拷贝到镜像文件系统中
EXPOSE : 指定监听端口
ENTRYPOINT : 预执行命令,创建容器并启动后才执行
创建 DockerFile,内容如下
// 指定基础镜像
FROM registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
// 指定创建人
MAINTAINER KingFish
// 将index.html 拷贝到容器指定目录中
COPY index.html /usr/tomcat/webapps/ROOT/
// 暴露8080端口
EXPOSE 8080/tcp
生成镜像命令
[root@VM-0-17-centos ~]# docker build -t newtomcat .
Sending build context to Docker daemon 34.3 kB
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
---> d07ca8aba782
Step 2/4 : MAINTAINER KingFish
---> Using cache
---> 656617cb361c
Step 3/4 : COPY /root/index.html /usr/tomcat/webapps/ROOT/
lstat root/index.html: no such file or directory
[root@VM-0-17-centos ~]# vim Dockerfile
[root@VM-0-17-centos ~]# docker build -t newtomcat .
Sending build context to Docker daemon 35.84 kB
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27
---> d07ca8aba782
Step 2/4 : MAINTAINER KingFish
---> Using cache
---> 656617cb361c
Step 3/4 : COPY index.html /usr/tomcat/webapps/ROOT/
---> 0ea8c38e385a
Removing intermediate container 47a460a9d1d1
Step 4/4 : EXPOSE 8080/tcp
---> Running in b2a165127694
---> e25e188ca7a1
Removing intermediate container b2a165127694
Successfully built e25e188ca7a1
// 查看镜像,发现newtomcat
[root@VM-0-17-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newtomcat latest e25e188ca7a1 About a minute ago 369 MB
docker.io/hello-world latest bf756fb1ae65 10 months ago 13.3 kB
registry.cn-hangzhou.aliyuncs.com/shuodao/tomcat-8.5.27 latest d07ca8aba782 2 years ago 369 MB
// 启动自定义容器
[root@VM-0-17-centos ~]# docker run -d -p 8888:8080 newtomcat
a57ed4735ea43450f4d308ac48e30c012bd7dfbeb6707058d80e8c26c17c673d
替换成 index.html
八、容器可视化 shipyard的安装使用
[root@VM-0-17-centos ~]# docker pull rethinkdb
Using default tag: latest
Trying to pull repository docker.io/library/rethinkdb ...
latest: Pulling from docker.io/library/rethinkdb
bb79b6b2107f: Pull complete
8be8ff2d863f: Pull complete
f1e76d8854d8: Pull complete
84795ccb1d28: Pull complete
c71976fa1d68: Pull complete
Digest: sha256:7e85ad23b6382fbb83af993a65b37ef60a7e57a38195482ebd6c7e3634f74bbc
Status: Downloaded newer image for docker.io/rethinkdb:latest
[root@VM-0-17-centos ~]# docker pull microbox/etcd
Using default tag: latest
Trying to pull repository docker.io/microbox/etcd ...
latest: Pulling from docker.io/microbox/etcd
8ded6e8ab3fd: Pull complete
bf8f85223d7a: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:941fd46b4eab265c65da9bfbf33397b853a7cef6c16df93a1e3fea7b4e47fc90
Status: Downloaded newer image for docker.io/microbox/etcd:latest
[root@VM-0-17-centos ~]# docker pull shipyard/docker-proxy
Using default tag: latest
Trying to pull repository docker.io/shipyard/docker-proxy ...
latest: Pulling from docker.io/shipyard/docker-proxy
8f4ec95ceaee: Pull complete
ac77a345f217: Pull complete
43039e3ef672: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:da6bbd1a145581a940d44940cce0f43705d7f8ec552a4e97e77104ec1b6dc3d1
Status: Downloaded newer image for docker.io/shipyard/docker-proxy:latest
[root@VM-0-17-centos ~]# docker pull swarm
Using default tag: latest
Trying to pull repository docker.io/library/swarm ...
latest: Pulling from docker.io/library/swarm
38e5683d7755: Pull complete
083aff163606: Pull complete
2064f1a73c6b: Pull complete
Digest: sha256:2de8883e2933840ed7ee7360ea1eed314bf8aeac37c0692b9ca651630fde3b7f
Status: Downloaded newer image for docker.io/swarm:latest
[root@VM-0-17-centos ~]# docker pull shipyard/shipyard
Using default tag: latest
Trying to pull repository docker.io/shipyard/shipyard ...
latest: Pulling from docker.io/shipyard/shipyard
cb5507795515: Pull complete
fd711d385b34: Pull complete
9f2a509de079: Pull complete
a5251eb716bb: Pull complete
Digest: sha256:5f065362680fa4565dd150c8da3edd09b79a7a3010d3ceef20093c2a879187e0
Status: Downloaded newer image for docker.io/shipyard/shipyard:latest
[root@VM-0-17-centos ~]# firewall-cmd --zone=public --add-port=4001/tcp --permanent
[root@VM-0-17-centos ~]# docker run -ti -d --restart=always --name shipyard-rethinkdb rethinkdb
a2b8f72978f08566c02e7ff8b723e051d611900edb05907cbe5eb88e89d5e92e
[root@VM-0-17-centos ~]# docker run -ti -d -p 4001:4001 -p 7001:7001 --restart=always --name shipyard-discovery microbox/etcd:latest -name discovery
19a768bf00d2db6c32054a9b1a09f037d471dbb1c526a467a693257d0c46315e
[root@VM-0-17-centos ~]# docker run -ti -d -p 2375:2375 --hostname=$HOSTNAME --restart=always --name shipyard-proxy -v /var/run/docker.sock:/var/run/docker.sock -e PORT=2375 shipyard/docker-proxy:latest
8356b6a7410a9f68fba485fcb1bb11443f29ba31abc27657291dd05f7f9b6977
[root@VM-0-17-centos ~]# docker run -ti -d --restart=always --name shipyard-swarm-manager swarm:latest manage --host tcp://0.0.0.0:3375 etcd://172.18.0.1:4001
ff03eeee98d7a14fb30e8ad7aac9ec52512a15f39017147978dab2fec5e7d35a
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run -ti -d --restart=always --name shipyard-swarm-agent swarm:latest join --addr 172.18.0.1:2375 etcd://172.18.0.1:4001
80c315da27db763dd45882b2ad478f132d1a4a18d765a346d08c0f66426d6f5d
[root@VM-0-17-centos ~]# docker run -ti -d --restart=always --name shipyard-controller --link shipyard-rethinkdb:rethinkdb --link shipyard-swarm-manager:swarm -p 9090:8080 shipyard/shipyard:latest server -d tcp://swarm:3375
c58d2b10eef19777b331d39b6596481811e73c91360f0c12051383a4ca2a9908
[root@VM-0-17-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c58d2b10eef1 shipyard/shipyard:latest "/bin/controller s..." 5 minutes ago Up 5 minutes 0.0.0.0:9090->8080/tcp shipyard-controller
80c315da27db swarm:latest "/swarm join --add..." 5 minutes ago Up 5 minutes 2375/tcp shipyard-swarm-agent
ff03eeee98d7 swarm:latest "/swarm manage --h..." 5 minutes ago Up 5 minutes 2375/tcp shipyard-swarm-manager
8356b6a7410a shipyard/docker-proxy:latest "/usr/local/bin/run" 6 minutes ago Up 6 minutes 0.0.0.0:2375->2375/tcp shipyard-proxy
19a768bf00d2 microbox/etcd:latest "/bin/etcd -name d..." 6 minutes ago Up 6 minutes 0.0.0.0:4001->4001/tcp, 0.0.0.0:7001->7001/tcp shipyard-discovery
a2b8f72978f0 rethinkdb "rethinkdb --bind all" 6 minutes ago Up 6 minutes 8080/tcp, 28015/tcp, 29015/tcp shipyard-rethinkdb
a57ed4735ea4 newtomcat "/bin/sh -c '/usr/..." 28 minutes ago Up 28 minutes 0.0.0.0:8888->8080/tcp xenodochial_lumiere
[root@VM-0-17-centos ~]#
上面的安装命令看起来比较杂,整理后安装命令如下:
docker pull rethinkdb
docker pull microbox/etcd
docker pull shipyard/docker-proxy
docker pull swarm
docker pull shipyard/shipyard
// 需要防火墙开放4001端口 -- 或者其他开放命令
sudo ufw allow 4001
//启动成容器
// 172.17.0.1 需要改动成自己的 docker0 的ip
docker run -ti -d --restart=always --name shipyard-rethinkdb rethinkdb
docker run -ti -d -p 4001:4001 -p 7001:7001 --restart=always --name shipyard-discovery microbox/etcd:latest -name discovery
docker run -ti -d -p 2375:2375 --hostname=$HOSTNAME --restart=always --name shipyard-proxy -v /var/run/docker.sock:/var/run/docker.sock -e PORT=2375 shipyard/docker-proxy:latest
docker run -ti -d --restart=always --name shipyard-swarm-manager swarm:latest manage --host tcp://0.0.0.0:3375 etcd://172.18.0.1:4001
docker run -ti -d --restart=always --name shipyard-swarm-agent swarm:latest join --addr 172.18.0.1:2375 etcd://172.18.0.1:4001
docker run -ti -d --restart=always --name shipyard-controller --link shipyard-rethinkdb:rethinkdb --link shipyard-swarm-manager:swarm -p 9090:8080 shipyard/shipyard:latest server -d tcp://swarm:3375
默认 用户名 admin,密码 shipyard
以上:内容部分参考
网络视频
如有侵扰,联系删除。 内容仅用于自我记录学习使用。如有错误,欢迎指正