美文网首页
漫长的Docker路

漫长的Docker路

作者: 二潘 | 来源:发表于2018-08-22 11:45 被阅读21次

Docker已经支持非常多的Linux平台,包括了Ubuntu和RHEL,还支持Debian、CentOS、Fedora、Oracle、Linux等

基本概念

Docker知识库"包括三个基本概念

  • 镜像(Image)
  • 容器(Container)
  • 仓库(Repository)
    先理解了这三个概念,就理解了 Docker 的整个生命周期。

安装docker的先决条件

1、要求的条件如下:
运行64位cpu架构的计算机(目前只能是x86_64和amd64),不支持32位cpu
运行Linux 3.8或者更高版本内核。一些老版本的2.6x或者其后的内核版本也能够运行docker,但运行的结果会有很大的不同。
内核必须支持一种适合的存储驱动:
Device Manager
AUFS
vfs
btrfs
ZFS(在docker1.7中引入)
默认存储驱动通常是Device Manager或AUFS
docker之路参考文章:http://www.dockerinfo.net/document

本地安装docker

可以参照https://blog.csdn.net/san1156/article/details/76038287这篇文档进行安装
1、在线安装docker
通过apt-get安装

  • uname -r #查看内核版本,运行Linux 3.8或者更高版本内核
  • sudo apt-get update # 更新系统资源列表
  • apt-get -y install docker.io # 安装docker
    如果打不开docker,重启docker
  • service docker start #重启动docker
  • docker -v # 查看docker版本信息
    可以查看版本,但是无法搜索内容:原因一:网络原因 原因二:更新源的问题,更改更新源
    网址:
    https://cr.console.aliyun.com/cn-hangzhou/mirrors
    复制镜像加速器
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://2ahozhpp.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2、通过阿里云的镜像仓库安装docker

用户权限修改

安装成功之后,需要使用root用户才能执行docker命令,如果希望用非root用户执行docker命令,可以执行以下语句。
注意:执行完成后,需要退出再登陆才能生效
sudo usermod -aG docker $your-user
logout
注销生效

术语介绍

Docker有仓库Repositories、镜像Images和容器Container三大核心,三者可以互相转化。

  • Docker,平台工具、守护进程
  • Image,镜像,包括应用及系统的只读数据包
  • Container,容器,虚拟机的运行实例
  • Registry,仓库登记,管理镜像文件
  • Docker hub,Docker官方公有仓库
    三大核心的关系,如下图所示:


    image.png

创建镜像

Docker镜像的创建方式有两种,分别是:

  • 基于现有的镜像修改
  • 直接创建新镜像
    本文介绍基于已有的镜像修改的方式,一共分为五步

第一步,下载官网的基础镜像

从官网上pull镜像hello-world,并运行

root@apple:/home/kevin/docker# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete 
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

从官网上pull镜像ubuntu,并以交互方式运行

root@apple:/home/kevin/docker# docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
e0a742c2abfd: Pull complete 
486cb8339a27: Pull complete 
dc6f0d824617: Pull complete 
4f7a5649a30e: Pull complete 
672363445ad2: Pull complete 
Digest: sha256:84c334414e2bfdcae99509a6add166bbb4fa4041dc3fa6af08046a66fed3005f
Status: Downloaded newer image for ubuntu:latest
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#

第二步,在CONTAINER中执行软件的安装

安装Linux基础软件
在第一步的基础上,在容器里面,用root用户执行Linux基础软件的安装。

root@93a1b9d39683:/home# apt-get update      
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]                                         
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [42.0 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [384 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.8 kB]                                                                                
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [178 kB]                                                                                   
Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [2931 B]                                                                                
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]                                                                                               
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]                                                                                         
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]                                                                                           
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]                                                                                          
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [207 kB]                                                                                           
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [742 kB]                                                                                        
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.3 kB]                                                                                 
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [640 kB]                                                                                    
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [16.8 kB]                                                                                 
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [4930 B]                                                                                      
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [6237 B]                                                                                  
Fetched 24.2 MB in 12min 34s (32.1 kB/s)                                                                                                                                   
Reading package lists... Done

root@93a1b9d39683:/home# apt-get install vim 
root@93a1b9d39683:/home# apt-get install curl
root@93a1b9d39683:/home# apt-get install openssh-server 
root@93a1b9d39683:/home# apt-get install python 

# 安装ifconfig、netstat
root@93a1b9d39683:/home# apt-get install net-tools

# 安装ping
root@93a1b9d39683:/home# apt-get install iputils-ping

退出CONTAINER(容器)

  • ctrl+d,退出容器且关闭,docker ps查看容器已退出运行(Exited)
  • ctrl+p,ctrl+q,退出容器但不关闭,docker ps查看容器依然处于Up状态

第三步,将CONTAINER保存为IMAGE

查看容器列表

root@apple:/home/kevin/docker# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
966bd52b72da        ubuntu              "/bin/sh -c 'while..."   3 hours ago         Exited (137) 2 hours ago                       stupefied_knuth
568e5204fff3        ubuntu              "/bin/sh -c 'while..."   4 hours ago         Exited (137) 2 hours ago                       kind_khorana
00f561d97811        ubuntu              "/bin/echo hello w..."   4 hours ago         Exited (0) 4 hours ago                         nifty_mcnulty
93a1b9d39683        ubuntu              "bash"                   4 hours ago         Up 3 hours                                     zealous_noether
abdc084f9821        hello-world         "/hello"                 4 hours ago         Exited (0) 3 hours ago                         sleepy_clarke
51097978fdc6        hello-world         "/hello"                 4 hours ago         Exited (0) 3 hours ago                         vibrant_panini

查看镜像列表

root@apple:/home/kevin/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              14f60031763d        3 days ago          120MB
hello-world         latest              1815c82652c0        5 weeks ago         1.84kB

保存容器到镜像

root@apple:/home/kevin/docker# docker commit 93a1b9d39683 learn/visual_init:v1
sha256:56a4eab7dc5b9c1b0dca010e36f4e792963a213ad4b362ab4662602eb66f0216

==============
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]Create a new image from a container's changes

  -a, --author=""     Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -m, --message=""    Commit message
  -p, --pause=true    Pause container during commit
==============

查看镜像列表

root@apple:/home/kevin/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
learn/visual_init   v1                  56a4eab7dc5b        39 seconds ago      321MB
ubuntu              latest              14f60031763d        3 days ago          120MB
hello-world         latest              1815c82652c0        5 weeks ago         1.84kB

容器的常用操作

root@apple:/home/kevin/docker# docker attach $CONTAINER_ID #连接一个已存在的docker容器
root@apple:/home/kevin/docker# docker stop $CONTAINER_ID #停止docker容器
root@apple:/home/kevin/docker# docker start $CONTAINER_ID #启动docker容器
root@apple:/home/kevin/docker# docker restart $CONTAINER_ID #重启docker容器
root@apple:/home/kevin/docker# docker kill $CONTAINER_ID #强制关闭docker容器
root@apple:/home/kevin/docker# docker logs $CONTAINER_ID #查看docker容器运行日志,确保正常运行
root@apple:/home/kevin/docker# docker inspect $CONTAINER_ID #查看container的属性,比如ip等等
root@apple:/home/kevin/docker# docker rm $CONTAINER_ID      # 删除容器

常用命令

可以看下面文档:
https://gfalisa.github.io/
https://www.menzel3.fun/
进行学习

进入docker容器
docker run -t -i 镜像名称 /bin/bash

查看本地镜像
docker images

存出镜像
docker save -o 名称(自己起) 镜像名称

载入镜像
docker load --input 名称(自己起)
docker load < 名称(自己起)

查看正在运行的容器
docker ps -a

删除容器
docker rm

-f 强制删除

删除镜像
docker rmi

docker启动
systemctl start docker

重启docker
systemctl restart docker

启动容器
docker start 容器名

docker pull 镜像名
docker run -dt --name 容器名 -p [PORT]:80 (镜像名)

进入容器
sudo docker exec -it 775c7c9ee1e1(容器ID) /bin/bash

开机自启容器
docker update --restart=always 容器ID

文件复制到容器里面
docker cp 本地的路径 容器ID : 容器里的路径

本地搭建sqli-labs网站;

第一步:
1.你需要一个装有docker环境的系统
2.打开一个网站 https://hub.docker.com/
3.搜索sqli-labs

image.png
4.随便打开一个
image.png
5.pull下镜像
6.启动一个容器
第二步:
1.等待下载完成
image.png
2.docker run -dt --name sqli-lab -p 8001:80 acgpiano/sqli-labs:latest
把容器的80端口映射到本机的8001端口
image.png

本地搭建upload-labs网站;

1.github 源码地址:
https://github.com/c0ny1/upload-labs
环境地址:https://hub.docker.com/r/gfattf1/upload-labs/
2.docker pull gfattf1/upload-labs
3.docker run -dt --name upload-labs -p 8002:80 gfattf1/upload-labs
4.访问地址
0.0.0.0:8002/upload-labs/

image.png

相关文章

  • 漫长的Docker路

    Docker已经支持非常多的Linux平台,包括了Ubuntu和RHEL,还支持Debian、CentOS、Fed...

  • 漫长的路

    我要走一条漫长的路 有时候 会有人在路上等我 他执拗地要陪我走上一小段 有时候 他会问我是否愿意 让他陪我走完这条...

  • 漫长的路

    太久没有出去,在家差点被闷坏,心情就如杂乱的头发,肆意生长,毫无规律,时而开心,突然低落,这次疫情不知何时才能结束...

  • 漫长的路

    上班的路还是那么长,上班的时间长了,原来50分钟的路程现在随着天气变冷变成一个多小时,那个赛博特转弯处的红绿灯最长...

  • 路漫长

    今天一个人去健身房,水杯拉在更衣室,跑步机,电视机不知道开了,练完才发现水没喝完。平时走的路也特别长,过红绿灯过好...

  • 路漫长

    无法用语言来诉说现在的心情,更无法描述此刻的眼泪。 当花飘落地时候,时间却没有停留,当大地再次复苏,我还是以往无常...

  • 路漫长

    路漫长 院里荒凉,唯有麻雀闹。 残柳黄花,当年迎风笑。 昆虫儿飞来逛去,绵皮袄复披闺中皆欢笑。 ...

  • 成长的路,漫长的路

    今天画得倒是很快,但是看书找关键词的过程实在是太漫长了,非常的累,但我想,这就是提升专注力的表现。只有专注,才有收...

  • 未来的路漫长

    雨水轻轻落在脸庞 没人看得清我的模样 远处的霓虹灯寂寞凄凉 有谁知道未来的路在何方 有人说只要努力就会成功在望 可...

  • 回家的路,漫长

    三年前的我还是一只纯纯的文艺青年,圆咕溜丢的脑瓜子里全是诗经楚辞、唐诗宋词、乡愁和落雨、群山与远方,自以为把...

网友评论

      本文标题:漫长的Docker路

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