Centos docker使用日记

712 阅读2分钟

Yum

简介

Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。

常用命令

显示已经安装的软件包 yum list installed

查找可以安装的软件包 yum list 软件名

安装软件包 yum install 软件名

卸载软件包 yum remove 软件名

在安装软件的时候,会有中断,让用户选择是否要继续,我们可以用 -y 来应答所有的 yes yum -y install 软件名

安装最新版node

curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash

yum -y install nodejs

安装

yum -y install docker

查看docker版本

docker version

启动docker服务

service docker start

列出docker的image

docker image ls

运行node项目

先下载源码

git clone https://github.com/ruanyf/koa-demos.git
cd koa-demos

在项目的根目录下,新建一个文本文件.dockerignore

.git
node_modules
npm-debug.log

上面代码表示,这三个路径要排除,不要打包进入 image 文件。如果你没有路径要排除,这个文件可以不新建。 然后,在项目的根目录下,新建一个文本文件 Dockerfile

FROM node:8.4
COPY . /app
WORKDIR /app
RUN npm install --registry=https://registry.npm.taobao.org
EXPOSE 3000

上面代码一共五行,含义如下。

FROM node:8.4:该 image 文件继承官方的 node image,冒号表示标签,这里标签是8.4,即8.4版本的 node。
COPY . /app:将当前目录下的所有文件(除了.dockerignore排除的路径),都拷贝进入 image 文件的/app目录。
WORKDIR /app:指定接下来的工作路径为/app。
RUN npm install:在/app目录下,运行npm install命令安装依赖。注意,安装后所有的依赖,都将打包进入 image 文件。
EXPOSE 3000:将容器 3000 端口暴露出来, 允许外部连接这个端口。

创建 image 文件

docker image build -t koa-demo .
# 或者
$ docker image build -t koa-demo:0.0.1 .

面代码中,-t参数用来指定 image 文件的名字,后面还可以用冒号指定标签。如果不指定,默认的标签就是latest。最后的那个点表示 Dockerfile 文件所在的路径,上例是当前路径,所以是一个点。

如果运行成功,docker image ls就可以看到新生成的 image 文件koa-demo了。

如果打包时出现报错情况


container_linux.go:235: starting container process caused “process_linux.go:258: applying cgroup configuration for process caused “Cannot set property TasksAccounting, or unknown property.””
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:235: starting container process caused "process_linux.go:258: applying cgroup configuration for process caused "Cannot set property TasksAccounting, or unknown prop

执行下yum update,完毕后在次执行打包命令。