容器背景:
层级架构:
容器对比传统化虚拟机:
可以把docker理解成是一款自带软件(比如:nignx、tomcat。。。。。)的镜像操作系统(首先是要下载镜像)
以下是Windows环境安装Docker
===========================================================================================================
本次使用CentOS系统进行试验,两台都进行如图配置
系统LinuxDocker安装:
环境:
docker-1 | 192.168.200.113 |
安装docker-ce------》这种是新版本的 #ce表示社区版、ee表示企业版
如果已经有了老版本的docker直接使用yum -y remove docker移除老版本的docker
两台主机都执行本次操作
[root@localhost ~]# hostname docker-1
[root@localhost ~]# bash
[root@docker-1 ~]# cd /etc/yum.repos.d/
[root@docker-1 yum.repos.d]#wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@docker-1 yum.repos.d]# ls
a Centos-7.repo local.repo
[root@docker-1 yum.repos.d]# mv a/CentOS-Base.repo ./
[root@docker-1 yum.repos.d]# ls
a Centos-7.repo CentOS-Base.repo local.repo
[root@docker-1 yum.repos.d]# yum install yum-utils device-mapper-persistent-data lvm2 -y #底层依赖包
[root@docker-1 yum.repos.d]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
[root@docker-1 yum.repos.d]# yum install docker-ce -y
设置启动docker服务并设置开机自启
[root@docker-1 yum.repos.d]# systemctl start docker
[root@docker-1 yum.repos.d]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
dcoker服务启动失败解决方法:
[root@docker yum.repos.d]# systemctl start docker
Job for docker.service failed because the control process exited with error code.
See "systemctl status docker.service" and "journalctl
-xe" for details.
[root@docker yum.repos.d]# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since 四 2020-01-23 11:25:53 CST; 13s ago
Docs: https://docs.docker.com
Process: 8499 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
(code=exited, status=1/FAILURE)
Main PID: 8499 (code=exited, status=1/FAILURE) 1月 23 11:25:51 docker systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
1月 23 11:25:51 docker systemd[1]: Failed to start Docker Application Container Engine.
1月 23 11:25:51 docker systemd[1]: Unit docker.service entered failed state.
1月 23 11:25:51 docker systemd[1]: docker.service failed.
1月 23 11:25:53 docker systemd[1]: docker.service holdoff time over, scheduling restart.
1月 23 11:25:53 docker systemd[1]: Stopped Docker Application Container Engine.
1月 23 11:25:53 docker systemd[1]: start request repeated too quickly for docker.service
1月 23 11:25:53 docker systemd[1]: Failed to start Docker Application Container Engine.
1月 23 11:25:53 docker systemd[1]: Unit docker.service entered failed state.
1月 23 11:25:53 docker systemd[1]: docker.service failed.
[root@docker yum.repos.d]# rm -rf /etc/docker/daemon.json
[root@docker yum.repos.d]# systemctl start docker
[root@docker-1 yum.repos.d]# cd
[root@docker-1 ~]# docker version #查看docker版本
有client和server表示安装成功 Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64 Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64
Experimental: false
进入网址获取加速器地址
加速器用于在提高在网上下载镜像的速度(两台都执行本次操作)
地址:https://cr.console.aliyun.com/ #获取自己的加速器地址
Docker国内源说明:
Docker 官方中国区
https://registry.docker-cn.com
网易
http://hub-mirror.c.163.com
中国科技大学
https://docker.mirrors.ustc.edu.cn
阿里云
https://pee6w651.mirror.aliyuncs.com
加速配置文件:
[root@docker-1 ~]# cat << END > /etc/docker/daemon.json
> {
> "registry-mirrors"["https://pee6w651.mirror.aliyuncs.com"] #这个是阿里云的源
> }
> END
[root@docker-1 ~]# systemctl daemon-reload
[root@docker-1 ~]# systemctl start docker
Docker镜像操作:
[root@docker-1 ~]# docker search nginx #查看nginx镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS…
bitnami/nginx Bitnami nginx Docker Image [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho…
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… [OK]
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. [OK]
centos/nginx--centos7 Platform for running nginx 1.8 or building n…
raulr/nginx-wordpress Nginx front-end for the official wordpress:f… [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles
centos/nginx--centos7 Platform for running nginx 1.12 or building …
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… [OK]
1science/nginx Nginx Docker images that include Consul Temp… [OK]
mailu/nginx Mailu nginx frontend [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX [OK]
wodby/nginx Generic nginx [OK]
centos/nginx--centos7 Platform for running nginx 1.10 or building …
查看好评在100以上的
[root@docker-1 ~]# docker search -s 100 nginx
Flag --stars has been deprecated, use --filter=stars= instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… [OK]
操作命令:
查看版本信息:docker version
查看Docker详细信息:docker info
下载镜像:docker pull 仓库名/镜像名 如docker pull docker.io/nginx,下载的是nginx镜像,下载完成后启动nginx服务就可以使用Nginx服务
上传镜像:docker push 仓库名/镜像名:标签
查看本地镜像:docker images【全部镜像】 docker images nginx【单独查看nginx镜像】
查看镜像的详细信息:docker inspect 镜像ID
修改镜像标签名:docker tag nginx:latest nginx:nginx 1 #此时标签名为nginx 1,用于区分多个nginx
删除镜像:docker rmi 镜像id docker rmi 镜像名:标签名
删除容器:docker rm -f 镜像ID
查找镜像:docker search 镜像名
退出容器:exit同时会停掉容器 按ctrl+p+q退出不会结束容器
进入运行的容器:docker exec -it 容器ID/bin/bash 或者 docker attach 容器ID 都不会生成新的进程
查看容器:docker ps -a(显示全部信息)docker ps -aq(只显示前面ID信息)
容器启动和停止:docker start 容器ID docker stop 容器ID
查看容器内输出:docker logs 容器ID
创建并运行命令:docker run -it -p端口:端口 镜像名:标签名 /bin/bash -c 操作
导出镜像包:docker export 镜像名 > 包名
导入镜像包:cat 包名 | docker import - 镜像名:标签
======================================================================================
镜像
下载nginx并开启nginx
[root@VM_0_4_centos ~]# docker pull docker.io/nginx #下载Nginx镜像
[root@VM_0_4_centos ~]# docker pull tomcat #下载tomcat镜像
[root@VM_0_4_centos ~]# docker images #查看本地镜像仓库
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 7 hours ago 126MB
tomcat latest c81106060722 30 hours ago 507MB
[root@VM_0_4_centos ~]# docker tag nginx:latest nginx:1 #修改镜像名
[root@VM_0_4_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 7 hours ago 126MB
nginx 1 231d40e811cd 7 hours ago 126MB
tomcat latest c81106060722 30 hours ago 507MB
[root@VM_0_4_centos ~]# docker rmi nginx:latest #删除镜像
Untagged: nginx:latest
[root@VM_0_4_centos ~]# docker images #查看本地镜像仓库
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1 231d40e811cd 7 hours ago 126MB
tomcat latest c81106060722 30 hours ago 507MB
------------------------------------------------------------------------------------------------------------------------------------------------
容器
[root@git ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@git ~]# docker run -it -p 80:80 nginx:1 /bin/bash
root@484cb75f4dd1:/# nginx
root@484cb75f4dd1:/# [root@git ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
484cb75f4dd1 nginx:1 "/bin/bash" 17 seconds ago Up 16 seconds 0.0.0.0:80->80/tcp zealous_bell
[root@docker ~]# netstat -lnpt #查看80端口
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 980/sshd
tcp6 0 0 :::80 :::* LISTEN 1749/docker-proxy
[root@docker ~]# docker attach 5bd3be54e193 #进入容器内开启服务
root@5bd3be54e193:/# nginx
===============================================================================================================
容器删除
[root@VM_0_4_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0a225744381 nginx:1 "/bin/bash -c 'while…" 58 seconds ago Up 57 seconds 80/tcp beautiful_mestorf
0c2b714a0a93 nginx:1 "/bin/bash -s ls /" 4 minutes ago Exited (0) 4 minutes ago pensive_einstein
ea594b197fc9 nginx:1 "/bin/bash" 17 hours ago Up 17 hours 0.0.0.0:80->80/tcp wizardly_aryabhata
f103725c8b30 nginx:1 "/bin/bash" 18 hours ago Up 18 hours 80/tcp lucid_swirles
[root@VM_0_4_centos ~]# docker rm -f 0c2b714a0a93 #删除容器
0c2b714a0a93
[root@VM_0_4_centos ~]# docker rm -f a0a #删除容器
a0a
[root@VM_0_4_centos ~]# docker ps -a
CONTAINER ID MAGE COMMAND CREATED STATUS PORTS NAMES
ea594b197fc9 nginx:1 "/bin/bash" 17 hours ago Up17 hours 0.0.0.0:80->80/tcp wizardly_aryabhata
f103725c8b30 nginx:1 "/bin/bash" 18 hours ago Up 18 hours 80/tcp ucid_swirles
[root@VM_0_4_centos ~]# docker run -it nginx /bin/bash #创建容器进入虚拟主机内部
root@75045f836eff:/# 此时退出有两种方法:exit同时会停掉容器 - ctrl+p+q退出不会结束容器进程
root@46097a0e50a7:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
=================================================================================================================
镜像导入导出
[root@VM_0_4_centos ~]#rz
[root@VM_0_4_centos ~]# docker export 4880 > nginx-dtt.tar #导出一个nginx镜像包
[root@VM_0_4_centos ~]# ls
[root@VM_0_4_centos ~]# ls
apache-tomcat-9.0.14.tar.gz mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz nginx-1.tar
jdk-8u191-linux-x64.tar.gz nginx-1.16.1.tar.gz nginx-dtt.tar
[root@VM_0_4_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4880f28fed4b nginx:1 "/bin/bash" About a minute ago Created youthful_ardinghelli
ea594b197fc9 nginx:1 "/bin/bash" 18 hours ago Up 18 hours 0.0.0.0:80->80/tcp wizardly_aryabhata
f103725c8b30 nginx:1 "/bin/bash" 19 hours ago Up 19 hours 80/tcp lucid_swirles
[root@VM_0_4_centos ~]# cat nginx-dtt.tar | docker import - nginx:dtt #将nginx-dtt.tar导入到images镜像源中
sha256:5fce17542029cdc0592a57ef7420211f0fdd13e346832579d913015c3b384865
[root@VM_0_4_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx dtt 5fce17542029 5 seconds ago 125MB
nginx 1 231d40e811cd 26 hours ago 126MB
nginx latest 231d40e811cd 26 hours ago 126MB
==========================================================================================
文件传输
[root@VM_0_4_centos ~]# touch liuxiang #创建一个新文件夹
[root@VM_0_4_centos ~]# docker cp liuxiang ea594b197fc9:/tmp #将文件传输到ea594b197fc9容器的/tmp下
[root@VM_0_4_centos ~]# docker attach ea59 #进入容器内
root@ea594b197fc9:/# ls /tmp #查看文件传输效果
liuxiang
root@ea594b197fc9:/# read escape sequence
===============================================================================================
[root@VM_0_4_centos ~]# mkdir -pv /opt/data/registry
mkdir: created directory ‘/opt/data’
mkdir: created directory ‘/opt/data/registry’
[root@VM_0_4_centos ~]# docker run -d -p 5000:5000 -v /opt/data/registry/:/tmp/registry docker.io/registry
/opt/data/registry/:/tmp/registry #容器内和宿主机目录间做一个映射
docker.io/registry #要运行的镜像
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
c87736221ed0: Pull complete
1cc8e0bb44df: Pull complete
54d33bcb37f5: Pull complete
e8afc091c171: Pull complete
b4541f6d3db6: Pull complete
Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
Status: Downloaded newer image for registry:latest
9e8a1c45d0f386152a3276ef476db194f55a9ebe74fd1b7e113b810ffea020af
[root@VM_0_4_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e8a1c45d0f3 registry "/entrypoint.sh /etc…" 21 hours ago Up 21 hours 0.0.0.0:5000->5000/tcp nifty_chaplygin
8b25538cedb8 nginx:1 "/bin/bash" 21 hours ago Created bold_neumann
4880f28fed4b nginx:1 "/bin/bash" 21 hours ago Created youthful_ardinghelli
ea594b197fc9 nginx:1 "/bin/bash" 40 hours ago Up 39 hours 0.0.0.0:80->80/tcp wizardly_aryabhata
f103725c8b30 nginx:1 "/bin/bash" 40 hours ago Up 40 hours 80/tcp lucid_swirles
[root@VM_0_4_centos ~]# vim /usr/lib/systemd/system/docker.service
修改这一条 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 111.231.11.193:5000
[root@VM_0_4_centos ~]# systemctl daemon-reload
[root@VM_0_4_centos ~]# systemctl start docker
[root@VM_0_4_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e8a1c45d0f3 registry "/entrypoint.sh /etc…" 21 hours ago Up 21 hours 0.0.0.0:5000->5000/tcp nifty_chaplygin
[root@VM_0_4_centos ~]# docker start 9e8a1c45d0f3
9e8a1c45d0f3
[root@VM_0_4_centos ~]# docker tag nginx:latest 172.17.0.4:5000/nginx:latest #修改镜像名
[root@VM_0_4_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
111.231.11.193:5000/nginx latest 231d40e811cd 2 days ago 126MB
[root@VM_0_4_centos ~]# docker push 111.231.11.193:5000/nginx:latest #上传到私有仓库