docker: 构建自己的镜像

我们给予ubuntu的镜像然后拷贝python的requirement.txt文件进去,再根据这个文件安装对应的python库

拷贝文件到docker容器。首先查找对应的容器ID。然后执行命令

docker cp 文件源路径 文件目标路径

root@zhf-maple:/home/zhf/桌面# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

f98b8e77182b maple412/ubuntu:nb_test "/bin/bash" 22 seconds ago Up 17 seconds tender_rosalind



root@zhf-maple:/home/zhf/桌面# docker cp /home/zhf/docker/requirement.txt f98b8e77182b:/home/software_requirement


root@f98b8e77182b:/home/software_requirement# ls -al

total 12

drwxr-xr-x 2 root root 4096 Sep 22 06:08 .

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上也可以看到上传的镜像

docker: 构建自己的镜像_第1张图片

 

对应的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

 

转载于:https://www.cnblogs.com/zhanghongfeng/p/11569109.html

你可能感兴趣的:(docker: 构建自己的镜像)