Centos 安装 Gitlab


title: docker简介及环境搭建

date: 2018-01-15 16:00:03
tags: [虚拟化, docker]
categories: docker

docker是什么

Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 AUFS 类的 Union FS 等技术,对进程进行封装隔离,属于操作系统层面的虚拟化技术。由于隔离的进程独立于宿主和其它的隔离的进程,因此也称其为容器。最初实现是基于 LXC,从 0.7 以后开始去除 LXC,转而使用自行开发的 libcontainer,从 1.11 开始,则进一步演进为使用 runC 和 containerd。

Docker 在容器的基础上,进行了进一步的封装,从文件系统、网络互联到进程隔离等等,极大的简化了容器的创建和维护。使得 Docker 技术比虚拟机技术更为轻便、快捷。

传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程;而容器内的应用进程直接运行于宿主的内核,容器内没有自己的内核,而且也没有进行硬件虚拟。因此容器要比传统虚拟机更为轻便。

Docker 三个基本概念

  • 镜像(Image):Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。
  • 容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的 命名空间。因此容器可以拥有自己的 root文件系统、自己的网络配置、自己的进程空间,甚至自己的用户 ID 空间。容器内的进程是运行在一个隔离的环境里,使用起来,就好像是在一个独立于宿主的系统下操作一样。这种特性使得容器封装的应用比直接在宿主运行更加安全。
  • 仓库(Repository):镜像构建完成后,可以很容易的在当前宿主上运行,但是,如果需要在其它服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,Docker Registry 就是这样的服务。一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签>的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest作为默认标签。用 Docker 的时候,需要经常从官方获取镜像,但是由于显而易见的网络原因,拉取镜像的过程非常耗时,严重影响使用 Docker 的体验。因此 DaoCloud等加速器服务商 推出了加速器工具解决这个难题,通过智能路由和缓存机制,极大提升了国内网络访问 Docker Hub 的速度,目前已经拥有了广泛的用户群体,并得到了 Docker 官方的大力推荐。如果您是在国内的网络环境使用 Docker,那么 Docker 加速器一定能帮助到您。docker常用仓库:https://hub.docker.com/explore/

docker daocloud加速器:

  • linux:curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://311e425f.m.daocloud.io,该脚本可以将 --registry-mirror 加入到你的 Docker 配置文件 /etc/docker/daemon.json 中。适用于 Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1,其他版本可能有细微不同
  • Mac:右键点击桌面顶栏的 docker 图标,选择 Preferences ,在 Daemon 标签(Docker 17.03 之前版本为 Advanced 标签)下的 Registry mirrors 列表中加入下面的镜像地址:http://311e425f.m.daocloud.io,点击 Apply & Restart 按钮使设置生效。
  • Windows: 在桌面右下角状态栏中右键 docker 图标,修改在 Docker Daemon 标签页中的 json ,把下面的地址:http://311e425f.m.daocloud.io,加到"registry-mirrors"的数组里。点击 Apply 。

环境搭建

mac下安装docker

官方mac的安装步骤

下载docker.dmg文件,然后点击,一步步操作

检测Docker Engine, Docker Compose, 和Docker Machine的版本:

docker version
Client:
 Version:      17.03.1-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Tue Mar 28 00:40:02 2017
 OS/Arch:      darwin/amd64

Server:
 Version:      17.03.1-ce
 API version:  1.27 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Fri Mar 24 00:00:50 2017
 OS/Arch:      linux/amd64
 Experimental: true

docker-compose --version
docker-compose version 1.11.2, build dfed245

docker-machine --version
docker-machine version 0.10.0, build 76ed2a6

运行官网提供的二个简单列子

拉取hello-world镜像

docker pull hello-world

查看hello-world镜像信息:

docker images hello-world
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              1815c82652c0        5 days ago          1.84 kB

根据镜像生成对应容器

docker run hello-world

ps不加参数只会把当前运行的Community打印出来,查看当前所有的Community,加上-a参数。

docker ps -a

当然也可以根据镜像生成一个具体名称的镜像,先删除当前容器

docker rm 60

当然如果当前容器正在运行,要删除这个容器,则使用命令

docker rm -f 60

使用镜像生成具体名称的容器:

docker run --name miaozhihao hello-world

于更多的docker run的命令可以使用来查看

docker run --help

第二个examples,启动docker的web服务

docker pull nginx
docker run -d -p 80:80 --name webserver nginx

-p参数是使用宿主机的80映射容器的80端口

curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

交互式终端方式进入 webserver容器,

docker exec -it webserver bash

修改nginx的显示页面:

root@41b6804c716e:/# echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
root@41b6804c716e:/# exit
exit

修改了容器的文件,也就是改动了容器的存储层。我们可以通过 docker diff

docker diff webserver
C /root
A /root/.bash_history
C /run
A /run/nginx.pid
C /usr/share/nginx/html/index.html
C /var/cache/nginx
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp

使用docker commit生成镜像

docker commit --author "zhihao.miao <1026145686@qq.com>" --message "修改了默认网页" webserver nginx:v2
sha256:0a34c054b8a826d85dddf4d1dbdd3028ab890feff4c8a0844e9b98dd146c2e07

--autho 指定作者 --message表示容器的一些信息

查看当前nginx镜像:

docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v2                  0a34c054b8a8        11 seconds ago      109 MB
nginx               latest              958a7ae9e569        4 weeks ago         109 MB

查看当前所有的容器,包括运行的和停止的

docker ps -a
\CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS                    PORTS                NAMES
41b6804c716e        nginx                                 "nginx -g 'daemon ..."   3 minutes ago       Up 3 minutes              0.0.0.0:80->80/tcp   webserver
85612b405cda        miaozhihao001dockerhub/commit_test1   "nginx -g 'daemon ..."   20 hours ago        Exited (0) 3 hours ago                         nginx_web
9ef1fb35d7aa        ubuntu:14.04                          "/bin/bash"              20 hours ago        Exited (0) 20 hours ago                        commit_test
bf8320b9e445        ubuntu:14.04                          "/bin/bash"              24 hours ago        Exited (0) 24 hours ago                        sharp_curie
9f9767eb8aaf        hello-world                           "/hello"                 10 days ago         Exited (0) 10 days ago                         miaozhihao

启动新的容器

docker run --name newwebserver -d -p 80:80 nginx:v2
3619b34ed347cf1ae2ee3ab32c419140871f3084b9a1325ab5d8c6155d43bf06
➜ curl localhost:80
<h1>Hello, Docker!</h1>

创建自己的docker镜像

编辑Dockerfile文件,填入以下内容:

FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

运行以下命令创建名为docker-whale的镜像:

docker build -t docker-whale .
  • -t: 给tag命令
  • .: Dockerfile文件所在路径
docker tag push pull
docker tag 8e15421920b1 xulingfeng/docker-whale:latest

上传操作

docker push xulingfeng/docker-whale

下载操作

docker pull centos
交互式的操作
docker run -t -i ubuntu /bin/bash    
  • t 分配了一个终端在新的容器中
  • -i 允许你和容器进行交互操作
  • /bin/bash 启动容器中的Bash shell
docker的守护状态,也就是后台运行
docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
  • docker run 运行容器
  • -d 让容器在后台运行
  • ubuntu 你希望运行容器的镜像
查看docker容器运行日志
docker logs -f 容器名
  • -f 类似与 tail -f
使用docker运行web应用
docker run -d -P training/webapp python app.py
  • -d:代表后台运行该容器
  • -P:映射容器中的web应用端口号到你的主机上32768-61000中的某一个端口。这样你可以访问该容器中的web应用
  • training/webapp: 一个已经构建好的镜像,包含一个简单的python flask框架web应用
  • python app.py:这个命令用来启动容器中的web

成功运行以上命令后,运行:
docker ps

查看到容器的5000端口号映射到了本地的32768,浏览器访问http://127.0.0.1:32768 看到helloworld 成功提示

自定义主机端口号
docker run -d -p 80:5000 training/webapp python app.py
  • -p 80:5000 将本机的80端口绑定容器内的5000端口,本地直接访问 http://127.0.0.1 即可
查看容器的进程
docker top 容器名
检查容器的状态信息
docker inspect 容器名
镜像搜索
docker search 内容
创建一个给pycharm开发用的镜像,包含python3,Django, Flask, requests, PyMySQL, ldap3, jira,celery, simplejson

centos镜像,分解步骤如下

  • 首先添加额外源:yum install -y epel-release
  • 安装编译环境:yum install -y gcc automake autoconf libtool make gcc-c++
  • 安装wget命令:yum install -y wget
  • 安装openssl-devel python的pip命令依赖:yum install -y openssl-devel
  • 下载python3.5.2最新包:wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz -P /software/
  • 解压python3.5.2压缩包并编译安装:tar -zxvf /software/Python-3.5.2.tgz -C /software/./configure && make -j2&& make install -j2
  • 更新pip:pip install --upgrade pippip install --upgrade setuptools
  • 安装所需的第三方包:pip install Django Flask requests PyMySQL ldap3 jira celery simplejson
通过Dockerfile构建镜像

Dockerfile:

FROM centos:latest
RUN yum install -y epel-release
RUN yum install -y gcc automake autoconf libtool make gcc-c++
RUN yum install -y wget
RUN yum install -y openssl-devel
RUN wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz -P /software/
RUN tar -zxvf /software/Python-3.5.2.tgz -C /software/
RUN cd /software/Python-3.5.2/ && ./configure python3 && make -j2&& make install -j2
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install Django Flask requests PyMySQL ldap3 jira celery simplejson

在Dockerfile目录中执行:docker build -t 名字:版本 .

兼容docker for mac 和 pycharm
brew install socat
socat TCP-LISTEN:2376,reuseaddr,fork,bind=127.0.0.1 UNIX-CLIENT:/var/run/docker.sock

一些docker命令总结

  • docker images :查看当前宿主机的所有镜像。
  • docker images ubuntu:根据仓库名列出镜像
  • docker images ubuntu:14.04:指定仓库名和标签
  • docker build -t webservice .:表示使用当前目录下的DockerFile来生成镜像,-t参数的值表示镜像的tagname,如果DockerFile在当前路径下则使用.,如果不在当前路径下则使用相对路径。
  • docker ps -a: 没有-a参数表示显示当前宿主机的正在运行的容器,加上-a表示显示当前宿主机所有的容器,包括已经退出的容器。
  • docker run -d -p 2222:22 --name base centos:7.1
    表示根据指定的镜像后台运行容器,容器的名字是base(--name就是指定容器的名字),centos:7.1表示镜像的名字,-p参数表示当前宿主机的2222端口对应容器的22端口。-d参数表示(Run container in background and print container ID)
  • docker exec -it base /bin/bash
    以交互式命令进入base容器并且执行/bin/bash命令
  • docker rmi webservice:删除webservice镜像
  • docker rm base: 删除base容器,如果base正在运行,则可以使用docker rm -f base进行强行删除
  • docker start 启动容器
  • docker stop 停止容器
相关推荐
笑笑布丁2 分钟前
一些python实践
linux·python
椰椰椰耶1 小时前
【Linux】常用的命令
linux·运维·服务器
xiaojiesec2 小时前
第145天:内网安全-Linux权限维持&Rootkit后门&Strace监控&Alias别名&Cron定时任务
linux·运维·安全
阿洵Rain4 小时前
【Linux】环境变量
android·linux·javascript
zealous_zzx5 小时前
深度解析Linux系统和Unix系统的基本概念及优缺点和原理
linux·unix
丢爸6 小时前
网络学习-eNSP配置NAT
linux·网络·学习
沐风ya6 小时前
NAT技术介绍+缺陷(内网穿透+工具),NAPT(介绍,替换过程,原理,NAT转换表)
linux·服务器·网络
别挡8 小时前
CentOS Stream 8中安装和使用 Docker
linux·docker·centos
人工智障调包侠8 小时前
Linux 目录介绍
linux·运维·服务器
愤怒的代码9 小时前
Centos使用阿里云镜像安装docker
linux·docker·centos