1: 環(huán)境準備(PS:mac 通過Parallels Desktop 安裝centos7 系統(tǒng))
新安裝的 centos 系統(tǒng)通過界面設(shè)置的賬戶密碼并不是root 權(quán)限的賬戶, 系統(tǒng)默認是沒有激活root 用戶的,需要手工操作 ,在命令界面輸入如下命令:然后使用su 切換賬戶
sudo passwd
Password:你當前的密碼
Enter new UNIX password:這個是root的密碼
Retype new UNIX password:重復root的密碼
2:centos7 安裝docker
Docker 從1.13版本之后采用時間線的方式作為版本號,分為社區(qū)版ce(免費提供給個人開發(fā)者或者小團體使用) 和企業(yè)版EE(提供額外的收費服務(wù))
1.卸載舊版本(如果安裝過舊版本)
yum remove docker docker-common docker-selinux docker-engine
2.使用yum 進行安裝
# step 1: 安裝必要的一些系統(tǒng)工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加軟件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安裝 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 開啟Docker服務(wù)
sudo service docker start
---------------------------------------------
# 安裝指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
# Loading mirror speeds from cached hostfile
# Loaded plugins: branch, fastestmirror, langpacks
# docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
# docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable
# docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
# Available Packages
# Step2 : 安裝指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]
3.安裝驗證
[root@centos-linux /]# docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
4:修改Docker 鏡像的默認存儲位置
#1:不知道配置文件放在哪里,可以使用systemctl 命令
[root@centos-linux /]# systemctl disable docker
[root@centos-linux /]# systemctl enable docker
#---顯示結(jié)果如下:
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
#2:新建文件夾存放鏡像
[root@centos-linux /]# cd home/
[root@centos-linux home]# mkdir docker
#3:修改docker.service 文件
vim /usr/lib/systemd/system/docker.service
#在EXECStart的后面增加后如下:
ExecStart=/usr/bin/dockerd --graph /home/docker
#4:reload配置文件
systemctl daemon-reload
#5:重啟docker
systemctl restart docker.service
#6:使用docker info 命令查看 Docker Root Dir是否改成設(shè)定的目錄
Docker Root Dir: /home/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
#ps:如果沒生效,第4 ,5 步多執(zhí)行兩次,或者等一下在查看,可能會有延遲
5:pull centos 基礎(chǔ)鏡像,當然也可以直接拉取他人上傳的鏡像
#1:拉取鏡像
[root@centos-linux ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
aeb7866da422: Pull complete
Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b
Status: Downloaded newer image for centos:latest
#2:查看鏡像
docker images
7:啟動容器
#1:啟動容器
[root@centos-linux /]# docker run -it <image id> bin/bash
#2:復制一個命令窗口,查看容器id
[root@centos-linux /]# docker ps
7:安裝jdk 并上傳新鏡像
#1:在宿主機上下載jdk 包,通過命令的方式復制到docker 容器中
[root@centos-linux /]# docker cp /usr/local/etc/jdk-11.0.1_linux-x64_bin.tar.gz [容器ID]:/usr/local/etc/
#2:進入容器內(nèi)部,解壓jdk
[root@a5ee065029fc etc]# tar -zxf jdk-11.0.1_linux-x64_bin.tar.gz
#3:重命名 JDK 目錄:
[root@a5ee065029fc etc]# mv jdk-11.0.1/ jdk11/
#4:刪除jdk 安裝文件
[root@a5ee065029fc etc]# rm -f jdk-11.0.1_linux-x64_bin.tar.gz
#5:切換一個命令窗口,在宿主機上制作新的鏡像(ps:容器不要關(guān),不然重新進入又是基礎(chǔ)鏡像了)
[root@centos-linux /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5ee065029fc 75835a67d134 "bin/bash" 15 minutes ago Up 15 minutes frosty_germain
[root@centos-linux /]# docker commit a5ee065029fc ly/jdk:0.1
#6:新鏡像制作完成后可以退出容器,使用docker images 查看新鏡像,進入新鏡像可以看到剛剛安裝的jdk
[root@centos-linux /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ly/jdk 0.1 ccfd7cb8dfc9 9 seconds ago 503MB
centos latest 75835a67d134 6 weeks ago 200MB
[root@centos-linux /]# docker run -it ccfd7cb8dfc9 bin/bash