Docker在Linux中的安装
一、rpm离线安装docker17.12
1.下载docker安装包
2.下载9个依赖
在http://mirrors.163.com/centos/7/os/x86_64/Packages/下载8个依赖
audit-libs-python-2.7.6-3.el7.x86_64.rpm
checkpolicy-2.5-4.el7.x86_64.rpm
libcgroup-0.41-13.el7.x86_64.rpm
libseccomp-2.3.1-3.el7.x86_64.rpm
libsemanage-python-2.5-8.el7.x86_64.rpm
policycoreutils-python-2.5-17.1.el7.x86_64.rpm
python-IPy-0.75-6.el7.noarch.rpm
setools-libs-3.3.8-1.1.el7.x86_64.rpm
rpm -ivh /root/docker/*.rpm –nodeps –force
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
###二、 启动Docker引擎
sudo systemctl enable docker
sudo systemctl start docker
三、建立docker用户组
sudo groupadd docker
四、将用户加入docker组
sudo usermod -aG docker $USER
这里使用阿里云的容器镜像服务,目前公测,免费的。
1) 去阿里云官网,登录控制台,在产品与服务里面找到容器镜像服务
2)点击开通
3) 点击镜像加速器,变有了加速地址(不用镜像加速器的话,镜像都是国外的,因为墙,所有下载是龟速)
五、镜像加速器
vi /etc/systemd/system/multi-user.target.wants/docker.service
ExecStart=/usr/bin/dockerd –registry-mirror=https://….mi
rror.aliyuncs.com
sudo systemctl daemon-reload
sudo systemctl restart docker
六、检查加速器是否生效
sudo ps -ef | grep dockerd
root 5346 1 0 19:03 ? 00:00:00 /usr/bin/dockerd
–registry-mirror=https://*****.mirror.aliyuncs.com
docker -v
systemctl start docker
验证 docker 是否安装成功并在容器中执行一个测试的镜像
docker run ubuntu echo hello docker
docker run nginx
docker run -p 8080:80 -d nginx
http://192.168.116.104:8080/
[root@test3 share]# docker cp index.html
[root@test3 share]# docker commit -m ‘fun’ d0e976512485 nginx-fun
七、删除镜像
docker rmi IMAGE ID
八、查看镜像
docker images
docker ps -a
九、小结
命令 | 用途 |
---|---|
docker pull | 获取image |
docker build | 创建image |
docker images | 列出image |
docker run | 运行container |
docker ps | 列出container |
docker rm | 删除container |
docker rmi | 删除image |
docker cp | 在host 和container之间拷贝文件 |
docker commit | 保存改动为新的image |
十、Dockerfile语法
FROM alpine:latest
MAINTAINER hsiehchou
CMD echo ‘hello docker’
touch Dockerfile
++++++++++++++++++++++++++++++++++++++++
FROM ubuntu
MAINTAINER hsiehchou
RUN sed -i ‘s/archive.ubuntu.com/mirros.ustc.edu.cn/g’ /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nginx
COPY index.html /var/www/html
ENTRYPOINT [“/usr/sbin/nginx”, “-g”, “daemon off;”]前台运行
EXPOSE 80
docker build -t hsiehchou/hello-nginx .
docker run -d -p 80:80 hsiehchou/hello-nginx
十一、小结
命令 | 用途 |
---|---|
FROM | base image |
RUN | 执行命令 |
ADD | 添加文件 |
COPY | 拷贝文件 |
CMD | 执行命令 |
EXPOSE | 暴露端口 |
WORKDIR | 指定路径 |
MIANTAINER | 维护者 |
ENV | 设定环境变量 |
ENVRYPOINT | 容器入口 |
USER | 指定用户 |
VOLUME | mount point |
十二、镜像分层
Dockerfile中的每一行都产生一个新层
十三、Volume
提供独立于容器之外的持久化存储
docker run -d –name nginx -v /usr/share/nginx/html nginx
docker exec -it nginx /bin/bash
docker run -v $PWD/html:/usr/share/nginx/html nginx
++++++++++++++++++++++++++++
docker create -v $PWD/data:/var /mydata –name data_container ubuntu
docker run -it –volumes-from data_container ubuntu /bin/bash
mount
十四、Registry
镜像仓库
十五、术语
English | 中文 |
---|---|
host | 宿主机 |
image | 镜像 |
container | 容器 |
registry | 仓库 |
daemon | 守护程序 |
client | 客户端 |
docker search whalesay
docker pull docker/whalesay
docker push myname/whalesay
国内的一些仓库
daocloud
时速云
aliyun
[root@test3 dockerfiler2]# docker run docker/whalesay cowsay Docker你好!
docker tag docker/whalesay hch/whalesay
curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-(uname -m) > /usr/local/bin/docker-compose
docker-compose.yml常用命令
命令 | 用途 |
---|---|
build | 本地创建镜像 |
command | 覆盖缺省命令 |
depends_on | 连接容器 |
ports | 暴露端口 |
volumes | 卷 |
image | pull镜像 |
docker-compose命令
命令 | 用途 |
---|---|
up | 启动服务 |
stop | 停止服务 |
rm | 删除服务中的各个容器 |
logs | 观察各个容器的日志 |
ps | 列出服务相关的容器 |
十六、docker基本命令
docker ps:查看正在运行的容器
docker images:查看现有的镜像
docker logs: 查看某个容器的日志
docker run: 运行某个容器
docker inspect:查看某个容器
docker exec:进入某个容器
docker start/stop:启动或者停止某个容器
[root@test3 hadoop-docker]# touch Dockerfile
[root@test3 hadoop-docker]# ll
total 0
-rw-r–r– 1 root root 0 Feb 27 19:18 Dockerfile
[root@test3 hadoop-docker]# vim Dockerfile
FROM ubuntu:14.04
MAINTAINER hsiehchou
WORKDIR /root
# install openssh-server, openjdk and wget
RUN apt-get update && apt-get install -y openssh-server openjdk-7-jdk wget
# install hadoop 2.7.2
RUN wget https://github.com/kiwenlau/compile-hadoop/release/download/2.7.2/hadoop-2.7.2.tar.gz && \
tar -zxvf hadoop-2.7.2.tar.gz && \
mv hadoop-2.7.2 /usr/local/hadoop && \
rm hadoop-2.7.2.tar.gz
# set environment variable
ENV JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd-64
ENV HADOOP_HOME=/usr/local/hadoop
ENV PATH=$PATH:/usr/local/hadoop/bin:/usr/local/hadoop/sbin
# ssh without key
RUN ssh-keygen -t rsa -f ~/.ssh/id_rsa -p '' && \
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
RUN mkdir -p ~/hdfs/namenode && \
mkdir -p ~/hdfs/datanode && \
mkdir $HADOOP_HOME/logs
COPY config/* /tmp/
RUN mv /tmp/ssh_config ~/.ssh/config && \
mv /tmp/hadoop-env.sh /usr/local/hadoop/etc/hadoop/hadoop-env.sh && \
mv /tmp/hdfs-site.xml $HADOOP_HOME/etc/hadoop/hdfs-site.xml && \
mv /tmp/core-site.xml $HADOOP_HOME/etc/hadoop/core-site.xml && \
mv /tmp/mapred-site.xml $HADOOP_HOME/etc/hadoop/mapred-site.xml && \
mv /tmp/yarn-site.xml $HADOOP_HOME/etc/hadoop/yarn-site.xml && \
mv /tmp/slaves $HADOOP_HOME/etc/hadoop/slaves && \
mv /tmp/start-hadoop.sh ~/start-hadoop.ssh && \
mv /tmp/run-wordcount.sh ~/run-wordcount.sh
RUN chmod +x ~/start-hadoop.sh && \
chmod +x ~/run-wordcount.sh && \
chmod +x $HADOOP_HOME/sbin/start-dfs.sh && \
chmod +x $HADOOP_HOME/sbin/start-yarn.sh
# format namenode
RUN /usr/local/hadoop/bin/hdfs namenode -format
CMD ["sh", "-c", "service ssh start: bash"]