Docker

133 阅读2分钟

以下操作均基于CentOS 7.6进行

安装&卸载

1. 卸载旧版本

yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine

2. 安装所需软件包

yum install -y yum-utils

3. 设置stable镜像

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. 更新索引

yum makecache fast

5. 安装Docker ce

yum -y install docker-ce docker-ce-cli containerd.io

安装完毕后可以使用命令systemctl enable docker设置开机自启动;使用命令systemctl start docker启动Docker

6.版本查看

安装完成后可以使用命令docker version来查看所安装Docker的版本信息

7. 镜像

7.1 配置镜像加速器

可以通过修改daemon配置文件(/etc/docker/daemon.json)来使用加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF' {   "registry-mirrors": ["https://r4s7f8dr.mirror.aliyuncs.com"] } EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

7.2 拉取hello-world镜像

拉取官网hello-world镜像来检测docker是否安装无误,使用命令docker pull hello-world

7.3 运行hello-world镜像

使用命令docker run hello-world,如果输出以下信息表明镜像运行正常:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

  1. The Docker client contacted the Docker daemon.

  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

    (amd64)

  1. The Docker daemon created a new container from that image which runs the

    executable that produces the output you are currently reading.

  1. The Docker daemon streamed that output to the Docker client, which sent it

    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:

  $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:

  [https://hub.docker.com/](https://hub.docker.com/)

For more examples and ideas, visit:

  [https://docs.docker.com/get-started/](https://docs.docker.com/get-started/)

镜像拉取及启动

以下操作是在Mac系统中结合Docker Desktop客户端进行的操作

1. MySQL镜像

1.1 镜像拉取

使用命令docker pull mysql可以拉取到最新的MySQL镜像,拉取成功后可在Docker面板中看到自己本地的所有镜像信息,如下图:

image.png

1.2 启动镜像并配置用户

docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root用户的密码 -d 镜像id

参数说明:

  • docker run:运行docker容器命令
  • --name mysql:给容器设置别名
  • -p 3306:3306:将容器的3306端口映射到宿主机的3306端口
  • -e MYSQL_ROOT_PASSWORD=你的密码:初始化root用户登录密码
  • -d:后台运行容器,并返回容器的id

查看运行中的容器可以使用命令:docker ps

image.png

1.3 进入容器

使用命令docker exec -it mysql /bin/bash

image.png

1.4 登录MySQL

使用命令mysql -uroot -p输入前面配置的root用户的密码

image.png 可以在命令行操作数据库或配置NavicatePremium等客户端进行本地数据库连接了。

1.5 镜像加速

Docker默认的镜像地址是国外的,访问速度较慢,可以替换阿里云的镜像加速地址。

  • 登录阿里云平台,注意每个人会分配不同的加速地址,登录阿里云账号访问该地址即可看到。
  • 在Docker面板Preferences-Docker Engine将镜像地址registry-mirrors对应的值替换为上面自己的阿里云镜像
  • 重启Docker