CentOs 7 安裝 Docker-ce 社區版本
安裝docker依賴包
yum install -y yum-utils device-mapper-persistent-data lvm2
添加Docker-ce 軟體源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
關閉Docker-ce 的邊緣版本和測驗版本
yum-config-manager --enable docker-ce-edge
yum-config-manager --enable docker-ce-test
更新yum源
yum makecache fast
安裝Docker-ce(第一種安裝方式)
yum install docker-ce -y
查看是否安裝成功
docker version
Client:
Version: 18.06.1-ce-rc1
API version: 1.38
Go version: go1.10.3
Git commit: 0928140
Built: Wed Aug 8 01:35:58 2018
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
查看可以提供的安裝版本(第二種安裝方式)
yum list docker-ce --showduplicates|sort -r
安裝指定版本的Docker-ce
yum install docker-ce-17.09.0.ce -y
使用Docker 腳本安裝Docker(第三種方式)
下載Docker 安裝腳本
curl -fsSL get.docker.com -o get-docker.sh
執行 Docker 腳本使用 aliyun 鏡像
sh get-docker.sh --mirror Aliyun
更換Docker 鏡像倉庫為國內鏡像源
注冊Daocloud賬號,并登陸Daocloud
https://www.daocloud.io/mirror#accelerator-doc
執行更換Daocloud 國內鏡像源命令
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://e674da1e.m.daocloud.io
重啟Docker 使國內源生效
systemctl restart docker
添加Docker 為隨機啟動
systemctl enable docker
Docker 鏡像的基礎操作
從github 拉取 鏡像
命令格式:
docker pull [選項] [Docker Registry 地址[:埠號]/]倉庫名[:標簽]
Docker 鏡像倉庫地址:地址的格式一般是 <域名/IP>[:埠號],默認地址是 Docker Hub, 倉庫名:兩段式名稱,即 <用戶名>/<軟體名>,對于 Docker Hub,如果不給出用戶名,則默認為 library,也就是官方鏡像,
實體:
docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
c64513b74145: Pull complete
01b8b12bad90: Pull complete
c5d85cf7a05f: Pull complete
b6b268720157: Pull complete
e12192999ff1: Pull complete
Digest: sha256:8c3cced1211d1a566088c0af049cc8cd6f33f5b275e62e6285ca6a13e66a98f0
Status: Downloaded newer image for ubuntu:latest
列出本地已下載的鏡像
docker image
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 735f80812f90 2 weeks ago 83.5MB
hello-world latest 2cb0d9787c4d 4 weeks ago 1.85kB
洗掉本地鏡像
docker image rm -f 2cb0d9787c4d
根據鏡像 ID、鏡像名、摘要洗掉鏡像
使用Dockerfile構建自己的鏡像
創建一個空白的目錄并進入此目錄
mkdir first
cd first/
上傳定制鏡像需要的檔案到此目錄下
[root@MiWiFi-R3-srv ~]# cd first/
[root@MiWiFi-R3-srv first]# ll
總用量 12
-rw-r--r--. 1 root root 604 8月 11 15:50 Dockerfile
-rw-r--r--. 1 root root 230 8月 11 14:57 index.js
-rw-r--r--. 1 root root 228 8月 11 14:58 package.json
[root@MiWiFi-R3-srv first]#
編輯Dockerfile檔案
# 基礎鏡像來自于 hub.docker.com
FROM centos
#鏡像維護資訊 https://xiaopangsoftware.taobao.com
MAINTAINER xiaopangruanjianxuetang
#指定作業目錄
WORKDIR /app
#將專案相關檔案拷貝到app下,如果目錄不存在會自動創建
COPY index.js /app
COPY package.json /app
# 安裝命令來自于 nodejs 官網 https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora
RUN curl --silent --location https://rpm.nodesource.com/setup_10.x | bash - && yum -y install nodejs && npm install
#暴露埠
EXPOSE 8080
#容器啟動命令
CMD ["node", "/app/index.js"]
構建鏡像
docker build -t xiaopang/centos-nodejs-1 .
-t 你的名字/鏡像的名字
構建成功的后半部分 顯示結果
...
npm notice created a lockfile as package-lock.json. You should commit this file.
added 50 packages in 9.583s
Removing intermediate container 8dc630bdf3fd
---> f0beb800684a
Step 7/8 : EXPOSE 8080
---> Running in f1e679738194
Removing intermediate container f1e679738194
---> 3c0c770cd1c2
Step 8/8 : CMD ["node", "/app/index.js"]
---> Running in 9377ccc41407
Removing intermediate container 9377ccc41407
---> cea31fd11519
Successfully built cea31fd11519
Successfully tagged xiaopang/centos-nodejs-1:latest
查看構建成功的images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaopang/centos-nodejs-1 latest cea31fd11519 7 minutes ago 343MB
centos latest 5182e96772bf 4 days ago 200MB
ubuntu latest 735f80812f90 2 weeks ago 83.5MB
運行新構建的images
docker run -p 8080:8080 -d xiaopang/centos-nodejs-1
訪問8080,驗證鏡像是否制作成功
[root@localhost first]# curl http://127.0.0.1:8080
Hello World
恭喜你鏡像制作成功了
你可能遇到的問題
docker build 報錯[Warning] IPv4 forwarding is disabled. Networking will not work.
- 編輯00-system.conf
vim /usr/lib/sysctl.d/00-system.conf
- 追加以下內容
net.ipv4.ip_forward=1
- 重啟網卡服務
systemctl restart network
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/33949.html
標籤:其他
