Docker快速上手
Docker 简介和安装 - Docker 快速入门
导出程序依赖包
pip freeze > requirements.txt
1、Docker安装:
apt-get install docker
# 或
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2、Docker换源
打开/etc/docker/daemon.json文件,添加如下内容:
sudo gedit /etc/docker/daemon.json
{"registry-mirrors":["https://registry.docker-cn.com"]}
systemctl restart docker
3、建立docker组用户
sudo groupadd docker
sudo usermod -aG docker $USER
退出当前终端并重新登录。
4、编写Dockerfile文件
在项目同级目录下创建Dockerfile文件,格式如下:
# 创建Dockerfile文件
vim Dockerfile
# 写入如下内容
# 基于的基础镜像,pyhton版本,可根据需求修改
FROM python:3.7
# 将代码添加到code文件夹
COPY ./docker_test /code
# 镜像工作目录,设置code文件夹是工作目录
WORKDIR /code
RUN pip install -r requirments.txt
CMD ["python","test.py"]
4、制作镜像
# Dockerfilepath:Dockerfile所在文件夹名称,当前目录为"."
docker build -t imagename Dockerfilepath
基于容器创建镜像
docker commit 镜像所在容器id 需保存的镜像的名字:版本号
5、运行镜像
# c_name为容器名,imagename为打包的镜像名,-p 9011:9011映射端口号
docker run -p 9011:9011 -d --name c_name imagename
6、Docker 删除空白TAG和REPOSITORY的镜像
zxh@zxh-Precision-3640-Tower:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask 1.0 1c808720febd 22 hours ago 6.24GB
66507636af96 5 days ago 591MB
91720f8a2e06 5 days ago 591MB
python 3.7 869a8debb0fd 13 days ago 903MB
zxh@zxh-Precision-3640-Tower:~$ sudo docker rmi 66507636af96
Error response from daemon: conflict: unable to delete 66507636af96 (must be forced) - image is being used by stopped container bc025caf0a9b
zxh@zxh-Precision-3640-Tower:~$ sudo docker stop bc025caf0a9b
bc025caf0a9b
zxh@zxh-Precision-3640-Tower:~$ sudo docker rm bc025caf0a9b
bc025caf0a9b
zxh@zxh-Precision-3640-Tower:~$ sudo docker rmi 66507636af96
Deleted: sha256:66507636af96e6dd25068e994632f9277eac2316029912ae5c17bb5e829216c0
Deleted: sha256:725d324287d1252a750573fd6296dabd7515a2dbe17bbdbdc6ba74887e8f9b8e
Deleted: sha256:4f428d8125f3337d33480a9e7e9cef3d8e6ddfa5dcf7bbb6e83d54551f572be8
zxh@zxh-Precision-3640-Tower:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask 1.0 1c808720febd 22 hours ago 6.24GB
91720f8a2e06 5 days ago 591MB
python 3.7 869a8debb0fd 13 days ago 903MB