Docker

1.安装

推荐使用下面命令 安装的为最新版本

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

查看版本信息

webrx@us:~$ docker --version
Docker version 20.10.12, build e91ed57

webrx@us:~$ docker version
Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:33 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied

 2.常用配置  免sudo执行docker常用命令

#01 建立deamon.json
$ sudo vim /etc/docker/daemon.json

#添加内容如下

{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn","https://hub-mirror.c.163.com/","https://reg-mirror.qiniu.com","https://registry.docker-cn.com"]
}

#02加载一服务重新启动一下服务
webrx@us:/$ 
webrx@us:/$ sudo systemctl reload docker
webrx@us:/$ sudo systemctl restart docker
webrx@us:/$ sudo service docker status
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2021-10-22 14:27:02 CST; 9s ago
TriggeredBy: ● docker.socket
 Docs: https://docs.docker.com
Main PID: 4984 (dockerd)
Tasks: 8
Memory: 29.5M
CGroup: /system.slice/docker.service
       └─4984 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock


#03查看 docker info可以看到相关的信息
$ docker info

Registry Mirrors:
https://docker.mirrors.ustc.edu.cn/
https://hub-mirror.c.163.com/
https://reg-mirror.qiniu.com/
https://registry.docker-cn.com/
Live Restore Enabled: false

`注意:操作docker时,必须有root组或docker组的权限,所以很多命令前需要使用sudo,如果要免sudo使用,执行如下命令:$ sudo gpasswd -a webrx docker 重新登录`
$ sudo gpasswd -a webrx docker
#在将用户“webrx”加入到“docker”组中

 镜像操作

#帮助命令

$ docker version
$ docker --version #版本信息
$ docker info  或 sudo docker info #显示docker系统信息,镜像和容器数量相关信息

#显示所有容器
webrx@us:~$ docker ps -a

删除镜像 docker rmi    删除容器是docker rm  删除所有容器 docker rm $(docker ps -aq)

镜像操作

#1 查看本地镜像,没有镜像

webrx@us:~$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE


#2 从官方hub.docker.com接取一个centos镜像案例
webrx@us:~$ docker pull centos

webrx@us:~$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   3 months ago   231MB

#运行centos镜像
webrx@us:~$ docker run -it centos
[root@b64048148642 /]# cat /etc/centos-release
CentOS Linux release 8.4.2105

#查看容器
webrx@us:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                     PORTS     NAMES
967203fb24b0   ubuntu    "bash"        49 seconds ago   Exited (0) 5 seconds ago             sweet_kalam
b64048148642   centos    "/bin/bash"   4 hours ago      Exited (0) 4 hours ago               dreamy_wilbur
webrx@us:~$ 

#拉取镜像
webrx@us:~$ docker pull ubuntu
还可以指定镜像版本
webrx@us:~$ docker pull mysql:5.6.51

Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest


#运行镜像 并查看版本信息

webrx@us:~$ docker run -it ubuntu
root@7bb2ce8af066:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

#拉取helloworld 并运行

webrx@us:~$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

webrx@us:~$ 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)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. 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/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

#删除镜像(加-f是强制删除 fe为images id 的前两位)

webrx@us:~$ docker rmi -f fe
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
webrx@us:~$ 

容器操作

#建立容器

webrx@us:~$ docker run -itd -P \--name m56 \-e MYSQL_ROOT_PASSWORD=root 0
大写P是随机分配端口号,也可以小写p指定端口
webrx@us:~$ docker run -itd -p 3307:3306 \-e MYSQL_ROOT_PASSWORD=root 0


webrx@us:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                                         NAMES
e84a9722f5ca   0         "docker-entrypoint.s…"   10 seconds ago   Up 8 seconds   0.0.0.0:3307->3306/tcp, :::3307->3306/tcp     suspicious_shamir
b43cfcf9335a   0         "docker-entrypoint.s…"   11 hours ago     Up 11 hours    0.0.0.0:49153->3306/tcp, :::49153->3306/tcp   m56

docker 安装mysql

1.拉取镜像

#默认拉取最新的
docker pull mysql

2.查看镜像

docker images

3.配置文件挂载

#将etc/mysql/my.cnf挂载到宿主机的 /usr/local/src/mysql/conf/my.cnf
/usr/local/src/mysql/conf:/etc/mysql/conf.d 


#挂载data文件
/usr/local/src/mysql/data:/var/lib/mysql 

4.启动命令

docker run -p 3306:3306 \
--name mysql \
-e MYSQL_ROOT_PASSWORD=root \
-v /usr/local/src/mysql/conf/:/etc/mysql/conf.d \
-v /usr/local/src/mysql/data:/var/lib/mysql \
--restart=on-failure:3 \
-d mysql

 4.进入容器内

#进入容器
docker exec -it mysql03 /bin/bash

#登录 默认密码是root
mysql -u ruihao -p

#创建新的用户
CREATE USER user IDENTIFIED BY 'password';

#授予权限
GRANT ALL PRIVILEGES ON *.*  TO 'user'@'%';

#刷新权限
FLUSH PRIVILEGES;

如果提示密码类型caching_sha2_password

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
 

 最好修改mysql数据库时区 在挂载的my.cnf文件

 

 

docker 安装 rabbitmq

1.拉取镜像

安装name为rabbitmq的这里是直接安装最新的,如果需要安装其他版本在rabbitmq后面跟上版本号即可

docker pull rabbitmq

2.运行mq

需要注意的是-p 5673:5672 解释:-p 外网端口:docker的内部端口 ,你们可以改成自己的外网端口号,我这里映射的外网端口是5673那么程序连接端口就是用5673(15762为管理界面的端口)

docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5673:5672 rabbitmq
#查看容器运行情况
docker ps 

 3.查看容器管理界面

#进入容器
docker exec -it 容器名称 /bin/bash

 进入容器内部后
 

#启用容器管理界面

rabbitmq-plugins enable rabbitmq_management

现在http://linuxip:15762访问管理界面

 4.用户创建

#添加新用户
rabbitmqctl add_user 用户名 密码

#设置权限
rabbitmqctl set_user_tags usr administrator[角色名称]

#查看当前用户
 rabbitmqctl list_users

root@my-rabbit:/# rabbitmqctl list_users
Listing users ...
user    tags
guest   [administrator]
usr     [administrator]

#修改密码
rabbitmqctl change_password username password

#删除用户
rabbitmqctl delete_user username

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值