docker容器基础

1.docker是什么

docker是一个用于构建、运行和管理容器的软件框架

利用docker可以快速传送、测试和部署代码

什么是容器?

容器可以看作一个盒子,把软件和软件依赖的各项服务,统一打包进容器中,

任何人都可以开箱即用

开发:不用考虑跨平台的细节问题

测试:不用纠结测试环境和开发环境是否一致

运维:不用关心各种语言细节

2.搭建docker环境

 推荐使用Ubuntu进行安装

2.1 虚拟机

执行一个命令:

curl -fsSL https://get.docker.com -o get-docker.sh &sudo sh get-docker.sh

2.2 云服务

腾讯云提供ubuntu+docker镜像

云服务提供:

  1. 7*24在线服务器
  2. 自动使用腾讯的docker加速

2.3 云服务器上使用docker

1.进入管理界面

2.点击容器管理、新建容器

 3.输入信息

  1. 容器名称(自定义)
  2. 容器镜像(mysql)
  3. 镜像版本(5.7)
  4. 端口映射(3306-3306)
  5. 环境变量(MYSQL_ROOT_PASSWORD: xxxxxx)

 注意:添加规则后 才能链接上mysql

3.docker技术体系

docker本质上是一个C/S软件,docker发送指令,dockerhost接收和执行指令:

  • 管理镜像
  • 管理容器
  • 管理系统资源

仓库:存放镜像

镜像:启动容器

容器:运行软件

另外一个工具:k8s

k8s是Kubernetes缩写,一般运行在生产环境,用来管理容器和容器运行过程中需要的资源

  • 服务发现和负载均衡
  • 存储编排
  • 自动发布和回滚
  • 自动启动、替换、关闭容器
  • 密钥和配置管理

k8s是一个非常专业,复杂的容器编排工具

 

4.docker常用命令

docker --help :列出所有的docker命令及其参数

4.0 免去sudo

sudo groupadd docker #创建新用户组
sudo usermod -aG docker $USER #当前用户加入用户组
newgrp docker #刷新用户组权限

4.1 镜像

关于镜像的操作,放在image子命令下面

$ docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       根据Dockerfile构建镜像
  history     查看镜像的构建步骤
  import      把一个容器的文件系统,导入为镜像
  inspect     查看镜像信息
  load        导入镜像
  ls          查看镜像
  prune       删除未使用的镜像
  pull        从仓库拉取镜像
  push        把镜像推送到仓库
  rm          删除镜像
  save        导出镜像
  tag         给镜像打tag,

Run 'docker image COMMAND --help' for more information on a command.

docker export:把容器的文件系统导入

举个例子:

增删查改

docker image pull mysql:5.7 #从仓库中拉取镜像
docker image ls #查看本地所有的镜像
docker image tag mysql:5.7 mysql:v5.7 #为镜像设置名字和tag
docker image rm mysql:v5.7 #删除镜像
docker image rm 7e6c  #删除镜像 通过image id删除

 删除失败:

  • image is referenced in multiple repositories 有多个id一样的镜像
  • image is being used 镜像正在使用

不常用的命令:

docker image save mysql:5.7 -o mysql.img #导出镜像
docker image load -i mysql.img #导入镜像

4.2 网络

默认情况下,容器之间是不联网

如果容器之间要通信,需要使用同一个网络

ubuntu@VM-24-5-ubuntu:~$ docker network --help

Usage:  docker network COMMAND

Manage networks

Commands:
  connect     让容器加入指定的网络
  create      创建网络
  disconnect  让容器退出指定的网络
  inspect     查看指定网络
  ls          列出所有网络
  prune       移除未使用的网络
  rm          移除指定的网络

Run 'docker network COMMAND --help' for more information on a command.

举例子

docker network create lgs_nat #创建网络
docker network ls #查看所有网络
docker network rm 8abc #删除指定的网络

4.3 容器

docker的命令中,容器子命令是重中之重

ubuntu@VM-24-5-ubuntu:~$ docker conrainer --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         创建并运行容器(重中之重)
  exec        进入运行中的容器里,执行命令
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.11.2)
  compose*    Docker Compose (Docker Inc., v2.21.0)
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

Commands:
  attach      将本地的输入输出,附加到容器中
  commit      根据容器及其修改,创建新的镜像
  cp          容器和服务器之间,文件传输
  create      创建新的容器(不运行)
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  export      把容器的文件系统,到处
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     查看容器信息
  kill        强行停止运行中的容器
  load        Load an image from a tar archive or STDIN
  logs        显示容器的执行日志
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  rename      Rename a container
  restart     重启容器
  rm          删除容器
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  start       启动容器
  stats       查看容器状态
  stop        停止容器
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Global Options:
      --config string      Location of client config files (default "/home/ubuntu/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context
                           set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket to connect to
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/home/ubuntu/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/ubuntu/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/ubuntu/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Run 'docker COMMAND --help' for more information on a command.

For more help on how to use Docker, head to https://docs.docker.com/go/guides/

创建容器:create

启动容器:start

停止容器:stop

重启容器:restart
删除容器:rm

restart = stop + start

run = create + start

以手动启动mysql为例,演示docker run的用法

ubuntu@VM-24-5-ubuntu:~$ docker container run  --name lgs_mysql_8.0 7e6d 
ubuntu@VM-24-5-ubuntu:~$ docker container run  --name mysql_8.0 -e MYSQL_ROOT_PASSWORD=12345678 7e6d 
ubuntu@VM-24-5-ubuntu:~$ docker container run  --name mysql_8.0 -e MYSQL_ROOT_PASSWORD=12345678 -p 3306:3306 7e6d 
ubuntu@VM-24-5-ubuntu:~$ docker container run  --name mysql_8.0 -e MYSQL_ROOT_PASSWORD=12345678 -p 3306:3306 -d 7e6d

ubuntu@VM-24-5-ubuntu:~$ docker container run \
--name mysql_8.0 \
-e MYSQL_ROOT_PASSWORD=12345678 -p 3307:3306 \
-d \
--volume /home/ubuntu/data:/var/lib/mysql \
--volume /home/ubntu/conf:/etc/mysql/conf.d \
7e6d  

--name:指定容器名字,方便后续管理 

-d:守护模式,后台运行容器

-p:端口映射,-p 服务器端口:容器端口

-e:设定环境变量

--volume:文件映射,用来持久化保存数据数据

 

进入容器执行命令:

docker container exec mysql_8.0 ls /var/lib/mysql

 

posted @ 2024-03-03 16:46  万溪汇海  阅读(5)  评论(0编辑  收藏  举报