天天看点

Docker docker部署以及运行你的第一个容器

安装 Docker

本节我们将在centos7.4中安装 Docker。

Docker 支持几乎所有的 Linux 发行版,也支持 Mac 和 Windows。各操作系统的安装方法可以访问:https://docs.docker.com/engine/installation/

Docker 分为开源免费的 CE(Community Edition)版本和收费的 EE(Enterprise Edition)版本。下面我们将按照文档,通过以下步骤安装 Docker CE 版本。

配置docker ce版本的repo源

#扩展yum功能
[[email protected] ~]# yum install -y yum-utils  

#添加docker-ce yum源   添加软件源信息
[[email protected] ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  

#自动选择最快yum仓库源
[[email protected] ~]# yum makecache fast   

#查看当前可以安装docker-ce版本
[[email protected] ~]# yum list docker-ce --showduplicates | sort -r
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
docker-ce.x86_64            3:19.03.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
           

使用yum源安装docker 

[[email protected] ~]# yum install epel-release -y
[[email protected] ~]# ll /etc/yum.repos.d/epel*
-rw-r--r-- 1 root root 1050 Sep 17  2019 /etc/yum.repos.d/epel.repo
-rw-r--r-- 1 root root 1149 Sep 17  2019 /etc/yum.repos.d/epel-testing.repo


yum安装软件包,我这里安装CE版本不安装1.13版本
[[email protected] ~]# yum install docker* -y 1.13版本

# yum -y install docker-ce-[VERSION]    //安装指定版本的格式 ,注意3:xxx 请移除3:
[[email protected] ~]# yum install docker-ce-19.03.9-3.el7  -y

检擦docker软件包是否部署成功
[[email protected] ~]# rpm -qa | grep docker
[[email protected] ~]# yum list -C docker

启动docker引擎服务,并且查看版本
[[email protected] ~]# systemctl start docker
[[email protected] ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.9
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       9d988398e7
  Built:            Fri May 15 00:24:05 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

可以看到和mysql一样C/S架构,有客户端和服务端。通过下面socket文件来进行本地通信
[[email protected] ~]# ps -ef | grep docker
root      23788      1  0 05:25 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
           
[[email protected] ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13433               [OK] 


将镜像拉取到本地
[[email protected] ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Downloading  1.948MB/27.1MB
8d69e59170f7: Download complete 
3f9f1ec1d262: Download complete 
d1f5ff4f210d: Download complete 
1e22bfa8652e: Download complete 

或者将nginx.tar镜像包导入到你的镜像列表里面,因为拉取镜像很慢,我将tar包上传了
如果nginx镜像是通过windows上传nginx.tar,导入的nginx镜像命令:
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[[email protected] ~]# ls
anaconda-ks.cfg  nginx.tar
[[email protected] ~]# docker load <nginx.tar 
1c95c77433e8: Loading layer  72.47MB/72.47MB
002a63507c1c: Loading layer  57.31MB/57.31MB
12fdf55172df: Loading layer  3.584kB/3.584kB
Loaded image: nginx:latest
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5a3221f0137b        10 months ago       126MB
[[email protected] ~]# docker run -itd -p 80:80 nginx
314d1d01c94136a35dcb07016814437666331d8f7b441f5d0a1f66863848c413
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
314d1d01c941        nginx               "nginx -g 'daemon of??   6 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp   happy_mirzakhani


或者这种方式导入镜像也可以,但是不建议,创建容器报错
[[email protected] ~]# cat nginx.tar | docker import - nginx:v1
sha256:0dd1a59f537a0760b91897283e7e568bbc29b3971337c31a630042a1a049fd7c
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v1                  0dd1a59f537a        2 seconds ago       130MB
nginx               latest              b2ebe65d87c7        2 minutes ago       130MB

[[email protected] ~]# docker run -itd -p 81:80 nginx:v1
docker: Error response from daemon: No command specified.



Run,全新创建一台容器;
-i,interactive交互模式;
-t,tty打开登陆终端;
-d,detach后台运行、启动;
-p,publish发布端口,将宿主机80(第一个)DNAT映射至容器的80;第一个80端口是宿主机的端口,这个端口是宿主机上面需要开启的,如果宿主机上80端口被占用了是不行的,通过DNAT映射
           
Docker docker部署以及运行你的第一个容器