docker基础
·
把应用和运行环境一起打包到容器,然后用容器运行的技术。
可以理解为把本地写的程序通过 Docker 打包成一个“安装包”,再上传到像应用商城一样的镜像仓库,别人直接拉取这个镜像并运行,就能使用你的程序。
运行链路
本地代码 → Dockerfile → 构建镜像 → 上传镜像仓库 → 别人拉取镜像 → 启动容器运行
Docker常用命令
# view local images
docker images
# pull image
docker pull image-name
# delete image
docker rmi image-id
# build image
# -f find the Dockerfile in the current directory
# -t display to label the image
# version indicates which version of this image
# . indicates build context which is the current directory
docker build -f Dockerfile --build-arg build-arg -t image-name:version .
# view running containers
docker ps
# view all containers
docker ps -a
# statt container
docker run -d \
-p local-port:container-port \
--name container-name \
--network net-name \
-e container-env-varible(KEY=value) \
-v local-directory:container-directory \
image-name:version
# stop container
docker stop container-name
# start the stopped container
docker start container-name
# restart container
docker restart container-name
# delete container
docker rm container-name
# force delete running container
docker rm -f container-name
# enter container
docker exec -it container-name /bin/bash
# if some container have not bash
docker exec -it container-name /bin/sh
# view container detailed information
docker inspect container-name
# view the usage of containers
docker stats
# view logs
docker logs container-name
# continuous tracking of logs
docker logs -f container-name
# view the last 100 lines of the log
docker logs --tail 100 container-name
# If the container is deleted, the internal data may be lost.
# so it is necessary to mount the data for preservation.
# view data volume
docker volume ls
# delete data volume
docker volume rm volume-name
# view network
docker network ls
# create network
docker network create net-name
# delete all stopped containers
docker container prune
# delete dangling images
docker image prune
# clear unnecessary data
# -a indicates clear unnecessary image
docker system prune -a
Docker Compose
一个配置文件,统一管理多个容器,就是原来一串docker run命令统一写到compose.yaml里
# order compose
docker compose up -d
# stop and delete these container
docker compose down
# view service status
docker compose ps
# view logs
docker compose logs -f
# rebuild and start
docker compose up -d --build
比如说你现在要构建一个后端项目,需要用到MySQL和Redis,compose.yaml里面就可以这样写:
# indicate what services are there, which means what containers
services:
app:
# based on the Dockerfile in the directory
build: .
container_name: myapp
ports:
- "8080:8080"
# startup sequence dependency
depends_on:
- mysql
- redis
networks:
- mynet
mysql:
# indicate to use pre-build image directly
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: testdb
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
networks:
- mynet
redis:
image: redis:7
container_name: redis
ports:
- "6379:6379"
networks:
- mynet
networks:
mynet:
volumes:
mysql_data:
Docker Compose 会自动给这一组服务建一个内部网络,放在同一个
compose.yml里的服务,默认都接到这个网络上,所以它们彼此能直接通信
Dockerfile
构建镜像的说明书,告诉Docker基于什么基础镜像,需要哪些文件,需要安装哪些依赖,容器启动执行什么命令。
# the begging of this image
FROM
# set work dir, like the cd in liunx
WORKDIR
# take local dir copy to image
COPY
# copy-plus contain automatically extract compressed packages, support remote URLs
ADD
# execute commands when building image
RUN
# execute commands by default when container start
CMD
# main execute commands by default when container start
ENTRYPOINT
# java -jar app.jar
ENTRYPOINT ["java", "-jar"]
CMD ["app.jar"]
# set environment var
ENV
# declare the port in the internal container
EXPOSE
RUN 用于镜像构建阶段,通常执行安装依赖、编译构建等操作;
CMD 用于容器启动阶段,指定容器默认启动命令。
数据持久化
解决问题:容器删除了,数据怎么处理
why?
容器一般是临时的,其内部文件系统内数据,随着容器的删除也删除了。像数据库这类文件、上传日志等不能只放容器内部,必须挂载到容器外面保存。
bind mount
直接挂载宿主目录机
-v /local/mysql:/var/lib/mysql
左侧是电脑上目录,右侧是容器内MySQL存数据位置。主要是你管路径,可以通过cd进去看。更适合开发环境使用。
volume
挂载在Docker自己管理的存储空间
-v mysql:/var/lib/mysql
左侧mysql是一个Docker volume,Docker自己维护管理,更适合生产使用,迁移更方便。
更多推荐
所有评论(0)