Docker
簡(jiǎn)介
基本概念
Docker 包括三個(gè)基本概念
- 鏡像(Image)
- 容器(Container)
- 倉(cāng)庫(kù)(Repository)
Docker 安裝 以centos為例
裝備工作
Docker CE 支持 64 位版本 CentOS 7,并且要求內(nèi)核版本不低于 3.10。 CentOS 7 滿足最低內(nèi)核的要求,但由于內(nèi)核版本比較低,部分功能(如 overlay2 存儲(chǔ)層驅(qū)動(dòng))無(wú)法使用,并且部分功能可能不太穩(wěn)定。
卸載舊版本
舊版本的 Docker 稱為 docker 或者 docker-engine,使用以下命令卸載舊版本:
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
使用 yum 安裝 (鑒于國(guó)內(nèi)網(wǎng)絡(luò)問(wèn)題,強(qiáng)烈建議使用國(guó)內(nèi)源)
執(zhí)行下面的命令添加 yum 軟件源:
$ sudo yum-config-manager \
--add-repo \
https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
# 官方源# $ sudo yum-config-manager \# --add-repo \# https://download.docker.com/linux/centos/docker-ce.repo
如果需要測(cè)試版本的 Docker CE 請(qǐng)使用以下命令:
$ sudo yum-config-manager --enable docker-ce-test
如果需要每日構(gòu)建版本的 Docker CE 請(qǐng)使用以下命令:
$ sudo yum-config-manager --enable docker-ce-nightly
安裝 Docker CE
更新 yum 軟件源緩存,并安裝 docker-ce。
$ sudo yum makecache fast
$ sudo yum install docker-ce
執(zhí)行這個(gè)命令后,腳本就會(huì)自動(dòng)的將一切準(zhǔn)備工作做好,并且把 Docker CE 的 Edge 版本安裝在系統(tǒng)中。
啟動(dòng) Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker
測(cè)試 Docker 是否安裝正確
[root@localhost ~]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Docker Centos
指定端口創(chuàng)建centos容器
sudo docker run --privileged --name my_centos -dti -p 2222:22 -p 28000:8000 centos /usr/sbin/init
將CMD或者entrypoint設(shè)置為/usr/sbin/init,會(huì)自動(dòng)將dbus等服務(wù)啟動(dòng)起來(lái),就可以使用systemctl了
注:使用下面命令創(chuàng)建容器會(huì)有dbus服務(wù)啟動(dòng)失敗的問(wèn)題,
Failed to get D-Bus connection: Operation not permitted
sudo docker run --name my_centos_1 -dti -p 6666:35000 centos /bin/bash
OpenSSH安裝
1.安裝ssh及組件
sudo yum install openssh*
2.安裝后查看當(dāng)前ssh版本如下所示
[root@75d0484e5f68 /]# rpm -qa | grep openssh
openssh-askpass-7.4p1-16.el7.x86_64
openssh-server-sysvinit-7.4p1-16.el7.x86_64
openssh-keycat-7.4p1-16.el7.x86_64
openssh-7.4p1-16.el7.x86_64
openssh-cavs-7.4p1-16.el7.x86_64
openssh-clients-7.4p1-16.el7.x86_64
openssh-ldap-7.4p1-16.el7.x86_64
openssh-server-7.4p1-16.el7.x86_64
3.注冊(cè)使用服務(wù)
systemctl enable sshd
systemctl start sshd
4.查看22端口,sshd服務(wù)已啟動(dòng)
[root@75d0484e5f68 /]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 3282 root 3u IPv4 10051181 0t0 TCP *:ssh (LISTEN)
sshd 3282 root 4u IPv6 10051183 0t0 TCP *:ssh (LISTEN)
5.開放宿主機(jī)對(duì)應(yīng)的端口號(hào) 12222->22
1.退出當(dāng)前容器的登錄,回到宿主機(jī)
exit
2.centos7端口操作命令
查看防火墻狀態(tài)。得到結(jié)果是running或者not running
firewall-cmd --state
在running 狀態(tài)下,向firewall 添加需要開放的端口命令為
firewall-cmd --permanent --zone=public --add-port=12222/tcp
//永久的添加該端口。去掉--permanent則表示臨時(shí)。刪除端口命令為
firewall-cmd --zone= public --remove-port=12222/tcp --permanent
//加載配置,使得修改有效。
firewall-cmd --reload
使用命令 //查看開啟的端口,出現(xiàn)8080/tcp這開啟正確
firewall-cmd --permanent --zone=public --list-ports
6.修改用戶登錄密碼,開始ssh連接
[root@75d0484e5f68 /]# passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
7.開始ssh連接,輸入用戶密碼登錄成功
Last failed login: Sun Jan 6 04:44:43 UTC 2019 from 192.168.219.1 on ssh:notty
There was 1 failed login attempt since the last successful login.
[root@75d0484e5f68 ~]#
Docker UI
- Run cmd
## 搜索dockerui鏡像
docker search dockerui
## 拉取鏡像
docker pull abh1nav/dockerui
## 指定端口創(chuàng)建dockerui容器
docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock abh1nav/dockerui
-
打開瀏覽器訪問(wèn)http://<dockerd host ip>:9000,就可以看到自己容器的UI界面了
DockerUI.png
Docker常用命令
- 搜索鏡像
docker search centos
- 獲取鏡像
docker pull [選項(xiàng)] [Docker Registry 地址[:端口號(hào)]/]倉(cāng)庫(kù)名[:標(biāo)簽]
- 重命名容器名稱
docker rename oldName newName
- 進(jìn)入容器,推薦使用exec進(jìn)入容器
docker exec -t -i my_centos_1 bash
- 查看所有鏡像
docker images
或者
docker images ls -a
- 查看運(yùn)行中的容器,后面加-a查看所有,包括未啟動(dòng)的
docker ps
docker ps -a
或者
docker container ls
docker container ls -a
- 停止運(yùn)行中的容器
docker stop 【容器名稱或者ID(較長(zhǎng),可輸入前面字符)】
- 啟動(dòng)未運(yùn)行的容器
docker start 【容器名稱或者ID(較長(zhǎng),可輸入前面字符)】
- 重啟運(yùn)行中的容器
docker restart 【容器名稱或者ID(較長(zhǎng),可輸入前面字符)】
- 刪除容器
docker container rm 【容器名稱或者ID(較長(zhǎng),可輸入前面字符)】
- 刪除鏡像
docker image rm 【鏡像名稱或者ID(較長(zhǎng),可輸入前面字符)】
