docker

docker容器

docker 基础命令
  • docker pull # 下载镜像
  • docker run -d -it 加镜像名称 #启动镜像 -i 交互式 -t伪终端 -d 后台运行 -p 端口映射 咧:80:80
  • docker ps #查看启动的容器 加 -a 所有
  • docker save 加镜像名 > /xxx/xxx.tar.gz 导出镜像
  • docker load -i /nginx.tar.gz 导入镜像
  • docker build 构建镜像 -t tag号

docker镜像制作

制作nginx镜像

[root@centos dockerfile]#vim Dockerfile 

FROM  centos    

RUN  dnf install nginx -y

COPY nginx.conf /etc/nginx/

EXPOSE 80 443

CMD  ["nginx"]

[root@centos dockerfile]#vim Dockerfile 

FROM  centos

RUN  dnf install nginx -y

COPY nginx.conf /etc/nginx/

COPY index.html /usr/share/nginx/html/

EXPOSE 80 443

CMD  ["nginx"]

搭建harbor

官方 github 地址:
https://github.com/vmware/harbor

1)安装docker-ce 和 docker-compose,解压harbor

安装docker-compose 需要epel源

# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# yum install docker-compose -y

安装docker

# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start

# 注意:
# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。
# vim /etc/yum.repos.d/docker-ee.repo
#   将[docker-ce-test]下方的enabled=0修改为enabled=1
#
# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
#   Loading mirror speeds from cached hostfile
#   Loaded plugins: branch, fastestmirror, langpacks
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            @docker-ce-stable
#   docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable
#   Available Packages
# Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]

解压harbor

[root@Centos7 ~]#tar xf harbor-offline-installer-v1.7.5.tgz -C /usr/local/

进入目录
[root@Centos7 ~]#cd /usr/local/harbor/

[root@Centos7 harbor]#ll
total 572840
drwxr-xr-x 3 root root        23 May 24 11:39 common
-rw-r--r-- 1 root root       939 Apr  1  2019 docker-compose.chartmuseum.yml
-rw-r--r-- 1 root root       975 Apr  1  2019 docker-compose.clair.yml
-rw-r--r-- 1 root root      1434 Apr  1  2019 docker-compose.notary.yml
-rw-r--r-- 1 root root      5608 Apr  1  2019 docker-compose.yml
-rw-r--r-- 1 root root      8033 Apr  1  2019 harbor.cfg
-rw-r--r-- 1 root root 585234819 Apr  1  2019 harbor.v1.7.5.tar.gz
-rwxr-xr-x 1 root root      5739 Apr  1  2019 install.sh
-rw-r--r-- 1 root root     11347 Apr  1  2019 LICENSE
-rw-r--r-- 1 root root   1263409 Apr  1  2019 open_source_license
-rwxr-xr-x 1 root root     36337 Apr  1  2019 prepare

2)修改配置文件,安装harbor

[root@Centos7 harbor]#vim harbor.cfg 

hostname = 192.168.8.101    #改为本机的IP

开始安装harbor
[root@Centos7 harbor]# ./install.sh 

3)安装完成后,打开浏览器页面访问IP,登录harbor,默认账号为:admin 密码:Harbor12345


image.png

4)在docker节点上修改docker.service文件

加入:--insecure-registry 192.168.8.101 #IP地址是harbor的地址

[root@centos ~]#vim /usr/lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.8.101


让配置文件生效。重启docker
[root@centos ~]#systemctl daemon-reload

[root@centos ~]#systemctl restart docker

5)登录harbor

[root@centos ~]#docker login 192.168.8.101
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded  #成功登录

6)上传镜像到harbor,在harbor的web界面上创建nginx项目


image.png

在docker节点上 先打tag号

[root@centos ~]#docker tag nginx:test2 192.168.8.101/nginx/nginx:test2

[root@centos ~]#docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
192.168.8.101/nginx/nginx   test2               f9536eaaa098        22 hours ago        320MB
nginx                       test2               f9536eaaa098        22 hours ago        320MB
nginx                       text1               3da4ee0c06eb        24 hours ago        320MB


7)上传镜像

[root@centos ~]#docker push 192.168.8.101/nginx/nginx:test2
The push refers to repository [192.168.8.101/nginx/nginx]
bf2700c60026: Pushed 
483ec908cdb1: Pushed 
dfd43c303cb0: Pushed 
0683de282177: Layer already exists 
test2: digest: sha256:1f2b4e0454fc588a77efd1501e681b2e0982eb9c65fd0827ee5244a951757cff size: 1156

下载镜像

[root@centos ~]#docker pull 192.168.8.101/nginx/nginx:test2
test2: Pulling from nginx/nginx
8a29a15cefae: Already exists 
9ed113cce0b8: Pull complete 
81ce9ff67a72: Pull complete 
ff4d54fc8fc6: Pull complete 
Digest: sha256:1f2b4e0454fc588a77efd1501e681b2e0982eb9c65fd0827ee5244a951757cff
Status: Downloaded newer image for 192.168.8.101/nginx/nginx:test2
192.168.8.101/nginx/nginx:test2

查看镜像
[root@centos ~]#docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
192.168.8.101/nginx/nginx   test2               f9536eaaa098        23 hours ago        320MB
nginx                       latest              9beeba249f3e        8 days ago          127MB
alpine                      latest              f70734b6a266        4 weeks ago         5.61MB
centos                      latest              470671670cac        4 months ago        237MB

你可能感兴趣的:(docker)