2.2 Docker安装和配置

CentOs 7 安装 Docker-ce 社区版本

安装docker依赖包

yum install -y yum-utils device-mapper-persistent-data lvm2

添加Docker-ce 软件源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

关闭Docker-ce 的边缘版本和测试版本

yum-config-manager --enable docker-ce-edge
yum-config-manager --enable docker-ce-test

更新yum源

yum makecache fast

安装Docker-ce(第一种安装方式)

yum install docker-ce -y

查看是否安装成功

docker version

Client:
Version:           18.06.1-ce-rc1
API version:       1.38
Go version:        go1.10.3
Git commit:        0928140
Built:             Wed Aug  8 01:35:58 2018
OS/Arch:           linux/amd64
Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

查看可以提供的安装版本(第二种安装方式)

yum list docker-ce --showduplicates|sort -r  

安装指定版本的Docker-ce

yum install docker-ce-17.09.0.ce -y

使用Docker 脚本安装Docker(第三种方式)

下载Docker 安装脚本

curl -fsSL get.docker.com -o get-docker.sh

执行 Docker 脚本使用 aliyun 镜像

sh get-docker.sh --mirror Aliyun

更换Docker 镜像仓库为国内镜像源

注册Daocloud账号,并登陆Daocloud

https://www.daocloud.io/mirror#accelerator-doc

执行更换Daocloud 国内镜像源命令

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://e674da1e.m.daocloud.io

重启Docker 使国内源生效

systemctl restart docker 

添加Docker 为随机启动

systemctl enable docker 

Docker 镜像的基础操作

从github 拉取 镜像
命令格式:

docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]

Docker 镜像仓库地址:地址的格式一般是 <域名/IP>[:端口号]。默认地址是 Docker Hub。
仓库名:两段式名称,即 <用户名>/<软件名>。对于 Docker Hub,如果不给出用户名,则默认为 library,也就是官方镜像。

实例:

 docker pull ubuntu

Using default tag: latest
latest: Pulling from library/ubuntu
c64513b74145: Pull complete 
01b8b12bad90: Pull complete 
c5d85cf7a05f: Pull complete 
b6b268720157: Pull complete 
e12192999ff1: Pull complete 
Digest: sha256:8c3cced1211d1a566088c0af049cc8cd6f33f5b275e62e6285ca6a13e66a98f0
Status: Downloaded newer image for ubuntu:latest

列出本地已下载的镜像

docker image

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              735f80812f90        2 weeks ago         83.5MB
hello-world         latest              2cb0d9787c4d        4 weeks ago         1.85kB

删除本地镜像

docker image rm -f 2cb0d9787c4d

根据镜像 ID、镜像名、摘要删除镜像

使用Dockerfile构建自己的镜像

创建一个空白的目录并进入此目录

mkdir first 
cd first/

上传定制镜像需要的文件到此目录下

[root@MiWiFi-R3-srv ~]# cd first/
[root@MiWiFi-R3-srv first]# ll
总用量 12
-rw-r--r--. 1 root root 604 8月  11 15:50 Dockerfile
-rw-r--r--. 1 root root 230 8月  11 14:57 index.js
-rw-r--r--. 1 root root 228 8月  11 14:58 package.json
[root@MiWiFi-R3-srv first]# 

编辑Dockerfile文件

# 基础镜像来自于 hub.docker.com
FROM centos

#镜像维护信息 https://xiaopangsoftware.taobao.com
MAINTAINER xiaopangruanjianxuetang

#指定工作目录
WORKDIR /app

#将项目相关文件拷贝到app下,如果目录不存在会自动创建
COPY index.js /app
COPY package.json /app

# 安装命令来自于 nodejs 官网 https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora
RUN curl --silent --location https://rpm.nodesource.com/setup_10.x | bash - && yum -y install nodejs && npm install

#暴露端口
EXPOSE 8080

#容器启动命令
CMD ["node", "/app/index.js"]

构建镜像

docker build -t xiaopang/centos-nodejs-1 .

-t 你的名字/镜像的名字

构建成功的后半部分 显示结果

...
npm notice created a lockfile as package-lock.json. You should commit this file.
added 50 packages in 9.583s
Removing intermediate container 8dc630bdf3fd
 ---> f0beb800684a
Step 7/8 : EXPOSE 8080
 ---> Running in f1e679738194
Removing intermediate container f1e679738194
 ---> 3c0c770cd1c2
Step 8/8 : CMD ["node", "/app/index.js"]
 ---> Running in 9377ccc41407
Removing intermediate container 9377ccc41407
 ---> cea31fd11519
Successfully built cea31fd11519
Successfully tagged xiaopang/centos-nodejs-1:latest

查看构建成功的images

docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
xiaopang/centos-nodejs-1   latest              cea31fd11519        7 minutes ago       343MB
centos                     latest              5182e96772bf        4 days ago          200MB
ubuntu                     latest              735f80812f90        2 weeks ago         83.5MB

运行新构建的images

docker run -p 8080:8080 -d xiaopang/centos-nodejs-1

访问8080,验证镜像是否制作成功

[root@localhost first]# curl http://127.0.0.1:8080
Hello World

恭喜你镜像制作成功了

你可能遇到的问题

docker build 报错[Warning] IPv4 forwarding is disabled. Networking will not work.

  1. 编辑00-system.conf
vim /usr/lib/sysctl.d/00-system.conf
  1. 追加以下内容
net.ipv4.ip_forward=1
  1. 重启网卡服务
systemctl restart network

猜你喜欢

转载自www.cnblogs.com/l-hh/p/12567320.html
2.2