美文网首页
Docker极简入门

Docker极简入门

作者: _晓石头_ | 来源:发表于2020-08-19 21:01 被阅读0次

Docker是什么?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

1、可以粗糙地理解为轻量级的虚拟机。(确实不是虚拟机)

2、开挂的chroot

image

linux安装

sudo wget -qO- https://get.docker.com/ | sh
sudo usermod -aG docker imooc

Docker架构

image image

docker运行第一个容器(hello-world镜像对应的容器)

查看docker所有的镜像。

docker images
image

运行hello-world镜像对应的容器

docker run hello-world
image

为了生成这条消息,Docker 采取了以下步骤:

  1. Docker 客户端联系了 Docker 守护进程。

  2. Docker 守护进程从 Docker Hub 中提取“ hello-world”镜像。(amd64)

  3. Docker 守护进程从该镜像创建了一个新的容器,该容器运行可执行文件,它生成当前正在读取的输出。

  4. Docker 守护进程将输出流流到 Docker 客户端,然后由 Docker 客户端发送到你的终端。

运行ubuntu 容器。

docker run -it ubuntu bash  # -it表示【控制台交互】【终端登录】
docker run ubuntu echo hello docker # 直接返回容器运行结果
image

运行nginx容器

docker run -p 28080:80 -d nginx  


参数含义:
-p 主机的28080端口映射容器的80端口
-d 容器在后台运行容器并打印容器ID
image

查看运行的容器

docker ps
image

查看nginx运行结果

image

修改nginx的主页显示。

  # 新建文件index.html
<html>
    <h1>Nginx running on Docker!</h1>
</html>

将新建的index.html替换nginx原有的默认页

docker cp index.html {容器ID}://usr/share/nginx/html  # 将{容器ID}换成docker ps查询出来的值

刷新页面得到:

image

停止再开始容器,页面保持修改后不变。

如果容器停止后,重新docker run运行新容器,页面是nginx的默认页。

想要让修改的页面保存进镜像,需要如下操作。

修改容器生成新镜像

docker commit -m 'Nginx running on Docker!' a5249807d2f9 nginx-html


参数解释:
-m 修改的注释
a5249807d2f9 容器的ID
nginx-html 新生成的镜像的名字
image

运行新的容器,nginx的默认页已经是修改后的默认页了。

image

常用命令

命令 含义
docker search {镜像名} 搜索镜像
docker pull {镜像名}:{tag} 拉取镜像
docker images 查看镜像
docker rmi {镜像ID} 删除镜像
docker run -p {host的port}:{container的port} -d {镜像名} 运行容器
docker ps 查看正在运行的容器
docker ps -a 查看所有容器
docker rm {容器ID} {容器ID} 删除多个容器
docker cp {文件名} {容器ID}://{目标路径} 在host和container之间拷贝文件
docker commit 通过容器保存成新的image
docker build 通过dockerfile新建image
docker exec -it {容器名} /bin/bash 进入容器

设置镜像加速器

vim /etc/docker/daemon.json  # 修改如下


{
  "registry-mirrors": [
    "https://mirror.ccs.tencentyun.com",
      "https://hub-mirror.c.163.com",
      "https://reg-mirror.qiniu.com",
      "https://docker.mirrors.ustc.edu.cn",
      "https://dockerhub.azk8s.cn",
      "https://registry.docker-cn.com"
  ]
}

dockerfile的使用

通过dockerfile新建image

新建目录d1,在目录d1下新建文件夹Dockerfile

├── d1
│   └── Dockerfile

Dockerfile的内容如下:

FROM alpine:latest
MAINTAINER author_name
CMD echo "Hello Docker!"

第一行:继承的镜像

第二行:作者信息

第三行:命令

执行镜像生成命令:

 # 进入Dockerfile所在的目录执行
docker build -t hello_docker .


参数解释:
-t:打标签
hello_docker:镜像名称
.: 表示当前目录
image

dockerfile实战-生成nginx镜像

创建目录d2,在目录下添加文件Dockerfile、 index.html

├── d2
│   ├── Dockerfile
│   └── index.html


Dockerfile内容:

FROM ubuntu
MAINTAINER author_name
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nginx
COPY index.html /var/www/html
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
EXPOSE 80

index.html内容:

有趣的Docker!

在Dockerfile所在目录,执行生成image的命令:

docker build -t ubuntu-nginx .
image

镜像分层

Dockerfile里的每一行都产生一个新层,且是只读层。

运行后产生的容器层,是可读写层。

image

分层的好处,共享相同层。

存储

数据挂载的三种方式

1、docker run -v {path} {镜像名}

通过docker inspect {镜像名}查看容器挂载host的哪个目录

image

进入容器的命令docker exec -it {容器名} /bin/bash

通过mount命令查看挂载信息

2、docker run -v {host path}:{container path} {镜像名}

3、docker run --volumes-from {容器名} {镜像名}

registry镜像仓库

1、搜素镜像

docker search whalesay

2、拉取镜像

docker pull whalesay

3、发布镜像

docker push myname/whalesay

多容器app

docker-compose下载安装

curl -L https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m) > /usr/local/bin/docker-compose

给所有用户添加执行权限:

chmod +x /usr/local/bin/docker-compose
image

docker-compose常用命令

查看启动失败的log

docker logs -f -t --tail 20 blog_ghost-app_1

停止编排的所有容器

docker-compose stop

删除编排的所有容器

docker-compose rm

重新创建镜像

docker-compose build

启动服务

docker-compose up -d

相关文章

网友评论

      本文标题:Docker极简入门

      本文链接:https://www.haomeiwen.com/subject/bmgdjktx.html