Docker 安装与配置
条评论Installation
Install Docker Engine on Ubuntu | Docker Documentation
1 | sudo apt-get remove docker \ |
apt 换源 (可选)
ubuntu | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
1 | 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 |
修改/etc/apt/sources.list
为以上源
Docker Compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process:
- Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere. - Define the services that make up your app in
docker-compose.yml
so they can be run together in an isolated environment. - Run
docker compose up
and the Docker compose command starts and runs your entire app. You can alternatively rundocker-compose up
using the docker-compose binary.
用Compose运行多个容器,服务的运行通过YAML
文件配置,
1 | version: '3' |
不同的服务就是不同的镜像的容器,运行compose,会根据services创建相关的镜像,运行后,每个镜像的实例开启,构成一个project的运行环境。
- 每个容器间如何访问呢?project开始执行后,每个容器都产生一个containerID,这个ID会在每个容器的/etc/hosts中记录下该ID号对应的docker虚拟网络中IP地址
commands
- docker compose down/up/stop/start 等命令执行时会通过本地路径查找docker-compose.yml配置文件
- compose 创建容器的命令规则:
- 如果service的image来自远程的镜像源,名称为
image:label
- 如果通过本地Dockerfile构建,则名称来格式
project_name:service_name
- 如果service的image来自远程的镜像源,名称为
Uninstall Docker Engine
Uninstall the Docker Engine, CLI, Containerd, and Docker Compose packages:
1
$ sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
1
2$ sudo rm -rf /var/lib/docker
$ sudo rm -rf /var/lib/containerd
You must delete any edited configuration files manually.
Dockerfile
1 | # For more information, please refer to https://aka.ms/vscode-docker-python |
From: 基于某一镜像创建镜像
WORKDIR:project在微系统中的工作目录,不指定则为“/”目录
EXPOSE:表示允许暴露微系统中的指定端口号
COPY:因为服务是微系统中运行,因此要将工作目录的文件复制到微服务
RUN:构建镜像时,执行的命令,命令能否执行取决于基于的系统是否支持该命令
CMD:镜像构建完后,需要在容器中执行的命令。
RUN和CMD的区别在于后者是在镜像构建后执行的命令,此时镜像已经运行了,前者相当于在构建镜像。
Issue
1. docker容器中执行命令不能联网,通过重启docker服务解决
通过重启docker服务解决
1 | sudo systemctl restart docker.service |
RUN pip install -r requirements.txt
不成功可能由于镜像源
2. Docker 安装错误
可能原因是apt错误
3. Docker error: HTTP 408 response body: invalid character ‘<’ looking for beginning of value
- Docker源的问题
- 镜像或标签错误
4. OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: “bash”: executable file not found in $PATH: unknown
在不同的发行版中支持的shell不同,如果bash
不能执行,可能可以切换成sh
- 本文链接:https://blog.charjin.top/2023/04/22/linux/docker-installation/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!
分享