Docker

CentOS7下安装Docker(一)

一、配置yum源

一般来说,安装好centos7后,通过命令查看已有yum源会看到7个.repo;

[root@rainbowzj ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo   
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  

而要安装Docker,则需要配置额外yum源,命令如下;(此为博主安装Elasticsearch配置的源,也可以添加其它yum源如阿里源等)

[root@rainbowzj ~]# vi /etc/yum.repos.d/elasticsearch.repo
#创建的为新文件,写入如下语句
[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

[root@rainbowzj ~]# yum insatll -y epel-release

再次查看发现yum源数量如下:
[root@rainbowzj ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo   epel.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  elasticsearch.repo  epel-testing.repo

二、安装docker

[root@rainbowzj ~]# yum -y install docker

[root@rainbowzj ~]# systemctl start docker
[root@rainbowzj ~]# systemctl enable docker

三、下载及创建容器

[root@rainbowzj ~]# docker pull centos
#此过程可能很慢
[root@rainbowzj ~]#docker run centos /bin/echo "Welcome to the Docker World"
Welcome to the Docker World 

[root@rainbowzj ~]#docker run -i -t centos /bin/bash
#进入docker内,使用uname -a查看内核信息,之后使用exit退出;

[root@rainbowzj ~]#docker ps(在不关闭docker的情况下,查看docker进程信息)
CONTAINER ID        IMAGE                   COMMAND             CREATED              STATUS              PORTS                  NAMES
c21f34efc5b7        my_image/centos_httpd   "/bin/bash"         About a minute ago   Up About a minute   0.0.0.0:8081->80/tcp   angry_noyce


[root@rainbowzj ~]#docker attach c21f34efc5b7
#连接成功
[root@c21f34efc5b7 /]#
#使用exit,返回

[root@rainbowzj ~]# docker run -it -p 8081:80 my_image/centos_httpd /bin/bash
[root@c21f34efc5b7 /]# /usr/sbin/httpd   
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
httpd (pid 21) already running
[root@c21f34efc5b7 /]# echo "httpd on Docker Container" > /var/www/html/index.html 
验证是否成功:
[root@c21f34efc5b7 /]# curl localhost
httpd on Docker Container#成功


[root@rainbowzj ~]#docker kill c21f34efc5b7
[root@rainbowzj ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

四、使用Dockerfile

Dockerfile常用指令:

INSTRUCTIONDescription
FROMiIt sets the Base Image for subsequent instructions.
MAINTAINERIt sets the Author field of the generated images.
RUNIt will execute any commands when Docker image will be created.
CMDIt will execute any commands when Docker container will be executed.
ENTRYPOINTIt will execute any commands when Docker container will be executed.
LABELIt adds metadata to an image.
EXPOSEIt informs Docker that the container will listen on the specified network ports at runtime.
ENVIt sets the environment variable.
ADDIt copies new files, directories or remote file URLs.
COPYIt copies new files or directories.The differences of [ADD] are that it’s impossible to specify remore URL and also it will not extract archive files automatically.
VOLUMEIt creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers
USERIt sets the user name or UID.
WORKDIRIt sets the working directory.
指令描述
from基于什么镜像
maintainer镜像的创建者
run在后面添加创建镜像时执行的命令
cmd在后面添加创建容器时执行的命令
entrypoint在后面添加创建容器时执行的命令(无cmd的可替换性)
label给镜像添加元数据
expose让docker容器在运行时在指定的端口上映射
env环境变量的设置
add复制新文件,目录,远程URL文件
copy复制新的文件与目录,与add的区别可指定URL文件与读取存档文件
volume将本地文件夹或者其他container的文件夹挂载到container中。
user设置用户或UID
workdir设置工作目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值