栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux安装Docker容器环境centos中安装docker-compose容器编排dockerfile文件构建镜像(史上最详细的docker)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux安装Docker容器环境centos中安装docker-compose容器编排dockerfile文件构建镜像(史上最详细的docker)

在Linux系统下安装docker容器环境 1.容器介绍 1.1 镜像(Image)
    镜像可以用来创建Docker 容器,Docker 提供了一个很简单的机制来创建镜像或者更新现有的镜像,
用户甚至可以直接从其他人那里下载一个已经做好的镜像来直接使用。
1.2 容器(Container)
   容器是从镜像创建的运行实例,它可以被启动、开始、停止、删除。每个容器都是相互隔离的、保证安全的平台,
可以把容器看做是一个简易版的 Linux 环境(包括root用户权限、进程空间、用户空间和网络空间等)
和运行在其中的应用程序。
1.3 仓库(Repository)
1.仓库是集中存放镜像文件的场所。有时候会把仓库和仓库注册服务器(Registry)混为一谈,并不严格区分。实际上,仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(tag)。
2.仓库分为公开仓库(Public)和私有仓库(Private)两种形式,用户也可以在本地网络内创建一个私有仓库
3.当用户创建了自己的镜像之后就可以使用push 命令将它上传到公有或者私有仓库,这样下次在另外一台机器上使用这个镜像时候,只需要从仓库上 pull 下来就可以了。
2.容器安装(docker) 2.1 基础环境:

2.1.1 本次实验是在centos7环境下且能访问互联网:

[root@server-3 ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@server-3 ~]# ping www.qq.com
PING ins-r23tsuuf.ias.tencent-cloud.net (183.194.238.117) 56(84) bytes of data.
64 bytes from . (183.194.238.117): icmp_seq=1 ttl=128 time=16.3 ms
64 bytes from . (183.194.238.117): icmp_seq=2 ttl=128 time=16.7 ms
64 bytes from . (183.194.238.117): icmp_seq=3 ttl=128 time=17.3 ms


2.1.2 关闭防火墙+selinux
[root@server-3 ~]# systemctl stop firewalld
[root@server-3 ~]# systemctl disable firewalld
[root@server-3 ~]# setenforce 0
setenforce: SELinux is disabled




2.1.3 配置阿里云yum源和docker所需源

1.使用wget下载阿里镜像的前提是 需要虚拟机 能够访问 互联网
2.若无法使用wget工具,则可使用 curl 对其进行下载
3. wget 和 curl 使用一个即可

[root@server-3 ~]# rm -rf /etc/yum.repos.d/*

[root@server-3 ~]# wget -O /etc/yum.repos.d/centos7.repo  http://mirrors.aliyun.com/repo/Centos-7.repo 
[root@server-3 ~]# curl -o /etc/yum.repos.d/centos7.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@server-3 ~]# yum install -y yum-utils device-mapper-persistent-data 
[root@server-3 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@server-3 ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo



2.2 安装最新版本docker:
[root@server-3 ~]# yum install -y lvm2 docker-ce


启动docker容器
[root@server-3 ~]# systemctl start docker
[root@server-3 ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

测试是否能够使用:
[root@server-3 ~]# docker -v
Docker version 20.10.8, build 3967b7d
[root@server-3 ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE


2.3安装指定版本的Docker-CE:

注意:
安装指定版本的docker之前需先将docker卸载干净

[root@server-3 ~]# systemctl stop docker
[root@server-3 ~]# rpm -qa |grep docker
docker-ce-cli-20.10.8-3.el7.x86_64
docker-ce-rootless-extras-20.10.8-3.el7.x86_64
docker-scan-plugin-0.8.0-3.el7.x86_64
docker-ce-20.10.8-3.el7.x86_64
[root@server-3 ~]# rpm -e docker-ce-cli-20.10.8-3.el7.x86_64
error: Failed dependencies:
        docker-ce-cli is needed by (installed) docker-scan-plugin-0:0.8.0-3.el7.x86_64
        docker-ce-cli is needed by (installed) docker-ce-3:20.10.8-3.el7.x86_64
[root@server-3 ~]# rpm -e docker-ce-cli-20.10.8-3.el7.x86_64 --nodeps
[root@server-3 ~]# rpm -e docker-ce-rootless-extras-20.10.8-3.el7.x86_64 --nodeps
[root@server-3 ~]# rpm -e docker-scan-plugin-0.8.0-3.el7.x86_64 --nodeps
[root@server-3 ~]# rpm -e docker-ce-20.10.8-3.el7.x86_64 --nodeps
[root@server-3 ~]# rpm -qa |grep docker

用rpm工具搜索已安装文件中的docker,搜出无返回时,即是卸载干净

Step 1:查找Docker-CE的版本:

[root@server-3 ~]# yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
docker-ce.x86_64         3:20.10.8-3.el7                       docker-ce-test   
docker-ce.x86_64         3:20.10.8-3.el7                       docker-ce-stable 
docker-ce.x86_64         3:20.10.8-3.el7                       @docker-ce-stable
.....
......
......
docker-ce.x86_64         18.01.0.ce-1.el7.centos               docker-ce-test   
docker-ce.x86_64         18.01.0.ce-0.1.rc1.el7.centos         docker-ce-test   
docker-ce.x86_64         17.12.1.ce-1.el7.centos               docker-ce-test   
docker-ce.x86_64         17.12.1.ce-1.el7.centos               docker-ce-stable 
docker-ce.x86_64         17.12.1.ce-0.2.rc2.el7.centos         docker-ce-test   
docker-ce.x86_64         17.03.2.ce-0.1.rc1.el7.centos         docker-ce-test   
docker-ce.x86_64         17.03.1.ce-1.el7.centos               docker-ce-stable 
docker-ce.x86_64         17.03.1.ce-0.1.rc1.el7.centos         docker-ce-test   
docker-ce.x86_64         17.03.0.ce-1.el7.centos               docker-ce-stable 
Available Packages

Step2: 安装指定版本的docker-ce (例如上面的17.12.1.ce-1.el7.centos)
安装指定版本的 docker-ce:
[root@server-3 ~]# yum install -y docker-ce-17.12.1.ce-1.el7.centos

启动+设置开机自启:
[root@server-3 ~]# systemctl start docker
[root@server-3 ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

查看版本:
[root@server-3 ~]# docker -v
Docker version 17.12.1-ce, build 7390fc6


2.4 配置镜像加速:
[root@server-3 ~]# cd /etc/docker/
[root@server-3 docker]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://s0lkb5s0.mirror.aliyuncs.com"]
}
EOF



查看是否配置成功:
[root@server-3 docker]# cat daemon.json 
{
  "registry-mirrors": ["https://s0lkb5s0.mirror.aliyuncs.com"]
}



重启docker服务:
[root@server-3 docker]# systemctl daemon-reload
[root@server-3 docker]# systemctl restart docker


3.容器使用(docker): 3.1拉取镜像

因为配置了阿里云的镜像加速,是可以直接从网上拉取镜像到本地的

[root@server-3 ~]# docker pull nginx:latest
latest: Pulling from library/nginx
07aded7c29c6: Pull complete 
bbe0b7acc89c: Pull complete 
44ac32b0bba8: Pull complete 
91d6e3e593db: Pull complete 
8700267f2376: Pull complete 
4ce73aa6e9b0: Pull complete 
Digest: sha256:969419c0b7b0a5f40a4d666ad227360de5874930a2b228a7c11e15dedbc6e092
Status: Downloaded newer image for nginx:latest

[root@server-3 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f8f4ffc8092c        18 hours ago        133MB
3.2使用镜像运行nginx:
[root@server-3 ~]# docker run -dit --name nginx-test -p 80:80 nginx:latest
a69c7e1270ad8c5ccf8682970875a2d21cba66ee1384b9c41735d699b438c96b
[root@server-3 ~]# docker ps 
ConTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
a69c7e1270ad        nginx:latest        "/docker-entrypoint.…"   3 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp   nginx-test
3.3访问nginx首页:
[root@server-3 ~]# curl 127.0.0.1



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

4.容器编排(docker-compose):
4.1 容器编排介绍:
   docker-compose是基于docker的编排工具,使容器的操作能够批量的,可视的执行,是一个管理多个容器的工具 ,
比如可以解决容器之间的依赖关系,当在宿主机启动较多的容器时候,如果都是手动操作会觉得比较麻烦而且容器出错,
这个时候使用docker的单机编排工具 docker-compose就会使得容器在运行使用的时候变得简单直观明了。


4.2容器编排下载
[root@server-3 ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   423  100   423    0     0    505      0 --:--:-- --:--:-- --:--:--   505
100 16.2M  100 16.2M    0     0  6903k      0  0:00:02  0:00:02 --:--:-- 11.5M

[root@server-3 ~]# chmod +x /usr/local/bin/docker-compose
[root@server-3 ~]# docker-compose -v
docker-compose version 1.25.0, build 0a186604


4.3容器编排使用

本实验是测试实验,只运行举例两个镜像nginx和mysql的使用

4.3.1 准备两个镜像:
[root@server-3 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f8f4ffc8092c        19 hours ago        133MB
mysql               5.6                 8d06d2d16232        19 hours ago        303MB
4.3.2 编写yaml文件:

编写启动nginx和mysql的docker-compose文件

[root@server-3 ~]# vim docker-compose.yaml 
version: '3.3'
services:
  mysql:
    image: mysql:5.6           #指定使用镜像 
    container_name: mysql       #指定启动时容器名称 --name
    environment:    #进行赋值操作 -e
      - MYSQL_ROOT_PASSWORD=000000
    volumes:                 #指定容器中哪个路径与宿主机中的路径进行数据卷映射 -v
      - /opt/mysql/:/root/mysql/
    ports:                    #指定容器端口与宿主机端口映射 -p
      - 3306:3306
  nginx:
    image: nginx:latest
    container_name: nginx
    volumes:
      - /opt/nginx/:/root/nginx/
    ports:
      - 80:80
4.3.3 启动docker-compose并查看容器状态:
[root@server-3 ~]# docker-compose up -d
Creating network "root_default" with the default driver
Creating nginx ... done
Creating mysql ... done

[root@server-3 ~]# docker-compose ps
Name               Command               State           Ports         
-----------------------------------------------------------------------
mysql   docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp
nginx   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:80->80/tcp 

[root@server-3 ~]# docker ps
ConTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
c6d1ea945341        nginx:latest        "/docker-entrypoint.…"   5 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp       nginx
a5aff8b2e4eb        mysql:5.6           "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:3306->3306/tcp   mysql
4.4 验证和访问docker-compose: 方法1:

此方法需关闭虚拟机的防火墙和selinux安全模式,具体参考本文档 2.1 基础环境
nginx:
浏览器访问: 虚拟机ip:80
我的ip是192.168.66.191
故我访问的是: 192.168.66.191:80




验证mysql:
此处我是用windows下的Navicat:
数据库账户:root
数据库密码:000000




方法2:

nginx:

[root@server-3 ~]# curl 127.0.0.1:80



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.


可见 curl 127.0.0.1:80 时上述代码中会出现welcome to nignx !字样,即是成功!!



mysql:
在虚拟机中进入容器里面,登录mysql数据库:

[root@server-3 ~]# docker exec -it mysql bash
root@a5aff8b2e4eb:/# mysql -uroot -p000000
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> 

可用 root 用户和 000000 密码 登录进mysql数据库,即为成功!!

5.Dockerfile文件: 5.1 dockerfile文件格式:
dockerfile格式:
FROM	 	指定所要使用的镜像 		
MAINTAINER 	 注释,可有可无		
RUN 		后可接命令					
WORKDIR		启动后进入到某路径中			
COPY 		复制		
ADD			复制,复制tar包等会直接解压
VOLUME		指定容器中要挂载的目录,对其进行开放
ENV			定义一个变量,方便后续对其进行引用	
ENTRYPOINT	相当于run和cmd 有基本的命令权限				
CMD			接命令,只可有一个,作为第一个命令					
	
			
entrypoint和cmd相结合
例:
ENTRYPOINT ["ls"]  此处写固定命令
CMD ["/opt/test"]  此处写路径
所得出的命令即为:ls /opt/test/
5.2 使用dockerfile构建镜像 5.2.1准备

1.拉取一个centos7.5的镜像
2.使用wget下载tomcat的源码包
3.复制yum源

[root@server-3 ~]# docker pull centos:7.5.1804
7.5.1804: Pulling from library/centos
5ad559c5ae16: Pull complete 
Digest: sha256:7a45e4a1efbaafc1d9aa89925b6fdb33288a96d35ea0581412316e2f0ad3720a
Status: Downloaded newer image for centos:7.5.1804
[root@server-3 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f8f4ffc8092c        25 hours ago        133MB
mysql               5.6                 8d06d2d16232        25 hours ago        303MB
centos              7.5.1804            cf49811e3cdb        2 years ago         200MB

[root@server-3 ~]# yum install -y wget
[root@server-3 ~]# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.53/bin/apache-tomcat-9.0.53.tar.gz


[root@server-3 ~]# cp /etc/yum.repos.d/centos7.repo ./
5.2.2 Dockerfile 编写
[root@server-3 ~]# vim Dockerfile 
FROM centos:7.5.1804				
MAINTAINER CcccrisTom
RUN rm -rf /etc/yum.repos.d/*
COPY ./centos7.repo /etc/yum.repos.d/
RUN yum install -y java
ADD ./apache-tomcat-9.0.53.tar.gz /usr/local/
WORKDIR /usr/local/apache-tomcat-9.0.53/
CMD ["sh","-c","bin/startup.sh"]

5.2.3 构建镜像查看
[root@server-3 ~]# docker build -t tomcat:v1 .
[root@server-3 ~]# docker images |grep tomcat
tomcat              v1                  a08cfe2baebd        16 seconds ago      553MB


5.3启动dockerfile构建的镜像
[root@server-3 ~]# docker run -it -p 8080:8080 tomcat:v1 bash
[root@18b83bffda6b apache-tomcat-9.0.53]# ps -ef |grep tomcat
root         14      1  0 11:04 pts/0    00:00:00 grep --color=auto tomcat
[root@18b83bffda6b apache-tomcat-9.0.53]# bin/startup.sh 
Using CATALINA_base:   /usr/local/apache-tomcat-9.0.53
Using CATALINA_HOME:   /usr/local/apache-tomcat-9.0.53
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.53/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/apache-tomcat-9.0.53/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.53/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

[root@18b83bffda6b apache-tomcat-9.0.53]# ps -ef |grep tomcat
root         25      1 11 11:04 pts/0    00:00:02 /usr/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-9.0.53/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/apache-tomcat-9.0.53/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.53/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-9.0.53 -Dcatalina.home=/usr/local/apache-tomcat-9.0.53 -Djava.io.tmpdir=/usr/local/apache-tomcat-9.0.53/temp org.apache.catalina.startup.Bootstrap start
root         45      1  0 11:05 pts/0    00:00:00 grep --color=auto tomcat


5.4 验证是否成功:

浏览器访问:192.168.66.191:8080




至此 :
| docker在linux环境下的安装和使用,
| docker-compose的安装和使用,
| dockcerfile文件格式以及如何构建镜像,

都详细介绍完毕。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/277296.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号