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常用指令:
INSTRUCTION | Description |
---|---|
FROM | iIt sets the Base Image for subsequent instructions. |
MAINTAINER | It sets the Author field of the generated images. |
RUN | It will execute any commands when Docker image will be created. |
CMD | It will execute any commands when Docker container will be executed. |
ENTRYPOINT | It will execute any commands when Docker container will be executed. |
LABEL | It adds metadata to an image. |
EXPOSE | It informs Docker that the container will listen on the specified network ports at runtime. |
ENV | It sets the environment variable. |
ADD | It copies new files, directories or remote file URLs. |
COPY | It 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. |
VOLUME | It creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers |
USER | It sets the user name or UID. |
WORKDIR | It 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 | 设置工作目录 |