docker的安裝與使用

文檔與安裝

基本安裝 官方的文檔走就行了,
本文羅列一些簡單步驟

https://docs.docker.com/install/linux/docker-ce/ubuntu/#prerequisites

1,安裝 前如果之前有安裝 則刪除
sudo apt-get remove docker docker-engine docker.io

2,查看自己的是哪個版本,官網會有不同的命令
sudo lsb_release -a
我是ubuntu16.04 ,

3,文檔提供了幾種安裝 方案
-1,設置 docker的倉庫後 apt-get
-2,用deb包
-3,用 convenience scripts 來安裝

一般都用 1,這裏也使用1

sudo apt-get update

4 安裝 其它

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

5,增加docker的官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

6,檢驗下fingerprint
sudo apt-key fingerprint 0EBFCD88

7 增加倉庫,(這裏也有選擇是什麼cpu架構,一般都是x86_64位)

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

sudo apt-get update

8 安裝
sudo apt-get install docker-ce

9 測試
sudo docker run hello-world

10 查看 版本
sudo docker version

例子1,用hub.docker.com拉取一個nginx

查看本機有多少image
sudo docker image ls

我們嘗試雲hub.docker.com 中拉一個出來測試下
https://hub.docker.com/_/nginx/
docker pull nginx

hub.docker.com 是一個docker 鏡像的倉庫,裏面有包括 ubuntu, redhat 這樣的 系統鏡像,也有在之上的 nginx,redis等,也包含 python之類的各版本鏡像, 即拉即用,牛逼轟轟.

啓動:
docker run --name some-nginx -d -p 8080:80 nginx

查看進程
ps -ef | grep docker

/usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.2 -container-port 80

測試
wget http://127.0.0.1:8080

例子2,自建一個Dockerfile 後build,後去執行

1,寫一個python文件
新建一個文件 hello.py

print("hello,world,first cmd")

測試,執行, python hello.py OK,

2, 新建一個Dockerfile

vi Dockerfile
內容如下


FROM python:3.6.7
ADD hello.py /
WORKDIR /
CMD ["python","hello.py"]

3, 因爲依賴了 python3.6.7,這裏要和hub.docker.com連接,進行登陸

ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxxxxx
Password:
WARNING! Your password will be stored unencrypted in /home/ming/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

4,把Dockerfile build成image

ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker build -t xxxxxx/hellopy .
Sending build context to Docker daemon  15.87kB
Step 1/4 : FROM python:3.6.7
3.6.7: Pulling from library/python
54f7e8ac135a: Pull complete
d6341e30912f: Pull complete
087a57faf949: Pull complete
5d71636fb824: Pull complete
0c1db9598990: Pull complete
2eeb5ce9b924: Pull complete
a8c530378055: Pull complete
687ed2fb2a0d: Pull complete
620aea26e853: Pull complete
Digest: sha256:fc34d5b6cf5d00a6139a74370dc27ddc9ce18303e2210d0f199a6050cc29aa45
Status: Downloaded newer image for python:3.6.7
---> 1ec4d11819ad
Step 2/4 : ADD hello.py /
---> ac96cabf064f
Step 3/4 : WORKDIR /
---> Running in d3565c0d40eb
Removing intermediate container d3565c0d40eb
---> 7635082ac9d2
Step 4/4 : CMD ["python","hello.py"]
---> Running in 062edb8e18b4
Removing intermediate container 062edb8e18b4
---> 55c88d812066
Successfully built 55c88d812066
Successfully tagged xxxxxx/hellopy:latest

5,查看image一些狀態

ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
xxxxxx/hellopy   latest              55c88d812066        45 seconds ago      918MB
<none>                   <none>              f8d761b2ce56        11 minutes ago      31B
<none>                   <none>              175510e82d22        12 minutes ago      31B
<none>                   <none>              a3b03c0ad5c3        14 minutes ago      31B
<none>                   <none>              9c7e982eeba3        17 minutes ago      31B
<none>                   <none>              551d0f216a27        18 minutes ago      31B
nginx                    latest              568c4670fa80        9 days ago          109MB
python                   3.6.7               1ec4d11819ad        2 weeks ago         918MB
hello-world              latest              4ab4c602aa5e        3 months ago        1.84kB


ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker history 551d0f216a27
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
551d0f216a27        19 minutes ago      /bin/sh -c #(nop)  CMD ["python hello.py"]      0B
8e315e5e1fde        19 minutes ago      /bin/sh -c #(nop) ADD file:69b0acbe51cbcdafd…   31B

6,運行

ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker run xxxxxx/hellopy
hello,world,first cmd

其它

把 當前用戶加到docker組中,

方便一些權限 問題(很多命令,如docker image ls等不用sudo了)
主要就是 gpasswd -a ming docker
然後重啓docker服務
然後重新進shell
如下:

ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ docker image ls
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/images/json: dial unix /var/run/docker.sock: connect: permission denied
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ groupadd docker
groupadd:“docker”組已存在
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ gpasswd -a ming docker
gpasswd:沒有權限。
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ sudo gpasswd -a ming docker
[sudo] ming 的密碼:
正在將用戶“ming”加入到“docker”組中
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ sudo service docker restart
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$

然後再重新進入shell即可,
測試:執行 docker image ls

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章