docker, docker-compose安装和使用
官方文档:
安装docker:
https://docs.docker.com/install/linux/docker-ce/centos/
安装docker-compose:
https://docs.docker.com/compose/install/
# 所有docker命令映射都是:local:container # create container docker run --name <name> -p 123:123 -d <image> -v local_path:container_path # if failed, see logs docker logs <name/id> # see how many docker containers docker ps -a # see networks docker network ls # see network detail docker network inspect <name/id> # enter a container docker exec -ti <name/id> bash # start docker after rebooting docker start <name/id> # check status docker stats [name/id] # delete container docker rm <name/id> docker container rm $(docker container ls -a -q) # Remove all containers # image docker build docker image ls -a # List all images on this machine docker image rm <image id> # Remove specified image from this machine docker image rm $(docker image ls -a -q) # Remove all images from this machine docker login # Log in this CLI session using your Docker credentials docker tag <image> username/repository:tag # Tag <image> for upload to registry docker push username/repository:tag # Upload tagged image to registry docker run username/repository:tag # Run image from a registry
# 4.删除images,通过image的id来指定删除谁 docker rmi <image id> # 想要删除untagged images,也就是那些id为<None>的image的话可以用 docker rmi $(docker images | grep "^<none>" | awk "{print $3}") # 要删除全部image的话 docker rmi $(docker images -q)
docker run 命令解析:
https://www.runoob.com/docker/docker-run-command.html
学习:
https://docs.docker.com/get-started/part3/
docker-compose.yaml例子
version: '3' services: nginx: image: nginx:latest # 端口映射 ports: - "80:80" # 依赖关系 先跑php depends_on: - "php" # 数据卷 volumes: # 映射主机./conf.d目录到容器/etc/nginx/conf.d目录 - "$PWD/conf.d:/etc/nginx/conf.d" - "$PWD/html:/usr/share/nginx/html" networks: - app_net # 容器名称 container_name: "compose-nginx" php: build: ./php-mysqli # image指定build Dockerfile生成镜像的名称 image: php:7.2-fpm-mysqli ports: - "9000:9000" volumes: - "$PWD/html:/var/www/html" networks: - app_net container_name: "compose-php" mysql: image: mysql:5.7 ports: - "3306:3306" # 环境变量 environment: # mysql密码 - MYSQL_ROOT_PASSWORD={自己填密码} networks: app_net: # 固定子网ip,网段必须在子网络10.10.*.* ipv4_address: 10.10.10.1 container_name: "compose-mysql" networks: # 配置docker network app_net: driver: bridge ipam: config: # 子网络 - subnet: 10.10.0.0/16
docker-compose up