咱們給予ubuntu的鏡像而後拷貝python的requirement.txt文件進去,再根據這個文件安裝對應的python庫python
拷貝文件到docker容器。首先查找對應的容器ID。而後執行命令sql
docker cp 文件源路徑 文件目標路徑docker
root@zhf-maple:/home/zhf/桌面# docker psubuntu
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESbash
f98b8e77182b maple412/ubuntu:nb_test "/bin/bash" 22 seconds ago Up 17 seconds tender_rosalindui
root@zhf-maple:/home/zhf/桌面# docker cp /home/zhf/docker/requirement.txt f98b8e77182b:/home/software_requirementthis
root@f98b8e77182b:/home/software_requirement# ls -alspa
total 123d
drwxr-xr-x 2 root root 4096 Sep 22 06:08 .code
drwxr-xr-x 4 root root 4096 Sep 22 06:08 ..
-rw-r--r-- 1 root root 2058 Sep 22 06:03 requirement.txttxt
root@f98b8e77182b:/home/software_requirement# python3 install -r requirement.txt
保存鏡像:
Docker ps -l找到最近一次修改的容器id。
root@zhf-maple:/home/zhf/桌面# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f98b8e77182b maple412/ubuntu:nb_test "/bin/bash" 17 minutes ago Exited (1) 10 minutes ago tender_rosalind
而後使用docker commit 容器ID 鏡像名就能夠生成鏡像了
root@zhf-maple:/home/zhf/docker# docker commit f98b8e77182b maple412/ubuntu:test
sha256:bca747cf9c55617d802d9e1633c6d70149959caef49af9a44f6d0a4b840c6c96
此時查看鏡像就有了咱們生成的鏡像
root@zhf-maple:/home/zhf/docker# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
maple412/ubuntu test bca747cf9c55 19 seconds ago 522MB
maple412/ubuntu nb_test 1a2a83944331 8 months ago 521MB
經過docker login -u xx -p xx 登陸docker後就能夠上傳就成功了
root@zhf-maple:/home/zhf/docker# docker push maple412/ubuntu:test
The push refers to repository [docker.io/maple412/ubuntu]
21b2d81ef223: Pushed
df28f5ba1b2a: Pushed
2c77720cf318: Layer already exists
1f6b6c7dc482: Layer already exists
c8dbbe73b68c: Layer already exists
2fb7bfc6145d: Layer already exists
test: digest: sha256:0a0ecefa6226f7cb22bf8387ec2ac766ab6c958a9b38cdeecc0063da85d2e6f8 size: 1573
docker hub上也能夠看到上傳的鏡像
對應的Dockerfile以下:
FROM ubuntu
WORKDIR /home/software_requirement
COPY ./requirement.txt /home/software_requirement
RUN apt-get update && apt-get install python3-pip --assume-yes
RUN pip3 install -r requirement.txt
這裏有2點須要注意下:
1 這裏的COPY命令,源路徑要寫相對路徑。也就是requirement.txt相對於Dockerfile的位置,不然會提示找不到源路徑位置
2 在使用apt-get install的時候會遇到以下錯誤,提示是否須要安裝,而後自動退出
Step 5/7 : RUN apt-get install python3.6
---> Running in c96a012485da
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
file libexpat1 libmagic-mgc libmagic1 libmpdec2 libpython3.6-minimal
libpython3.6-stdlib libreadline7 libsqlite3-0 libssl1.1 mime-support
python3.6-minimal readline-common xz-utils
Suggested packages:
python3.6-venv python3.6-doc binutils binfmt-support readline-doc
The following NEW packages will be installed:
file libexpat1 libmagic-mgc libmagic1 libmpdec2 libpython3.6-minimal
libpython3.6-stdlib libreadline7 libsqlite3-0 libssl1.1 mime-support
python3.6 python3.6-minimal readline-common xz-utils
0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded.
Need to get 6580 kB of archives.
After this operation, 33.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c apt-get install python3.6' returned a non-zero code: 1
解決辦法就是在命令最後加上--assume-yes
RUN apt-get update && apt-get install python3-pip --assume-yes