要安裝運行Python的容器,那先到https://hub.docker.com/去查找一下,看看有哪些可用的鏡像。
恩,最高的版本已經是3.8.0b4了,不過考慮下還是安裝3.7.4穩定版把。
用來安裝的虛擬機是Ubuntu 18.04服務器版,安裝虛擬機就跳過不寫了,太簡單。
為了方便,使用root用戶操作。
在Ubuntu上安裝Docker
使用官方腳本,在終端輸入
curl -fsSL get.docker.com -o get-docker.sh
瞬間下載安裝腳本:
-rw-r--r-- 1 root root 13185 Sep 18 12:30 get-docker.sh
執行一下
root@ubuntu:~# sh get-docker.sh
很快的,安裝完了。啟動docker服務:
systemctl start docker
systemctl status docker
可以看到,docker服務已經在運行了,設置開機啟動用systemctl enable docker。
root@ubuntu:~# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-09-18 12:56:23 UTC; 9h ago
Docs: https://docs.docker.com
Main PID: 30874 (dockerd)
Tasks: 10
CGroup: /system.slice/docker.service
└─30874 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
試運行一下docker hello-world,第一次會提示 "Unable to find image 'hello-world:latest' locally", 然后從docker hub去下載鏡像到本地。
再次運行,出現下面提示,運行成功。
root@ubuntu-1804-102:~# 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 image ls, 可以看到hello-world已經在本地了
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 8 months ago 1.84kB
下載python 3.7.4鏡像,需要幾分鐘,看網速,因為官方鏡像有900M多
docker pull python:3.7.4
看下鏡像列表:
root@ubuntu:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.7.4 02d2bb146b3b 6 days ago 918MB
hello-world latest fce289e99eb9 8 months ago 1.84kB
運行容器
root@ubuntu:~# docker run -p 8000:8000 -i -t -v /data/www/:/www python:3.7.4 /bin/bash
root@c6baa2eb898c:/#
這里 -p 參數表示發布端口號到主機, -i -t 或者 -it 表示交互式容器, -v /data/www/:/www 表示把主機上/data/www目錄映射到容器的/www目錄下。
交互模式下,使用exit即可退出, 如果想讓容器一直運行,則可以添加-d參數。
root@ubuntu:~# docker run -p 8000:8000 -i -t -d -v /data/www/:/www python:3.7.4 /bin/bash
e21ad1ec196836fb61aea72a1b811376312d935c152b7d17eb425b8e8015da02
啟動/停止容易,以及在交互模式運行hello world程序。
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e21ad1ec1968 python:3.7.4 "/bin/bash" About a minute ago Up About a minute 0.0.0.0:8000->8000/tcp vigorous_wescoff
root@ubuntu:~# docker stop e21ad1ec1968
e21ad1ec1968
root@ubuntu:~# docker run -p 8000:8000 -i -t -v /data/www/:/www python:3.7.4 /bin/bash
root@f292060243d7:/# cd /www
root@f292060243d7:/www# python3 hello.py
Hello World!
root@f292060243d7:/www#
常用命令:
啟動容器:docker run <參數>
停止容器:docker stop <參數>
查看容器:docker ps
刪除容器:docker rm <容器>
查看鏡像: docker image ls
刪除鏡像:docker image rm <鏡像>
#刪除鏡像可以使用鏡像名,即<倉庫名>:<標簽>
#可以使用長id,短id(長id的前3位)
#可以使用鏡像摘要: docker image ls --digests
root@ubuntu:~# docker image ls --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
python 3.7.4 sha256:0f0e991a97426db345ca7ec59fa911c8ed27ced27c88ae9966b452bcc6438c2f 02d2bb146b3b 6 days ago 918MB
#使用摘要更精確的刪除 docker image rm
@DIGEST
如何保存容器的更改:
發現需要使用pip安裝模塊,但安裝完后重新運行容器發現新裝的模塊并沒有保存,這是因為沒有commit。
在docker容器交互界面安裝完以后,另開一個窗口到本機,查看運行中的容器:
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4931cfd2b692 python:3.7.4 "/bin/bash" 2 minutes ago Up 2 minutes crazy_nightingale
記錄下Container ID, 提交一下
root@ubuntu:~# docker commit 4931cfd2b692 python:3.7.4
sha256:636bf7a28bc7057c8c3b60fe5c736b32bb87b8af0e1b0328b1c28ded8245d7f0
再登錄容器交互界面,發現前面的更改已經保存了。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
