MENU

Docker安装部署

December 13, 2019 • 安全

Docker安装部署

准备工作

请尽量配置yum源为阿里云,这样yum安装快速

# 通过以下命令可查看系统版本和内核版本等信息
cat /etc/redhat-release
#-----------------------------------
CentOS Linux release 7.4.1708 (Core) 
#-----------------------------------

uname -a
#---------------------------------------------------------------------------------------------------------
Linux docker01 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
#---------------------------------------------------------------------------------------------------------

cat /etc/os-release
#------------------------------------------
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
#------------------------------------------

更新默认的yum源

centos 默认的yum源在国外

yum repolist
#------------------------------------------------------------------------------------------------------------------------------
Loaded plugins: fastestmirror
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
repo id                                                         repo name                                                          status
base/7/x86_64                                                   CentOS-7 - Base                                                    0
extras/7/x86_64                                                 CentOS-7 - Extras                                                  0
updates/7/x86_64                                                CentOS-7 - Updates                                                 0
repolist: 0
#------------------------------------------------------------------------------------------------------------------------------

# 公司内服务器域名解析总有问题,时好时不好,很烦,这里直接用hosts做解析
vi /etc/hosts
#-------------------------------------
221.206.129.236  mirrors.aliyun.com
#-------------------------------------

# 更换成aliyun yum源
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 编译CentOS-Base.repo,把带mirrors.aliyuncs.com的行都删除
vi /etc/yum.repos.d/CentOS-Base.repo

# 运行以下命令生成缓存
yum clean all
yum makecache

# 查看已启用的repo,确保centos-extras repository是启用了,安装docker时需要
yum repolist
#--------------------------------------------------------------------------
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                  repo name                                                        status
base/7/x86_64                            CentOS-7 - Base - mirrors.aliyun.com                             9,591
extras/7/x86_64                          CentOS-7 - Extras - mirrors.aliyun.com                             284
updates/7/x86_64                         CentOS-7 - Updates - mirrors.aliyun.com                          1,540
repolist: 11,415
#--------------------------------------------------------------------------

更新系统(可选)

yum update

安装Docker CE

安装

yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce
#--------------------------------------------------------------------------
.......
Installed:
  docker-ce.x86_64 0:17.09.1.ce-1.el7.centos                                                                                                                                    

Dependency Installed:
  audit-libs-python.x86_64 0:2.7.6-3.el7
  checkpolicy.x86_64 0:2.5-4.el7
  container-selinux.noarch 2:2.28-1.git85ce147.el7
  libcgroup.x86_64 0:0.41-13.el7
  libseccomp.x86_64 0:2.3.1-3.el7
  libsemanage-python.x86_64 0:2.5-8.el7
  policycoreutils-python.x86_64 0:2.5-17.1.el7
  python-IPy.noarch 0:0.75-6.el7    
  setools-libs.x86_64 0:3.3.8-1.1.el7       
......
#--------------------------------------------------------------------------

启动docker

systemctl start docker

# 查看docker的版本信息
docker version
#--------------------------------------------------------------------------
Client:
 Version:      17.09.1-ce       # 客户端版本
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:23:40 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.1-ce       # 服务端版本
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:25:03 2017
 OS/Arch:      linux/amd64
 Experimental: false
#--------------------------------------------------------------------------

# 查看网络信息
ip addr
#--------------------------------------------------------------------------
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:50:56:ab:4c:50 brd ff:ff:ff:ff:ff:ff
    inet 10.240.4.185/24 brd 10.240.4.255 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:feab:4c50/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN  # docker0 虚拟网桥
    link/ether 02:42:72:ac:05:bf brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:72ff:feac:5bf/64 scope link 
       valid_lft forever preferred_lft forever
#--------------------------------------------------------------------------

systemctl list-unit-files | grep docker
#--------------------------------------------------------------------------
docker.service                                disabled
#--------------------------------------------------------------------------

# 设置成自启服务
systemctl enable docker.service

# 查看状态
systemctl status docker
#--------------------------------------------------------------------------
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2017-12-12 17:24:31 CST; 13min ago
     Docs: https://docs.docker.com
 Main PID: 23479 (dockerd)
   CGroup: /system.slice/docker.service
           ├─23479 /usr/bin/dockerd
           └─23490 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libco...

Dec 12 17:24:29 docker01 dockerd[23479]: time="2017-12-12T17:24:29.594209004+08:00" level=info msg="libcontainerd: new containerd process, pid: 23490"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.596093094+08:00" level=warning msg="failed to rename /var/lib/docker/tmp for background deletio...chronously"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.654014669+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.654714697+08:00" level=info msg="Loading containers: start."
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.852920366+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17...IP address"
Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.996504508+08:00" level=info msg="Loading containers: done."
Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.004149257+08:00" level=info msg="Docker daemon" commit=19e2cf6 graphdriver(s)=overlay version=17.09.1-ce
Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.004282017+08:00" level=info msg="Daemon has completed initialization"
Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.015479108+08:00" level=info msg="API listen on /var/run/docker.sock"
Dec 12 17:24:31 docker01 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
#--------------------------------------------------------------------------

运行hello-world image验证docker安装是否成功

docker run hello-world
#--------------------------------------------------------------------------
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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.
    (amd64)
 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/
#-------------------------------------------------------------------------- 

升级和卸载docker

# 升级
yum -y upgrade docker-ce

# 卸载
yum remove docker-ce

# 删除Images, containers, volumes, or customized configuration files 
rm -rf /var/lib/docker

使用阿里镜像加速器

使用阿里云专属加速器加快获取Docker官方镜像,否则在国内速度会慢到你无法忍受哒。步骤如下:

  1. 免费注册一个阿里云账号
  2. 进入加速器页面
  3. 选择镜像加速器

mark

通过Nginx镜像部署静态页

拉取镜像源

可以通过

docker search nginx
docker pull nginx

来查找官方hub上的镜像,和拉取镜像,拉取完毕后可以查看本地镜像

docker images

构建包含Nginx的容器

在构建容器之前先准备好素材,也就是静态的htmlNginx相关配置,在centos上建了一个home目录,里面有applicationconfig两个文件夹,一个存放网站发布文件,一个存放配置。

demo.html只是标准的html文件,default.conf文件则是nginx部署网站的配置

server {
listen    80;
server_name    localhost;
location / {
    root /app;
    index demo.html index.htm;
}
}

listen是端口,默认80就可以了,这个是网站在服务器的内部端口,外网通过这个端口是访问不到站点的。到时候容器运行时会做端口映射,重新指定端口供外网访问。server_name后面写localhost表示本地即可,location里面的root表示nginx部署站点所在文件路径,由于我们是容器部署,所以这里的/app路径其实是容器的目录。index是启动项,我们设置为我们的demo.html即可。

  网站文件和nginx配置准备好了以后我们就可以开始通过nginx镜像来构建我们的容器了,容器都有自己的目录的,但是为了方便操作以及做持久化,我们会通过docker运行容器的-v参数来做目录挂载。

运行容器的命令

docker run -d -p 8086:80 -v /home/application/demo:/app -v /home/config:/etc/nginx/conf.d --name demo nginx

然后通过

docker ps 

来查看运行中的容器

故障解决

运行之后我出现了docker ps后没有我运行的容器的情况,通过docker ps -all可以查看到已经关闭了的容器,然后通过docker logs CONTAINERID来查看运行的日志判断错误

Dockerfile定制镜像

准备镜像

因为不熟悉Ubuntu,所以还是用回我的centos

docker pull centos

下载jkd、tomcat安装包

上传到/usr/local/softsoft文件夹自己mkdir一下

jdk1.8下载

tomcat8下载

解压,创建dockerfile文件

tar -zxvf apache-tomcat-xxxxx.tar.gz #解压
tar -zxvf jdk-xxx-linux-xxx.tar.gz #解压
rm -rf apache-tomcat-xxxxx.tar.gz #删除安装包
rm -rf jdk-8u171-linux-x64.tar.gz #删除安装包
touch Dockerfile #创建文件

操作完成后,目录内容如下

mark

编写dockerfile文件

#指定操作的镜像
FROM centos
# 维护者信息
MAINTAINER biubiu
#执行命令:创建目录
RUN mkdir -p /usr/local/soft
#将jdk1.8.0_171添加到镜像centos的/usr/local/soft/目录下,并命名为jdk
ADD jdk1.8.0_171 /usr/local/soft/jdk
#将apache-tomcat-8.5.31添加到镜像centos的/usr/local/soft/目录下,并命名为tomcat
ADD apache-tomcat-8.5.31 /usr/local/soft/tomcat
#添加环境变量
ENV JAVA_HOME /usr/local/soft/jdk
ENV CATALINA_HOME /usr/local/soft/tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
#暴露8080端口
EXPOSE 8080
#启动时运行tomcat
CMD ["/usr/local/soft/tomcat/bin/catalina.sh","run"]
  • 其中路径根据自己本机实际操作修改
  • FROM : 指定基础镜像,并且必须是第一条指令
  • MAINTAINER : 指定作者
  • RUN : 运行指定的命令
  • ADD : 复制命令,把文件复制到镜像中。
  • ENV : 设置环境变量
  • EXPOSE : 功能为暴漏容器运行时的监听端口给外部
  • CMD : 指定容器启动时运行的命令

构建docker镜像

docker build -t repostory/centos_tomcat .
  • -t 设置tag名称, 命名规则registry/image:tag(若不添加版本号,默认latest)
  • . 表示使用当前目录下的Dockerfile文件(注意语句后面有一个点)

启动镜像,访问

docker run -d -p 8080:8080 --nem Icentos repostory/centos_tomcat
  • -d 后台运行
  • -p 端口映射 宿主机port:容器port
  • --name 指定容器运行名称

mark

整合自

Docker使用Dockerfile创建Centos(tomcat+jdk)镜像

docker实战之通过nginx镜像来部署静态页

完结

本文作者:Losir
本文链接:Docker安装部署 - https://ionssource.cn/archives/47/
版权声明:如无特别声明,本文即为原创文章,仅代表个人观点,版权归 Losir 所有,未经允许不得转载!

Last Modified: February 15, 2020
开往-友链接力