Docker实践与命令
安装docker:yum -y install docker下载镜像:docker search centos //搜索镜像启动容器: 因为是在1台虚拟机搭建3个docker的缘故,所以这次选择的docker 网卡类型为bridge模式(bridge模式容器ip无法自己指定),网段为 172.17.0.0/16 网关为172.17.0.1,三台容器的主机名和ip分别为:server172.
一、Yum 安装 Docker
yum -y install docker
启动 Docker 后再查看版本:
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
Experimental: false
1. 下载镜像:
[root@192 docker]# docker search centos //搜索镜像
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
解决:
vim /etc/sysconfig/docker-storage
DOCKER_STORAGE_OPTIONS="--selinux-enabled --log-driver=journald --signature-verification=false"
[root@192 docker]# docker pull centos:7.2.1511
Trying to pull repository docker.io/library/centos ...
7.2.1511: Pulling from docker.io/library/centos
a8c7037c15e9: Pull complete
Digest: sha256:50cca1e74da4b6a4eb4ade029c8fdd4ee8564776801914d9bd89df8c6344add0
Status: Downloaded newer image for docker.io/centos:7.2.1511
[root@192 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 7.2.1511 9aec5c5fe4ba 3 years ago 195 MB
[root@192 docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2. 启动容器:
因为是在1台虚拟机搭建3个docker的缘故,所以这次选择的docker 网卡类型为 bridge
模式(bridge
模式容器ip无法自己指定),网段为 172.17.0.0/16
网关为 172.17.0.1
,三台容器的主机名和ip分别为:
server 172.17.0.2
agent1 172.17.0.3
agent2 172.17.0.4
# 开始启动3台容器:
[root@192 docker]# docker run -it -d --name server --network bridge --hostname server docker.io/centos:7.2.1511 /bin/bash
e35571a1261d70173ceb6ed33c84d04bf8b300c0fb073b23ac67afa335980b4f
[root@192 docker]# docker run -it -d --name agent1 --network bridge --hostname agent1 docker.io/centos:7.2.1511 /bin/bash
87fa9ab888066f45023999f77152df140a8b6d8b5b4dd47ce166bd7f99025a36
[root@192 docker]# docker run -it -d --name agent2 --network bridge --hostname agent2 docker.io/centos:7.2.1511 /bin/bash
8b685b1e4177f62bc74f4c535e1905298c09ed97ab520f43b9b6b82c01e51a21
# 查看容器并进入:
[root@192 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b685b1e4177 docker.io/centos:7.2.1511 "/bin/bash" About a minute ago Up About a minute agent2
87fa9ab88806 docker.io/centos:7.2.1511 "/bin/bash" 2 minutes ago Up 2 minutes agent1
e35571a1261d docker.io/centos:7.2.1511 "/bin/bash" 2 minutes ago Up 2 minutes server
# 进入容器:
[root@192 docker]# docker exec -it server /bin/bash
# 退出容器
[root@server /]# exit
# 可能遇到问题:
[root@192 ~]# docker exec -it server /bin/bash
Error response from daemon: Container e35571a1261d70173ceb6ed33c84d04bf8b300c0fb073b23ac67afa335980b4f is not running
# 解决:
[root@192 ~]# docker start server
server
3. 配置ssh:
# 想查看容器的IP,结果ifconfig命令没有,需要安装
[root@server /]# yum -y install net-tools
# 也没有shh命令,需要安装
[root@server /]# yum list | grep ssh
[root@server /]# yum -y install openssh-clients.x86_64 openssh-server.x86_64
# 修改root密码为123456
[root@server /]# passwd root
# 启动SSHD服务,或者加&在后台启动
[root@server /]# /usr/sbin/sshd -D
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
[root@server /]# ssh-keygen -A
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519
[root@server /]# /usr/sbin/sshd -D &
[root@server /]# ssh 172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
ECDSA key fingerprint is SHA256:WzL8/DOOOlphUV4Cs8nBNxbM6LWml4FN5A2DCL3adao.
ECDSA key fingerprint is MD5:f6:c7:f5:cb:f3:2b:19:6c:6f:89:ff:d9:ee:0a:af:82.
Are you sure you want to continue connecting (yes/no)?
4. 重启docker服务:
[root@192 docker]# systemctl restart docker
[root@192 docker]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2022-03-21 00:25:56 PDT; 28min ago
Docs: http://docs.docker.com
Main PID: 50766 (dockerd-current)
Memory: 18.0M
CGroup: /system.slice/docker.service
├─50766 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker...
├─50776 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interva...
├─51467 /usr/bin/docker-containerd-shim-current e35571a1261d70173ceb6ed33c84d04bf8b300c0fb073b23ac67afa335980b4f /var/run/d...
├─51573 /usr/bin/docker-containerd-shim-current 87fa9ab888066f45023999f77152df140a8b6d8b5b4dd47ce166bd7f99025a36 /var/run/d...
└─51665 /usr/bin/docker-containerd-shim-current 8b685b1e4177f62bc74f4c535e1905298c09ed97ab520f43b9b6b82c01e51a21 /var/run/d...
Mar 21 00:25:56 192.168.42.132 dockerd-current[50766]: time="2022-03-21T00:25:56.328358495-07:00" level=info msg="Firewalld runn...false"
Mar 21 00:25:56 192.168.42.132 dockerd-current[50766]: time="2022-03-21T00:25:56.436304532-07:00" level=info msg="Default bridge...dress"
Mar 21 00:25:56 192.168.42.132 dockerd-current[50766]: time="2022-03-21T00:25:56.588477237-07:00" level=info msg="Loading contai...done."
Mar 21 00:25:56 192.168.42.132 dockerd-current[50766]: time="2022-03-21T00:25:56.730061633-07:00" level=info msg="Daemon has com...ation"
Mar 21 00:25:56 192.168.42.132 dockerd-current[50766]: time="2022-03-21T00:25:56.730927542-07:00" level=info msg="Docker daemon"...1.13.1
Mar 21 00:25:56 192.168.42.132 systemd[1]: Started Docker Application Container Engine.
Mar 21 00:25:56 192.168.42.132 dockerd-current[50766]: time="2022-03-21T00:25:56.757134704-07:00" level=info msg="API listen on ....sock"
Mar 21 00:42:08 192.168.42.132 oci-umount[51500]: umounthook <debug>: prestart container_id:e35571a1261d rootfs:/var/lib/docker/...rootfs
Mar 21 00:42:21 192.168.42.132 dockerd-current[50766]: time="2022-03-21T00:42:21.421376820-07:00" level=error msg="Handler for POST /v...
Mar 21 00:42:50 192.168.42.132 oci-umount[51606]: umounthook <debug>: prestart container_id:87fa9ab88806 rootfs:/var/lib/docker/...rootfs
Hint: Some lines were ellipsized, use -l to show in full.
重启过程中遇到的问题:
[root@192 docker]# systemctl restart docker.service
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
# 解决:修改这两个配置文件后重启docker即可(来自:https://www.cnblogs.com/lhd1998/p/13361739.html?utm_source=tuicool)
vim /etc/sysconfig/docker-storage
DOCKER_STORAGE_OPTIONS="--selinux-enabled --log-driver=journald --signature-verification=false"
vim /etc/docker/daemon.json
{ "storage-driver": "devicemapper" }
5. 将本地文件上传到docker容器中:
# 方式一
[root@192 ~]# docker cp /mnt/jdk-8u91-linux-x64.tar.gz e35571a1261d:/mnt
# 方式二
[root@192 ~]# docker inspect -f '{{.ID}}' e35571a1261d
e35571a1261d70173ceb6ed33c84d04bf8b300c0fb073b23ac67afa335980b4f
[root@192 ~]# docker cp /mnt/jdk-8u91-linux-x64.tar.gz e35571a1261d70173ceb6ed33c84d04bf8b300c0fb073b23ac67afa335980b4f:/mnt
6. 导出镜像:
# 将正在运行的 docker 容器打包成 image 镜像
docker commit e35571a1261d[容器ID] iec104:v1.0[名称:版本号]
# 把刚刚打包好的镜像保存成 tar 文件:docker save 镜像名:版本号 -o /路径/保存的包名.tar
docker save iec104:v1.0 -o /home/hadoop/Documents/iec104.tar
# 将打包好的镜像拷贝到新机器上面,执行load命令解压
docker load < iec104.tar
7. 删除容器和镜像:
[root@192 ~]# docker rm -f f46b3d7c8b51
f46b3d7c8b51
[root@192 ~]# docker rmi 5cb3aa00f899
二、离线安装 Docker 和 Docker-compose
1. 下载 docker 镜像
地址(国内可能访问不到):https://download.docker.com/linux/static/stable/x86_64/
备用地址:http://mirrors.sustech.edu.cn/docker-ce/linux/static/stable/x86_64/
将 docker-23.0.6.tgz
文件上传到服务器上面并解压,复制文件到系统路径
tar -zxvf docker-23.0.6.tgz
sudo cp docker/* /usr/bin/
2. 配置 Docker 服务
使用以下命令创建 Docker 服务配置文件:
sudo tee /etc/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
3. 服务生效
# 为文件设置可执行权限
sudo chmod 777 /etc/systemd/system/docker.service
# 重新加载配置文件,让配置生效
sudo systemctl daemon-reload
# 启用 Docker 服务
$ sudo systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /etc/systemd/system/docker.service.
# 启动 Docker 服务
sudo systemctl start docker
4. 查看状态
# 查看版本
$ docker --version
Docker version 23.0.6, build ef23cbc
sudo systemctl status docker
# 或者
sudo service docker status
5. 下载安装 docker-compose
地址:https://github.com/docker/compose/releases
版本:docker-compose-linux-x86_64
# 改名
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
# 添加执行权限
chmod +x /usr/local/bin/docker-compose
#查看版本号
$ docker-compose version
Docker Compose version v2.21.0
参考:docker离线安装及部署各类中间件(x86系统架构)
三、Docker 安装 hadoop
方式一:从最基础的Linux镜像开始安装
接着上面拉取 Centos
镜像、三个节点配置好免密后,再上传 java
、hadoop
等的安装包解压后自己配置即可。以下是详细步骤:
[root@localhost mnt]# mkdir dockers
[root@localhost mnt]# cd dockers
(1)安装SSH:
[root@localhost dockers]# vi Dockerfile
FROM centos:7.2.1511
MAINTAINER dys
RUN yum install -y openssh-server sudo
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN yum install -y openssh-clients
RUN echo "root:111111" | chpasswd
RUN echo "root ALL=(ALL) ALL" >> /etc/sudoers
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN mkdir /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
[root@localhost dockers]# docker build -t="centos7-ssh" .
[root@localhost dockers]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7-ssh latest ac8dbd1104fd About a minute ago 413 MB
docker.io/centos 7.2.1511 9aec5c5fe4ba 3 years ago 195 MB
(2)设置固定IP:
需要用到 pipework,他用于给容器设置IP。
[root@localhost dockers]# yum install -y git
[root@localhost dockers]# git clone https://github.com/jpetazzo/pipework.git
[root@localhost dockers]# cp pipework/pipework /usr/local/bin/
[root@localhost dockers]# yum -y install bridge-utils
# 创建网络
[root@localhost dockers]# brctl addbr br1
[root@localhost dockers]# ip link set dev br1 up
[root@localhost dockers]# ip addr add 192.168.3.1/24 dev br1
# 基于 centos7-ssh 这个镜像启动一个容器,名为 centos7.ssh
[root@localhost dockers]# docker run -d --name=centos7.ssh centos7-ssh
# 设置IP:为名为 centos7.ssh 的容器指定了IP 192.168.3.20
[root@localhost dockers]# pipework br1 centos7.ssh 192.168.3.20/24
# 分别使用 ping 与 ssh 命令进行验证,看是否可以ping通和成功登录
[root@localhost dockers]# ping 192.168.3.20
[root@localhost dockers]# ssh 192.168.3.20
# ssh成功登录后,就意味着已经准备好了一台服务器,再运行2个容器、设置好IP,那么就相当于有了3台服务器
[root@localhost dockers]# docker run -d --name=centos7.ssh2 centos7-ssh
[root@localhost dockers]# docker run -d --name=centos7.ssh3 centos7-ssh
[root@localhost dockers]# pipework br1 centos7.ssh2 192.168.3.22/24
[root@localhost dockers]# pipework br1 centos7.ssh3 192.168.3.23/24
(3)构建Hadoop镜像:
上面是运行了3个centos容器,需要在每个容器中单独安装Hadoop环境,我们可以像构建SSH镜像一样,构建一个Hadoop镜像,然后运行3个Hadoop容器,这样就更简单了。
[root@localhost dockers]# vi Dockerfile
FROM centos7-ssh
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
RUN mv /usr/local/jdk1.8.0_91 /usr/local/jdk1.8
ENV JAVA_HOME /usr/local/jdk1.8
ENV PATH $JAVA_HOME/bin:$PATH
ADD hadoop-3.2.2.tar.gz /usr/local
RUN mv /usr/local/hadoop-3.2.2 /usr/local/hadoop
ENV HADOOP_HOME /usr/local/hadoop
ENV PATH $HADOOP_HOME/bin:$PATH
RUN yum install -y which sudo
这里是基于 centos7-ssh
这个镜像,把 JAVA
和 Hadoop
的环境都配置好了。
前提:在Dockerfile所在目录下准备好 jdk-8u101-linux-x64.tar.gz
与 hadoop-2.7.3.tar.gz
# 执行构建命令,新镜像命名为 hadoop
[root@localhost dockers]# docker build -t="hadoop" .
# 运行3个hadoop容器,分别命名为 hadoop0,hadoop1,hadoop2,hadoop0 作为master
[root@localhost dockers]# docker run --name hadoop0 --hostname hadoop0 -d -P -p 50070:50070 -p 8088:8088 hadoop
[root@localhost dockers]# docker run --name hadoop1 --hostname hadoop1 -d -P hadoop
[root@localhost dockers]# docker run --name hadoop2 --hostname hadoop2 -d -P hadoop
# 容器hadoop0启动时,映射了端口号,50070和8088,是用来在浏览器中访问hadoop WEB界面的
# 配置IP
[root@localhost dockers]# pipework br1 hadoop0 192.168.3.30/24
[root@localhost dockers]# pipework br1 hadoop1 192.168.3.31/24
[root@localhost dockers]# pipework br1 hadoop2 192.168.3.32/24
(4)配置Hadoop集群:
新开3个终端窗口,分别连接到 hadoop0
, hadoop1
, hadoop2
,便于操作。
# 在各个容器中修改 /etc/hosts,添加:
192.168.3.30 master
192.168.3.31 slave1
192.168.3.32 slave2
接下来就是配置SSH免登录,及hadoop的配置文件了。
方式二:拉取别人已经分享好的hadoop镜像配置
# 选择star数量最多的镜像拉取就可以
docker search hadoop
docker run --name hadoop1 -d -h master docker.io/sequenceiq/hadoop-docker
docker run --name hadoop2 -d -h slave1 docker.io/sequenceiq/hadoop-docker
docker run --name hadoop3 -d -h slave2 docker.io/sequenceiq/hadoop-docker
docker exec -it hadoop1 bash
bash-4.1# java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
bash-4.1# cat /etc/centos-release
CentOS release 6.6 (Final)
bash-4.1# ls -l /usr/local/hadoop-2.7.0/
total 44
-rw-r--r--. 1 10021 10021 15429 Apr 10 2015 LICENSE.txt
-rw-r--r--. 1 10021 10021 101 Apr 10 2015 NOTICE.txt
-rw-r--r--. 1 10021 10021 1366 Apr 10 2015 README.txt
drwxr-xr-x. 2 10021 10021 4096 Apr 10 2015 bin
drwxr-xr-x. 3 10021 10021 19 Apr 10 2015 etc
drwxr-xr-x. 2 10021 10021 101 Apr 10 2015 include
drwxr-xr-x. 2 root root 4096 Jul 22 2015 input
drwxr-xr-x. 3 10021 10021 19 Jul 22 2015 lib
drwxr-xr-x. 2 10021 10021 4096 Apr 10 2015 libexec
drwxr-xr-x. 3 root root 4096 Feb 20 22:28 logs
drwxr-xr-x. 2 10021 10021 4096 Apr 10 2015 sbin
drwxr-xr-x. 4 10021 10021 29 Apr 10 2015 share
后面步骤可自行百度,基本就是安装hadoop那老一套,可参考:docker安装hadoop伪集群
更多推荐
所有评论(0)