在ubuntu上部署与使用docker(python)
创始人
2024-05-29 10:49:32
0
  • 1.安装Docker

首先,更新现有的包列表

sudo apt update

接下来安装一些允许童HTTPS才能使用的软件包:

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

然后将官方Docker存储库的GPG秘钥添加到您的系统

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

将Doker存储库添加到APT源

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

接下来,使用新添加的repo源中的Docker包更新包数据库

sudo apt update

确保docker repo安装而不是默认的Ubunturepo:

apt-cache policy docker-ce

虽然Docker的版本号可能不同,但您还是会看到这样的输出:

docker-ce:
Installed: (none)
Candidate: 18.03.1~ce~3-0~ubuntu
Version table:
18.03.1~ce~3-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages

最后,安装Docker:

sudo apt install docker-ce

现在应该安装好Docker了,检查它是否正在运行:

sudo systemctl status docker

输出应类似于以下内容,表明该服务处于工作状态:

● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-07-05 15:08:39 UTC; 2min 55s ago
Docs: https://docs.docker.com
Main PID: 10096 (dockerd)
Tasks: 16
CGroup: /system.slice/docker.service
├─10096 /usr/bin/dockerd -H fd://
└─10113 docker-containerd --config /var/run/docker/containerd/containerd.toml
  • 2.使用Docker命令

查看所有可用子命令 请输入:

docker attach      Attach local standard input, output, and error streams to a running containercommit      Create a new image from a container's changescp          Copy files/folders between a container and the local filesystemcreate      Create a new containerdiff        Inspect changes to files or directories on a container's filesystemevents      Get real time events from the serverexport      Export a container's filesystem as a tar archivehistory     Show the history of an imageimport      Import the contents from a tarball to create a filesystem imageinspect     Return low-level information on Docker objects

查看特定命令,请输入:

docker docker-subcommand --help

要查看有关Docker的系统信息 请使用:

docker info
  • 4.使用Docker镜像

Dock容器是从Docker镜像构建的,默认情况下,Docker从Docker Hub中提取镜像,这是一个有Docker管理的Docker镜像市场, 这是Dock的让项目背后的公司。任何人都可以在Docker Hub上托管他们的Docker镜像,所以您只需要将您的应用程序和Linux放在那边托管即可

要检查 新是否可以从Docker Hub访问和下载镜像,请输入:

docker run hello-world

输出下面的内容Docker正常工作:

latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
......

您可以使用docker带子命令的search命令搜索Docker Hub上可用的镜像。例如,要搜索Ubuntu映像,请输入:

docker search ubuntu

该脚本将对Docker Hub进行抓取,并返回名称与搜索字符串匹配的所有镜像的列表。输出将类似于:

NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 7917 [OK]
dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 193 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 156 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 93 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 87 [OK]
neurodebian NeuroDebian provides neuroscience research s… 50 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components=m… 38 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 36 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 [OK]
tutum/ubuntu Simple Ubuntu docker images with SSH access 18
i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 13
ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys… 12
1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 10 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 6 [OK]
eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 6 [OK]
codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 4 [OK]
darksheer/ubuntu Base Ubuntu Image -- Updated hourly 4 [OK]
1and1internet/ubuntu-16-apache ubuntu-16-apache 3 [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 3 [OK]
1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 1
1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0 [OK]
pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0
smartentry/ubuntu ubuntu with smartentry 0 [OK]
ossobv/ubuntu
...

在OFFICIAL列中,带OK标记的表明这个镜像由公司构建和支持。其他镜像则由个人创建。确定要使用的映像后,可以使用命令pull将其下载到计算机。

执行以下ubuntu命令将官方映像下载到您的计算机:

docker pull ubuntu

要查看已下载到计算机的镜像,请输入:

docker images

输出应类似于以下内容:

REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
hello-world latest e38bc07ac18e 2 months ago 1.85kB
  • 5.运行Docker容器

让我们使用Ubuntu 的镜像运行一个 容器 。-i和-t子命令的意思是为您提供一个对容器的交互shell访问:

docker run -it ubuntu

展示如下:

root@4af22e88dc79:/#

  • 6.管理Docker容器

查看服务器中的docker容器:

docker ps

输出类似:

CONTAINER ID IMAGE COMMAND CREATED

查看所有容器运行状态

docker ps -a

输出结果为:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4af22e88dc79 ubuntu "/bin/bash" 3 minutes ago Exited (0) About a minute ago hardcore_maxwell
bc48dc240562 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago practical_lehmann

查看最新的容器:

docker ps -l

输出结果:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4af22e88dc79 ubuntu "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago hardcore_maxwell

启动容器,使用docker start ,后跟容器id,命令入下:

docker start 4af22e88dc79

使用docker ps 查看状态:

oot@iZm5e97gc9tpdsp3b9qmhkZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4af22e88dc79 ubuntu "/bin/bash" 6 minutes ago Up 48 seconds hardcore_maxwell

要停止正在运行的容器 docker stop 后跟容器ID或者名称 hardcore_maxwell

docker stop hardcore_maxwell

删除容器 docker rm 容器名

docker rm practical_lehmann
  • 使用Docker 运行python程序

创建 dockerpyhton文件并进入文件

mkdir dockerpyhton --》 cd dockerpyhton

  1. 创建文件名为demo.py

from random import randint
min_number = int(input("请输入最小值"))
max_number = int(input("请输入最大值"))
if( max_number < min_number ):
print("输入有误")
else:
rnd_number = randint(min_number,max_number)
print(rnd_number)
  1. 编写dockerfile文件

文件名为:

FROM python
WORKDIR /demo
COPY . /demo
CMD ["python","demo.py"]
  1. 制作镜像

docker build -f ./Dockerfile -t docker_python:1.0 .
# docker_python:1.0 为镜像名
  1. 查看镜像并启动容器

docker images
docker run -it 46e1851ea636

Dockerfile 可以指定Python环境和安装requments.txt 使用build 命令来创建镜像 然后docker run启动镜像

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...