Docker是什么?
docker其实是一家公司名,其实我们常说的docker实际指的是容器。
容器就是再隔离的环境运行的一个进程,如果进程停止,容器就会被销毁。隔离的环境拥有自己的系统文件,ip地址,主机名等。
白话点讲,我们自己的笔记本经常会安装虚拟机,才安装应用,中间件或数据库等软件。这个传统意义上叫虚拟化,因为一般虚拟化出来的主机会安装完整的操作系统,并且会运行我们需要运行的程序,并且是多个(web+app+DB)。
容器化,我们一般刚接触的人员,可以理解成小的虚拟化机器。你的程序在容器里面跑,同样需要具备基础环境+程序。
一般我们应用容器化,我们需要准备docker的基础镜像,一般是系统镜像,像常用的aplne,centos,ubuntu等,然后将我们的程序运行上去。
逻辑上其实就跟你在linux主机上,安装你的程序一样都需要具备,比如需要jdk,gcc等。容器可以理解成小的虚拟机,如果你需要web+app+DB,如果是docker容器,你就需要多个容器实例(web一个,app一个,db一个)
上面这么说,可能读者就糊涂了,要这么麻烦,干嘛要用容器啊。我们以前公司开发程序,开发打包,给测试,测试安装测试环境,然后发往生产,经常会出现,原来开发那,测试那没有的问题,到现场就莫名其妙的错,可能是因为某些依赖不通,比如jdk版本,操作系统版本等等。使用容器你就可以避免这些问题了,除了配置文件的变更,所有的依赖都可以做成镜像,直接发布使用即可,当然这只是其中一个有点。
相当于jdk,一次构建,随处可用。其他我就列举一下,读者慢慢体会
- 灵活:即使是最复杂的应用也可以集装箱化。
- 轻量级:容器利用并共享主机内核。
- 可互换:您可以即时部署更新和升级。
- 便携式:您可以在本地构建,部署到云,并在任何地方运行。
- 可扩展:您可以增加并自动分发容器副本。
- 可堆叠:您可以垂直和即时堆叠服务。
安装Docker
一般centos7以上基本都可以直接用如下命令安装
yum install -y docker-io
也可以通过阿里云的方式安装,具体操作如下,也可以上阿里云网站查询
由于国外的yum源有的时候访问不了,我们一般采用阿里云镜像源
centos镜像源:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 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
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo systemctl enable docker
sudo systemctl start docker
docker的基本命令
1、docker启动
[root@linux01 ~]# systemctl start docker
2、查看docker镜像列表
[root@linux01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.37-oracle a17d55060104 13 days ago 404MB
bitnami/etcd latest f8cade17916c 13 days ago 152MB
prom/prometheus latest 514e6a882f6e 2 weeks ago 204MB
quay.io/coreos/etcd latest 61ad63875109 3 years ago 39.5MB
从左到右依次是镜像名称、镜像标签、镜像ID
3、查看当前主机运行的docker容器
[root@linux01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6658e8093fe4 nginx "/docker-entrypoint.…" 19 seconds ago Up 18 seconds 80/tcp zealous_hellman
4、查询可用的docker镜像,这些镜像是在互联网上的,有的可能因为网络问题,无法下载
[root@linux01 ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16439 [OK]
bitnami/nginx Bitnami nginx Docker Image 120 [OK]
bitnami/wordpress-nginx Bitnami Docker Image for WordPress with NGINX 56 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 33
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 16 [OK]
rancher/nginx-ingress-controller 10
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
bitnami/nginx-ldap-auth-daemon 3
5、将需要的镜像下载至本地
[root@linux01 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:1c13bc6de5dfca749c377974146ac05256791ca2fe1979fc8e8278bf0121d285
Status: Image is up to date for nginx:latest
docker.io/library/nginx:latest
6、启动一个docker 镜像
[root@linux01 ~]# docker run -it -d -p 8080:80 nginx
b385e2f2e6ac869eca03ed0305e6feea4430850f26926130c5582eb2cdcb551c
[root@linux01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b385e2f2e6ac nginx "/docker-entrypoint.…" 2 seconds ago Up 1 second 0.0.0.0:8080->80/tcp, :::8080->80/tcp heuristic_vaughan
[root@linux01 ~]# curl http://127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@linux01 ~]#
7、进入docker 镜像,如进入上面启动的nginx
#docker exec -it 容器ID 指定shell
[root@linux01 ~]# docker exec -it b385e2f2e6ac /bin/bash
root@b385e2f2e6ac:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc
8、如何停止docker镜像
#docker stop 容器ID or docker rm -f 容器ID
[root@linux01 ~]# docker stop b385e2f2e6ac
b385e2f2e6ac
#再启动一个nginx,然后删除
[root@linux01 ~]# docker run -it -d -p 8080:80 nginx
a16a82b6d636acaef906e3bbba80a0832b5b6be2ca5ea6101ecf1544f56807d3
[root@linux01 ~]# docker rm -f a16a82b6d636acaef906e3bbba80a0832b5b6be2ca5ea6101ecf1544f56807d3
a16a82b6d636acaef906e3bbba80a0832b5b6be2ca5ea6101ecf1544f56807d3
9、学会使用docker -h,查看一些常见的参数
[root@linux01 ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and
default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
输出.....
网友评论