Article directory
Preface
Docker installation and use
1. What is docker
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable image, and then publish it to any popular Linux or Windows operating system machine, which can also be virtualized. Containers completely use the sandbox mechanism and will not have any interfaces with each other.
2. The difference between docker and virtual machines
Virtual machine: A virtual machine uses a Hypervisor (virtual machine management system, common ones include VMWare workstation, VirtualBox) to virtualize virtual hardware such as network cards, CPUs, and memory, and then build virtual machines on top of them. Each virtual machine is an independent operation. The system has its own system kernel.
Containers: Containers use namespace to isolate file systems, processes, networks, devices and other resources, and use cgroups to limit permissions and CPU resources. Ultimately, containers do not affect each other and containers cannot affect the host.
3. Docker installation (centos series installation)
1. Install docker command
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
2. Start docer and set it to start automatically at boot
systemctl start docker
systemctl enable docker
3.docker configuration acceleration
Modify the /etc/docker/daemon.json file and add the registry-mirrors key value; register with Alibaba Cloud to get your own accelerator
{
.
"registry-mirrors": ["https://registry.docker-cn.com"]
}
4.docker installation verification
docker --version
4. Docker use
1.Docker commonly used commands
指令 说明
docker images 查看已下载的镜像
docker rmi 镜像名称:标签名 删除已下载的镜像
docker search 镜像 从官方仓库(hub.docker.com)查找镜像
docker pull 镜像名称:标签名 标签名默认是 latest,代表最新版本。
docker run 创建容器
docker ps 列出运行中的容器(运行中)
docker ps -a 列出所有的容器(运行、未运行)
docker rm 容器名称 删除停止的容器
docker rm -f 容器名称 删除运行中的容器
docker start 容器名称 启动容器
docker stop 容器名称 停止容器
docker restart 容器名称 重启容器
docker exec 执行容器中的指令
docker logs -f 容器名称 查看容器日志
2.docker parameters
1. Common commands
docker run --name 容器名称 -d -p 主机端口:容器内端口 -e 环境变量 -v localpath:dockerpath --link 其它容器名:容器中别名 镜像名称:标签名
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
--name="XXXX": 为容器指定一个名称;
-e username="XXXX": 设置环境变量;
-m :设置容器使用内存最大值;
--link=[]: 添加链接到另一个容器;当要访问其它容器中的内容时,需要先链接才可以使用
-p: 开放一个端口或一组端口
-v: 映射容器的卷
2. Basic command usage
Start container
docker run -d --name jenkins -v /root/xvdd:/var/jenkins_home -p 8888:8080 -p 50001:50000 jenkins/jenkins:lates
View log
docker logs -f jenkins
Command interaction
docker exec -it --user root jenkins bash
Copy files into container
docker cp test.py jenkins:/root
3. Use of network commands
View default network
docker network ls
View network details
docker network inspect 网络id
Create new network
docker network create -d 网络类型 新网络名称
Use new network
docker run -d -it --network test_network --name network_test1 jenkins/jenkins
delete network
docker network rm test_network
5. Use Dockerfile to customize the image
Dockerfile is a text file that contains instructions (Instructions) . Each instruction builds a layer, so the content of each instruction describes how the layer should be built.
Create Dockerfile
FROM ubuntu:16.04
# 复制国内更新源
COPY sources.list /etc/apt/sources.list
# 跳转到指定目录
WORKDIR /home
# 复制文件到容器
COPY requirements.txt .
# 安装curl、python3、pip3以及flask用的包
RUN apt-get update \
&& apt-get install python3 -y && apt-get install curl -y \
&& apt-get install python3-pip -y \
&& pip3 install --upgrade pip \
&& apt-get --purge remove python3-pip -y \
&& curl https://bootstrap.pypa.io/pip/3.5/get-pip.py -o get-pip.py \
&& python3.5 get-pip.py \
&& pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
# 复制flask示例py文件
COPY main.py .
# 对外暴露端口
EXPOSE 8899
# 运行测试项目
CMD ["python3","main.py"]
Files used
to build the image
docker build -t ubuntu_flask:2.0 . -f Dockerfile_3
Start the service:
Command summary:
# Base images 基础镜像
FROM centos
#MAINTAINER 维护者信息
MAINTAINER xxxxxx
#ENV 设置环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#ADD 文件放在当前目录下,拷过去会自动解压
ADD nginx-1.8.0.tar.gz /usr/local/
ADD epel-release-latest-7.noarch.rpm /usr/local/
#RUN 执行以下命令
RUN rpm -ivh /usr/local/epel-release-latest-7.noarch.rpm
RUN yum install -y wget lftp gcc gcc-c++ make openssl-devel pcre-devel pcre && yum clean all
RUN useradd -s /sbin/nologin -M www
#WORKDIR 相当于cd
WORKDIR /usr/local/nginx-1.8.0
RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-pcre && make && make install
RUN echo "daemon off;" >> /etc/nginx.conf
#EXPOSE 映射端口
EXPOSE 80
#CMD 运行以下命令
CMD ["nginx"]
6. Installation and use of docker-compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application requires. Then, using a single command, you can create and start all services from the YML file configuration
1.docker-compose installation
Download binary files from github
https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64
Put it in the /usr/local/bin/ path and rename it to add executable permissions
mv docker-compose-linux-x86_64 docker-compose
chmod +x docker-compose
2.dockr-compose command
docker-compose up
用于部署一个 Compose 应用
默认情况下该命令会读取名为 docker-compose.yml 或 docker-compose.yaml 的文件
当然用户也可以使用 -f 指定其他文件名。通常情况下,会使用 -d 参数令应用在后台启动
docker-compose stop
停止 Compose 应用相关的所有容器,但不会删除它们
被停止的应用可以很容易地通过 docker-compose restart 命令重新启动
docker-compose rm
用于删除已停止的 Compose 应用
它会删除容器和网络,但是不会删除卷和镜像
docker-compose restart
重启已停止的 Compose 应用
如果用户在停止该应用后对其进行了变更,那么变更的内容不会反映在重启后的应用中,这时需要重新部署应用使变更生效
docker-compose ps
用于列出 Compose 应用中的各个容器
输出内容包括当前状态、容器运行的命令以及网络端口
docker-compose down
停止并删除运行中的 Compose 应用
它会删除容器和网络,但是不会删除卷和镜像
3.docker-compose.yml file configuration
1. image
services:
web:
image: hello-world
在 services 标签下的第二级标签是 web,这个名字是用户自己自定义,它就是服务名称。 image 则是指定服务的镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。 例如下面这些格式都是可以的:
image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd
2. build
服务除了可以基于指定的镜像,还可以基于一份 Dockerfile,在使用 up 启动之时执行构建任务,这个构建标签就是 build,它可以指定 Dockerfile 所在文件夹的路径。Compose 将会利用它自动构建这个镜像,然后使用这个镜像启动服务容器。
build: /path/to/build/dir
也可以是相对路径,只要上下文确定就可以读取到 Dockerfile。
build: ./dir
设定上下文根目录,然后以该目录为准指定 Dockerfile。
build:
context: ../
dockerfile: path/of/Dockerfile
注意 build 都是一个目录,如果你要指定 Dockerfile 文件需要在 build 标签的子级标签中使用 dockerfile 标签指定,如上面的例子。 如果你同时指定了 image 和 build 两个标签,那么 Compose 会构建镜像并且把镜像命名为 image 后面的那个名字。
build: ./dir
image: webapp:tag
既然可以在 docker-compose.yml 中定义构建任务,那么一定少不了 arg 这个标签,就像 Dockerfile 中的 ARG 指令,它可以在构建过程中指定环境变量,但是在构建成功后取消,在 docker-compose.yml 文件中也支持这样的写法:
build:
context: .
args:
buildno: 1
password: secret
下面这种写法也是支持的,一般来说下面的写法更适合阅读。
build:
context: .
args:
- buildno=1
- password=secret
与 ENV 不同的是,ARG 是允许空值的。例如:
args:
- buildno
- password
这样构建过程可以向它们赋值。
注意:YAML 的布尔值(true, false, yes, no, on, off)必须要使用引号引起来(单引号、双引号均可),否则会当成字符串解析。
3. command
使用 command 可以覆盖容器启动后默认执行的命令。
command: bundle exec thin -p 3000
也可以写成类似 Dockerfile 中的格式:
command: [bundle, exec, thin, -p, 3000]
4.container_name
前面说过 Compose 的容器名称格式是:<项目名称><服务名称><序号> 虽然可以自定义项目名称、服务名称,但是如果你想完全控制容器的命名,可以使用这个标签指定:
container_name: app
这样容器的名字就指定为 app 了。
5.depends_on
在使用 Compose 时,最大的好处就是少打启动命令,但是一般项目容器启动的顺序是有要求的,如果直接从上到下启动容器,必然会因为容器依赖问题而启动失败。 例如在没启动数据库容器的时候启动了应用容器,这时候应用容器会因为找不到数据库而退出,为了避免这种情况我们需要加入一个标签,就是 depends_on,这个标签解决了容器的依赖、启动先后的问题。 例如下面容器会先启动 redis 和 db 两个服务,最后才启动 web 服务:
version: '2'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
注意的是,默认情况下使用 docker-compose up web 这样的方式启动 web 服务时,也会启动 redis 和 db 两个服务,因为在配置文件中定义了依赖关系。
6.dns
和--dns参数一样用途,格式如下:
dns: 8.8.8.8
也可以是一个列表:
dns:
- 8.8.8.8
- 9.9.9.9
此外 dns_search 的配置也类似:
dns_search: example.com
dns_search:
- dc1.example.com
- dc2.example.com
7. tmpfs
挂载临时目录到容器内部,与 run 的参数一样效果:
tmpfs: /run
tmpfs:
- /run
- /tmp
8. expose
这个标签与Dockerfile中的EXPOSE指令一样,用于指定暴露的端口,但是只是作为一种参考,实际上docker-compose.yml的端口映射还得ports这样的标签。
expose:
- "3000"
- "8000"
9. external_links
在使用Docker过程中,我们会有许多单独使用docker run启动的容器,为了使Compose能够连接这些不在docker-compose.yml中定义的容器,我们需要一个特殊的标签,就是external_links,它可以让Compose项目里面的容器连接到那些项目配置外部的容器(前提是外部容器中必须至少有一个容器是连接到与项目内的服务的同一个网络里面)。 格式如下:
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
10. extra_hosts
添加主机名的标签,就是往/etc/hosts文件中添加一些记录,与Docker client的--add-host类似:
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
启动之后查看容器内部hosts:
162.242.195.82 somehost
50.31.209.229 otherhost
复制Error复制成功...
11. logging
这个标签用于配置日志服务。格式如下:
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
默认的driver是json-file。只有json-file和journald可以通过docker-compose logs显示日志,其他方式有其他日志查看方式,但目前Compose不支持。对于可选值可以使用options指定。 有关更多这方面的信息可以阅读官方文档: https://docs.docker.com/engine/admin/logging/overview/
12. ports
映射端口的标签。 使用HOST:CONTAINER格式或者只是指定容器的端口,宿主机会随机映射端口。
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
13. volumes
挂载一个目录或者一个已存在的数据卷容器,可以直接使用 [HOST:CONTAINER] 这样的格式,或者使用 [HOSTCONTAINERro] 这样的格式,后者对于容器来说,数据卷是只读的,这样可以有效保护宿主机的文件系统。 Compose的数据卷指定路径可以是相对路径,使用.或者..来指定相对目录。 数据卷的格式可以是下面多种形式:
volumes:
// 只是指定一个路径,Docker 会自动在创建一个数据卷(这个路径是容器内部的)。
- /var/lib/mysql
// 使用绝对路径挂载数据卷
- /opt/data:/var/lib/mysql
// 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。
- ./cache:/tmp/cache
// 使用用户的相对路径(~/ 表示的目录是 /home/<用户目录>/ 或者 /root/)。
- ~/configs:/etc/configs/:ro
// 已经存在的命名的数据卷。
- datavolume:/var/lib/mysql
如果你不使用宿主机的路径,你可以指定一个volume_driver。
volume_driver: mydriver
14. volumes_from
从其它容器或者服务挂载数据卷,可选的参数是 :ro或者 :rw,前者表示容器只读,后者表示容器对数据卷是可读可写的。默认情况下是可读可写的。
volumes_from:
- service_name
- service_name:ro
- container:container_name
- container:container_name:rw
15. network_mode
网络模式,与Docker client的--net参数类似,只是相对多了一个service:[service name] 的格式。 例如:
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
可以指定使用服务或者容器的网络
16. networks
加入指定网络,格式如下:
services:
some-service:
networks:
- some-network
- other-network
关于这个标签还有一个特别的子标签aliases,这是一个用来设置服务别名的标签,例如:
services:
some-service:
networks:
some-network:
aliases:
- alias1
- alias3
other-network:
aliases:
- alias2
相同的服务可以在不同的网络有不同的别名
4. Use docker-compose
Service: an application container
. Project: a complete business unit composed of a set of associated application containers.
It can be seen that a project can be associated with multiple services (containers), and Compose manages it for projects.
Deploy flask+mysql+redis service
Create three folders and docker-compose.yml file:
flask build image file:
configure docker-compose.yml file
version: '3'
services:
redis:
image: redis
expose:
- 6379
ports:
- "6399:6379"
mysql:
image: mysql:5.7
expose:
- 3306
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: python
MYSQL_USER: root
volumes:
- ./02_mysql:/var/lib/mysql
flask:
build: ./01_flask
volumes:
- ./01_flask/project:/home/project
ports:
- "8883:5000"
depends_on:
- mysql
- redis
Execute the command in this path
docker-compose up
Stop and delete the container
Summarize
Docker's daily deployment service is very convenient. The startup speed is within seconds. It is lightweight and fast for virtual machines. Moreover, a docker image can start multiple containers and build a customized image that you need. All platforms can be used smoothly.