-
虚拟化技术与Docker区别
1、虚拟化技术:将服务,网络,内存,存储等硬件抽象出来运行的一整套完整操作系统。运行进程属于虚拟系统。
2、Docker:通过一些linux内核技术,对进程进行封装、文件系统,网络隔离等,没有独立的存储空间以及硬件资源,属于相对于虚拟化技术轻量级。
基本概念
1、镜像(image)
相当于一个root文件系统,Docker的每个镜像是分层存储,每一层隶属一个root文件系统层。相当于java中的类。
2、容器(container)
以镜像层为基础层,在其上创建一个容器存储层而独立运行的进程。相当于java类创建的实例。
3、仓库(repository)
用于分发镜像,集中存储的服务。类似于maven中央仓库。Docker 的中央镜像服务器是Docker Hub。还有其他的Docker Registry 服务。国内的镜像服务仓库被称为加速器。比如网易云镜像服务,阿里云镜像库等。同时可以自己本地搭建Registry服务。Docker 安装(如下是Centos7 64位安装CE版本)
Docker 分为 CE 和 EE 两大版本。CE 即社区版(免费,支持周期 7 个月),EE 即企业版,强调安全,付费使用,支持周期 24 个月。
- moby、docker-ce与docker-ee
最早的时候docker就是一个开源项目,主要由docker公司维护。
2017年年初,docker公司将原先的docker项目改名为moby,并创建了docker-ce和docker-ee。
这三者的关系是:
moby是继承了原先的docker的项目,是社区维护的的开源项目,谁都可以在moby的基础打造自己的容器产品
docker-ce是docker公司维护的开源项目,是一个基于moby项目的免费的容器产品
docker-ee是docker公司维护的闭源产品,是docker公司的商业产品。
moby project由社区维护,docker-ce project是docker公司维护,docker-ee是闭源的。
要使用免费的docker,从网页docker-ce上获取。
要使用收费的docker,从网页docker-ee上获取。
1、添加Docker源以及安装启动
[root@Eden666 ~]# yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@Eden666 ~]# yum install docker-ce
[root@Eden666 ~]# systemctl enable docker //用于设置Docker在系统重启时,自动启动Docker服务
//systemctl disable docker:取消系统重启时自动启动docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@Eden666 ~]# systemctl start docker
//重新启动:service docker restart
2、测试是否安装正确
[root@Eden666 ~]# docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
[root@Eden666 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@Eden666 ~]# docker run -it ubuntu bash //测试Ubuntu镜像
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
32802c0cfa4d: Pull complete
da1315cffa03: Pull complete
fa83472a3562: Pull complete
f85999a86bef: Pull complete
Digest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49
Status: Downloaded newer image for ubuntu:latest
root@12e3f456d4c2:/# exit //退出当前镜像容器
exit
- 其他操作命令
1、 卸载Docker包:yum remove docker-ce
2、安装完docker后,宿主机上有图像,容器,卷或自定义配置文件等信息在文件/var/lib/docker下,若需要删除,则同时需要rm -rf /var/lib/docker
Docker中软件环境安装
1、jdk
- docker pull java 默认拉取Docker Hub 官方仓库中latest镜像。
[root@Eden666 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 93fd78260bd1 10 days ago 86.2MB
hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
java latest d23bdf5b1b1b 22 months ago 643MB
运行docker:docker run -it --name java java
重新使用别名后再次运行:docker run -it java
其中,--name后面的“java”是为容器指定了一个别名,而最后的那个“java”指的是下载镜像时的名称。
命令以及参数的含义:
run:启动一个镜像容器
-d:指定容器运行于后台
-it:-i 和 -t 的缩写;
-i:以交互模式运行容器,通常与 -t 同时使用
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用
--name:指定容器名字,后续可以通过名字进行容器管理进入java容器并测试
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Eden666 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 93fd78260bd1 10 days ago 86.2MB
hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
java latest d23bdf5b1b1b 22 months ago 643MB
[root@Eden666 ~]# docker run -it java
root@c04af2d9dbfa:/# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2~bpo8+1-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)
root@c04af2d9dbfa:/#
- 多窗口进入java容器的方式。
1、docker attach 容器id
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c3c1957ca37 java "/bin/bash" 8 seconds ago Up 8 seconds trusting_galois
[root@Eden666 ~]# docker attach 9c //只需两位就行
2、docker exec -it 容器id /bin/bash
[root@Eden666 ~]# docker exec -it 82 /bin/bash
root@822df775abf9:/# exit
exit
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
822df775abf9 java "/bin/bash" 52 seconds ago Up 50 seconds practical_wright
[root@Eden666 ~]# docker stop 82
82
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
以上两种进入两种进入docker 容器的方式区别:attach方式会导致不同终端窗口阻塞,若其中一个终端退出以及任何操作会影响其他终端窗口同样变化。exec方式可以操作不同终端窗口命令,互不影响。
- 停止容器:docker stop java
2、tomcat
[root@Eden666 ~]#docker pull tomcat
[root@Eden666 ~]# docker run -p 9300:8080 tomcat //加上 -d 参数控制台不会输出日志信息以及会后台运行
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
[root@Eden666 ~]# netstat -na | grep 9300
tcp6 0 0 :::9300 :::* LISTEN
说明:docker stop 容器id 并不是删除了容器,而是标志位Exited 状态。docker run 每次启动都是全新的docker
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
[root@Eden666 ~]# docker stop 5a
5a
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 12 minutes ago Exited (143) 56 seconds ago cocky_ritchie
822df775abf9 java "/bin/bash" 35 minutes ago Exited (0) 34 minutes ago practical_wright
9c3c1957ca37 java "/bin/bash" 40 minutes ago Exited (0) 36 minutes ago trusting_galois
c04af2d9dbfa java "/bin/bash" 42 minutes ago Exited (0) 40 minutes ago lucid_dubinsky
6a5e5a96f7ae java "/bin/bash" About an hour ago Exited (0) About an hour ago reverent_margulis
75a7f9618f3e java "/bin/bash" About an hour ago Exited (0) About an hour ago hardcore_beaver
b4bd7bc6afb1 java "/bin/bash" About an hour ago Exited (1) About an hour ago gifted_aryabhata
48d5abffea90 java "/bin/bash" About an hour ago Exited (0) About an hour ago affectionate_margulis
a34aad33ff1c java "/bin/bash" About an hour ago Exited (0) 24 minutes ago java
35ffb2bfbd4e hello-world "/hello" 2 hours ago Exited (0) 2 hours ago zealous_curran
bee48ff4fd0a hello-world "/hello" 25 hours ago Exited (0) 25 hours ago gracious_benz
12e3f456d4c2 ubuntu "bash" 25 hours ago Exited (0) 25 hours ago thirsty_hellman
[root@Eden666 ~]# docker start 5a
5a
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 12 minutes ago Up 3 seconds 0.0.0.0:9300->8080/tcp cocky_ritchie
[root@Eden666 ~]# docker run -dp 9200:8080 tomcat
4e0e74a94cafa8fb03c50d4ca3fd6f3a51360d2cc9d477173c3d106bd3377ef2
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0e74a94caf tomcat "catalina.sh run" 18 seconds ago Up 17 seconds 0.0.0.0:9200->8080/tcp upbeat_sammet
5a99d71dae3d tomcat "catalina.sh run" 16 minutes ago Up 3 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
822df775abf9 java "/bin/bash" 40 minutes ago Exited (0) 39 minutes ago practical_wright
9c3c1957ca37 java "/bin/bash" 44 minutes ago Exited (0) 40 minutes ago trusting_galois
c04af2d9dbfa java "/bin/bash" About an hour ago Exited (0) 44 minutes ago lucid_dubinsky
6a5e5a96f7ae java "/bin/bash" About an hour ago Exited (0) About an hour ago reverent_margulis
75a7f9618f3e java "/bin/bash" About an hour ago Exited (0) About an hour ago hardcore_beaver
b4bd7bc6afb1 java "/bin/bash" About an hour ago Exited (1) About an hour ago gifted_aryabhata
48d5abffea90 java "/bin/bash" About an hour ago Exited (0) About an hour ago affectionate_margulis
a34aad33ff1c java "/bin/bash" About an hour ago Exited (0) 28 minutes ago java
35ffb2bfbd4e hello-world "/hello" 2 hours ago Exited (0) 2 hours ago zealous_curran
bee48ff4fd0a hello-world "/hello" 25 hours ago Exited (0) 25 hours ago gracious_benz
12e3f456d4c2 ubuntu "bash" 25 hours ago Exited (0) 25 hours ago thirsty_hellman
如何删除Docker中状态为Exited的容器
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0e74a94caf tomcat "catalina.sh run" 18 seconds ago Up 17 seconds 0.0.0.0:9200->8080/tcp upbeat_sammet
5a99d71dae3d tomcat "catalina.sh run" 16 minutes ago Up 3 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
822df775abf9 java "/bin/bash" 40 minutes ago Exited (0) 39 minutes ago practical_wright
9c3c1957ca37 java "/bin/bash" 44 minutes ago Exited (0) 40 minutes ago trusting_galois
c04af2d9dbfa java "/bin/bash" About an hour ago Exited (0) 44 minutes ago lucid_dubinsky
6a5e5a96f7ae java "/bin/bash" About an hour ago Exited (0) About an hour ago reverent_margulis
75a7f9618f3e java "/bin/bash" About an hour ago Exited (0) About an hour ago hardcore_beaver
b4bd7bc6afb1 java "/bin/bash" About an hour ago Exited (1) About an hour ago gifted_aryabhata
48d5abffea90 java "/bin/bash" About an hour ago Exited (0) About an hour ago affectionate_margulis
a34aad33ff1c java "/bin/bash" About an hour ago Exited (0) 28 minutes ago java
35ffb2bfbd4e hello-world "/hello" 2 hours ago Exited (0) 2 hours ago zealous_curran
bee48ff4fd0a hello-world "/hello" 25 hours ago Exited (0) 25 hours ago gracious_benz
12e3f456d4c2 ubuntu "bash" 25 hours ago Exited (0) 25 hours ago thirsty_hellman
[root@Eden666 ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
[root@Eden666 ~]# docker rm $(sudo docker ps -qf status=exited)
822df775abf9
9c3c1957ca37
c04af2d9dbfa
6a5e5a96f7ae
75a7f9618f3e
b4bd7bc6afb1
48d5abffea90
a34aad33ff1c
35ffb2bfbd4e
bee48ff4fd0a
12e3f456d4c2
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0e74a94caf tomcat "catalina.sh run" 3 minutes ago Up 3 minutes 0.0.0.0:9200->8080/tcp upbeat_sammet
5a99d71dae3d tomcat "catalina.sh run" 19 minutes ago Up 7 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
- 配置国内镜像加速参考链接(http://www.docker-cn.com/registry-mirror)
3、mysql
[root@Eden666 docker]# docker run -dp 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 --name=mysql mysql
44de7df2a0fd8307b5ae631e48ffc7e448df496d062340273840ab039b5d5e55
[root@Eden666 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
44de7df2a0fd mysql "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp mysql
[root@Eden666 docker]# docker exec -it 44 /bin/bash
root@44de7df2a0fd:/# mysql -u root -p123456
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)