Docker 安装Python 3.7.4
2022-11-18 11:19:12    0    0    0
junjie
要安装运行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 ``` 再登录容器交互界面,发现前面的更改已经保存了。 作者:AK蜗牛 链接:https://www.jianshu.com/p/dad7a7ae13ac 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

上一篇: 宝塔中使用Laravel程序 .env 配置不生效的解决方法putenv函数被禁

下一篇: win7 可打开的最新vscode版本1.70

Table of content