当前位置:   article > 正文

docker部署、swarm、私有仓库_docker swarm构建镜像

docker swarm构建镜像

引用地址:[docker]docker初学习-docker部署、swarm、私有仓库 - 码码忙盲 - 博客园

1 docker部署

centos

安装基础软件
yum install -y yum-utils device-mapper-persistent-data lvm2
配置阿里云docker镜像源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
查看docker版本:yum list docker-ce --showduplicates
安装docker
选择版本安装:yum install docker-ce-18.03.0.ce
安装最新版本:yum install docker-ce
设置docker开机启动:systemctl enable docker
配置docker仓库-使用申请的阿里加速器
阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台
vi /etc/docker/daemon.json
{
"registry-mirrors": "https://****.mirror.aliyuncs.com"  # 自己用自己的
}
sudo systemctl enable docker
systemctl start docker

ubuntu18

更新系统包索引
sudo apt-get update
添加HTTPS协议,允许apt从HTTPS安装软件包
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
添加Docker 公共密钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
设置版本库类型(Ubuntu18.04对应版本“bionic“”)选一个

问题汇总

  • docker启动失败


问题解决:未关闭防火墙
systemctl stop firewalld
systemctl disabled firewalld

  • 报错Failed to start Docker Application Container Engine


问题解决:修改daemon
mv /etc/docker/daemon.json /etc/docker/daemon.conf

容器部署参考

  • mysql

docker mysql文档Docker Hub
docker run -d --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wordpress mysql
此时不需要被外界访问,则不需要进行端口映射

  • wordpress

docker wordpress文档Docker Hub
docker rn -d -e WOREPRESS_DB_HOST=mysql:3306 --link mysql -p 8080:80 wordpress
登录127.0.0.1::8080 进行安装并登录wordpress

2 Docker操作

查看容器ip

docker inspect id

Dockerfile

构建jdk1.8镜像

创建路径并拉取jdk1.8 tar.gz包到路径下,
编辑Dockerfile

FROM centos:7  # 关联系统镜像
MAINTAINER wyx  # 指定创建者
WORKDIR /usr
RUN mkdir /usr/local/java  # 创建目录
ADD jdk-8u231-linux-x64.tar.gz /usr/local/java/  # 上传压缩包 自动解压

ENV JAVA_HOME /usr/local/java/jdk1.8.0_231  # 配置环境变量
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/bin/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH

构建镜像
docker build -t='jdk1.8' .
-t 指定镜像名称
. 目录为当前目录

[root@vm01 dockerjdk8]# docker build -t='jdk1.8' .
Sending build context to Docker daemon  194.2MB
Step 1/9 : FROM centos:7
7: Pulling from library/centos
ab5ef0e58194: Pull complete
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
 ---> 5e35e350aded
Step 2/9 : MAINTAINER wyx
 ---> Running in 88be083190e4
Removing intermediate container 88be083190e4
 ---> 1ef184104707
Step 3/9 : WORKDIR /usr
 ---> Running in 7c3938bb2c9b
Removing intermediate container 7c3938bb2c9b
 ---> 51e5ab1cca89
Step 4/9 : RUN mkdir /usr/local/java
 ---> Running in 56aebc8491a0
Removing intermediate container 56aebc8491a0
 ---> e77ccf2a1f91
Step 5/9 : ADD jdk-8u231-linux-x64.tar.gz /usr/local/java/
 ---> b1a4632b66c1
Step 6/9 : ENV JAVA_HOME /usr/local/java/jdk1.8.0_231
 ---> Running in f6bb0539032d
Removing intermediate container f6bb0539032d
 ---> 4945a29dc602
Step 7/9 : ENV JRE_HOME $JAVA_HOME/jre
 ---> Running in cdbf0fd64909
Removing intermediate container cdbf0fd64909
 ---> ef86cf5b0873
Step 8/9 : ENV CLASSPATH $JAVA_HOME/bin/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
 ---> Running in ba11af8b235b
Removing intermediate container ba11af8b235b
 ---> 74e88b807e73
Step 9/9 : ENV PATH $JAVA_HOME/bin:$PATH
 ---> Running in c4860a6cf81f
Removing intermediate container c4860a6cf81f
 ---> e6c88f955de4
Successfully built e6c88f955de4
Successfully tagged jdk1.8:latest

使用docker images 查看已构建镜像

[root@vm01 dockerjdk8]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jdk1.8              latest              e6c88f955de4        3 minutes ago       606MB
rancher/rancher     latest              76d691616967        2 days ago          669MB
centos              7                   5e35e350aded        2 months ago        203MB
java                8                   d23bdf5b1b1b        3 years ago         643MB

3 使用Portainer管理Docker

创建portainer容器即可管理整个docker管理机

在每台主机上安装

docker pull portainer/portainer
每台主机上开放docker网络管理接口2375
vi /etc/sysconfig/docker
尾部添加
OPTIONS='-Htcp://0.0.0.0:2375 -H unix:///var/run/docker.sock'
重启生效配置
service docker restart
启动portainer容器
docker run -d -p 9000:9000 portainer/portainer -H tcp://192.168.99.104:2375

登录管理页面

192.168.99.104:9000
注册账号密码admin/admin

添加管理节点


修改管理节点-- 左上角CHANGE ENVIRONMENT

查看swarm状态


创建共享网络

创建数据卷

创建container


添加端口映射

映射数据卷

添加已创建网络

添加环境变量

硬件资源配置-可使用默认

完成创建

4 Docker私有仓库

1.搭建私有仓库

1.拉取私有仓库镜像
docker pull registry
2.启动私有仓库容器
docker run -di --name=registry -p 5000:5000 registry
3.打开浏览器输入http://IP:5000/v2/_catalog,如图显示则部署成功

当前没有存入镜像 所以是空的
4.修改配置文件 信任私有仓库地址
vi /etc/docker/daemon.conf
{
"insecure-registries": "192.168.56.112:5000"
}

systemctl restart docker

将镜像上传到私有仓库

1.给需要上传的镜像打标签
docker tag jdk1.8(指定镜像) 192.168.56.112:5000/jdk1.8(标签名)
生成的新镜像实际与原镜像相同,IMAGE ID无变化

  1. [root@vm01 dockerjdk8]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. 192.168.56.112:5000/jdk1.8 latest e6c88f955de4 7 minutes ago 606MB
  4. jdk1.8 latest e6c88f955de4 7 minutes ago 606MB

2.上传镜像
docker push 192.168.56.112:5000/jdk1.8
报错,与添加信任私有仓库地址可能有关,修改后无用

Get https://192.168.56.112:5000/v2/: http: server gave HTTP response to HTTPS client

解决:编辑vim /usr/lib/systemd/system/docker.service
在ExecStart后加入私有仓库变量 --insecure-registry ip:5000

ExecStart=/usr/bin/dockerd --insecure-registry 192.168.56.112:5000

重启后 重新push成功

[root@vm01 dockerjdk8]# docker push 192.168.56.112:5000/jdk1.8
The push refers to repository [192.168.56.112:5000/jdk1.8]
b40274eaddf3: Pushed
b1dc884c462a: Pushed
77b174a6a187: Pushed
latest: digest: sha256:d879451e0bce62c2585da77d23899cf473e1ff1b29c07174740984b8e2072759 size: 949

此时查看页面已有上传镜像名称

从镜像仓库下载

docker pull 192.168.56.112:5000/jdk1.8

[root@vm01 dockerjdk8]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
192.168.56.112:5000/jdk1.8   latest              e6c88f955de4        2 hours ago         606MB
jdk1.8                       latest              e6c88f955de4        2 hours ago         606MB
rancher/rancher              latest              76d691616967        2 days ago          669MB
centos                       7                   5e35e350aded        2 months ago        203MB
java                         8                   d23bdf5b1b1b        3 years ago         643MB
[root@vm01 dockerjdk8]# docker rmi 192.168.56.112:5000/jdk1.8
Untagged: 192.168.56.112:5000/jdk1.8:latest
Untagged: 192.168.56.112:5000/jdk1.8@sha256:d879451e0bce62c2585da77d23899cf473e1ff1b29c07174740984b8e2072759
[root@vm01 dockerjdk8]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jdk1.8              latest              e6c88f955de4        2 hours ago         606MB
rancher/rancher     latest              76d691616967        2 days ago          669MB
centos              7                   5e35e350aded        2 months ago        203MB
java                8                   d23bdf5b1b1b        3 years ago         643MB
[root@vm01 dockerjdk8]# docker pull 192.168.56.112:5000/jdk1.8
Using default tag: latest
latest: Pulling from jdk1.8
Digest: sha256:d879451e0bce62c2585da77d23899cf473e1ff1b29c07174740984b8e2072759
Status: Downloaded newer image for 192.168.56.112:5000/jdk1.8:latest
[root@vm01 dockerjdk8]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
192.168.56.112:5000/jdk1.8   latest              e6c88f955de4        2 hours ago         606MB
jdk1.8                       latest              e6c88f955de4        2 hours ago         606MB
rancher/rancher              latest              76d691616967        2 days ago          669MB
centos                       7                   5e35e350aded        2 months ago        203MB
java                         8                   d23bdf5b1b1b        3 years ago         643MB

标签: docker

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/752518
推荐阅读
相关标签
  

闽ICP备14008679号