docker的数据位置及安装位置
- docker默认的安装位置是在:“C:\Program Files\Docker”
- 数据位置,包括镜像位置(wsl2)及docker桌面的位置是在:C:\Users\Administrator\AppData\Local\Docker
- 由于docker所耗存储是大的,且随镜像的增多而增大,所以不能放在C盘,但是: 网上直接迁移docker使用的wsl2所在位置,或者直接软链数据位置(或\wsl\data):都将使docker desktop无法打开或闪退
- 遂在安装之前,直接将上面两个目录都完整软链;然后再安装docker desktop
1
2
3
4
| mklink /j "C:\Users\Administrator\AppData\Local\Docker" "F:\Docker"
mklink /j "C:\Program Files\Docker" "F:\Program Files\Docker"
mklink /j "C:\Users\Administrator\AppData\Roaming\Docker" "F:\Roaming\Docker"
mklink /j "C:\Users\Administrator\AppData\Roaming\Docker Desktop" "F:\Roaming\Docker Desktop"
|
- 隔了几天,好像又闪退,无语
- 感觉和电脑配置以及镜像空间占用大小有关系,面向玄学编程
mysql
1
| docker run -p 3307:3306 --name mysql --restart=always --privileged=true -v /g/docker/mysql/data:/var/lib/mysql -v /g/docker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=来个密码 -d mysql
|
- 兼容之前版本的密码认证,需要进入容器,root登录mysql;做以下操作,主机客户端才能连接
1
2
3
4
5
6
| CREATE USER '来个用户'@'%' IDENTIFIED BY '来个密码';
GRANT ALL ON *.* TO '上面的用户'@'%';
ALTER USER '上面的用户'@'%' IDENTIFIED BY '来个密码' PASSWORD EXPIRE NEVER;
# 可以修改密码
# ALTER USER '上面的用户'@'%' IDENTIFIED WITH mysql_native_password BY '换个密码';
flush privileges;
|
- 配置文件
/g/docker/mysql/conf/my.cnf
1
2
3
4
5
6
| chmod -R 644 /g/docker/mysql/conf/my.cnf
[mysqld]
max_connections=1600
secure_file_priv=/var/lib/mysql
skip_ssl
|
PS:如果出现下面这个错误,等一会就行
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
redis
1
| docker run -p 6380:6379 --name redis --restart=always -v /g/docker/redis/conf:/etc/redis/redis.conf -v /g/docker/redis/data:/data -v /g/docker/redis/persist_data:/persist_data -d redis redis-server /etc/redis/redis.conf/redis.conf
|
确保主机中的挂载目录存在
redis-server 指定的是文件,而非目录
其中redis的配置G:\docker\redis\conf\redis.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| chmod -R 644 /g/docker/redis/conf/redis.conf
# 注释掉这部分,使redis可以外部访问;否则外部访问将报错EOF
# bind 127.0.0.1
# 用守护线程的方式启动
daemonize no
# 给redis设置密码
requirepass q123456we
# AOF持久化开启,默认是no
appendonly yes
# 更改本地redis数据库存放文件夹(可选)
dir /persist_data
# 防止出现远程主机强迫关闭了一个现有的连接的错误,默认是300
tcp-keepalive 300
# AOF持久化配置
appendfsync everysec
# RDB持久化关闭,默认是no
save ""
|
dockerfile 构建
目录
1
2
3
4
5
6
7
8
9
10
11
12
| ➜ /workspace tree .
.
├── Dockerfile
├── download
│ ├── go1.18.3.linux-amd64.tar.gz
│ ├── protoc
│ │ ├── bin
│ │ ├── include
│ │ └── readme.txt
│ └── protoc.tar.gz
├── run.sh
└── zshrc_tmp
|
命令(注意最后的点)
1
| docker build -f Dockerfile -t workspace:go .
|
dockerfile 部分代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| FROM ubuntu:18.04
LABEL Author="yiwei.duan@outlook.com" Description="This image is ubuntu:20.04 with common software." Version="1.0"
WORKDIR /workspace
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" > /etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list
RUN apt-get update \
&& apt-get install -y openssh-server \
&& mkdir /var/run/sshd \
&& echo 'root:来个密码' | chpasswd \
&& echo "PermitRootLogin no " >> /etc/ssh/sshd_config \
&& echo "PasswordAuthentication yes " >> /etc/ssh/sshd_config \
&& echo "PubkeyAuthentication yes " >> /etc/ssh/sshd_config \
&& echo "来个新用户名 ALL=(ALL) ALL" >> /etc/sudoers
ADD run.sh /
RUN chmod +x /run.sh
CMD ["/run.sh"]
|
TIPS:
出现 unexpected EOF,可能是电脑或者网络不太行,直接继续执行之前的命令。
如果,emmm,比如,A容器使用过了2222端口进行了ssh连接,但之后将A删除了,新建B容器,也使用2222端口,需要将~/.ssh/known_hosts中的那条 [127.0.0.1]:2222删除
dockerfile中使用CMD,如下,容器无法启动;但sh文件单独执行是成功的,这时将run.sh中最后追加 " su root “即可
1
2
3
| ADD run.sh /
RUN chmod +x /run.sh
CMD ["/run.sh"]
|
dockerfile中无交互生成ssh,并将本地公钥写进autthorized_key
1
2
3
4
5
| RUN ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa \
&& touch ~/.ssh/authorized_keys \
&& chmod 600 ~/.ssh/authorized_keys \
&& chmod 700 ~/.ssh \
&& echo "主机公钥,位置在~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
|
dockerfile中生成登录git的ssh
1
2
3
| RUN ssh-keygen -t rsa -C "github邮箱" -P "" -f ~/.ssh/docker_rsa \
&& touch ~/.ssh/config \
&& echo "Host github.com\n\tHostName github.com\n\tPreferredAuthentications publickey\n\tIdentityFile ~/.ssh/docker_rsa\n\tUser duan1v" >> ~/.ssh/config
|
- 到github中 New SSH key
- 测试连接git
主机使用vscode;配置~/.ssh/config
1
2
3
4
| Host 来个名字
HostName 127.0.0.1
User docker用户名
Port docker端口
|
dockerfile无交互创建新用户
1
2
3
| RUN useradd 来个用户名 -m \
&& echo "来个密码\n重复密码" | passwd 上面那个用户名 \
&& usermod -a -G 上面那个用户名,sudo 上面那个用户名
|
docker 推送镜像
- 先创建仓库,创建之后可以看到,仓库名 是以"docker用户名/“为前缀的,还有一部分是自定义的,记住这个仓库名;自定义部分可以取,需要推送的那个镜像名就好,不用加TAG
https://hub.docker.com/repositories
1
2
3
| PS C:\Users\Administrator\Desktop> docker login
Authenticating with existing credentials...
Login Succeeded
|
- 将需要上传的 镜像A:A的TAG 重新生成一个与仓库名同名的镜像
1
| docker tag 镜像A:A的TAG 仓库名:A的TAG
|
容器使用主机的vpn,dockerfile中添加
1
2
| ENV https_proxy="http://host.docker.internal:主机的代理端口" \
http_proxy="http://host.docker.internal:主机的代理端口" \
|