docker的数据位置及安装位置
- docker默认的安装位置是在:”C:\Program Files\Docker”
- 数据位置,包括镜像位置(wsl2)及docker桌面的位置是在:C:\Users\Administrator\AppData\Local\Docker
- 由于docker所耗存储是大的,且随镜像的增多而增大,所以不能放在C盘,但是: 网上直接迁移docker使用的wsl2所在位置,或者直接软链数据位置(或\wsl\data):都将使docker desktop无法打开或闪退
- 遂在安装之前,直接将上面两个目录都完整软链;然后再安装docker desktop
| mklink /j "C:\Users\Administrator\AppData\Local\Docker" "F:\Docker" |
| mklink /j "C:\Program Files\Docker" "F:\Program Files\Docker" |
mysql
docker pull mysql
docker run -p 3307:3306 --name mysql --privileged=true -v /g/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=来个密码 -d mysql
- 兼容之前版本的密码认证,需要进入容器,root登录mysql;做以下操作,主机客户端才能连接
| 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; |
PS:如果出现下面这个错误,等一会就行
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
redis
docker pull redis
docker run -p 6380:6379 --name redis -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
| |
| |
| |
| daemonize no |
| |
| requirepass q123456we |
| |
| appendonly yes |
| |
| dir /persist_data |
| |
| tcp-keepalive 300 |
| |
| appendfsync everysec |
| |
| save "" |
dockerfile 构建
目录
| ➜ /workspace tree . |
| . |
| ├── Dockerfile |
| ├── download |
| │ ├── go1.18.3.linux-amd64.tar.gz |
| │ ├── protoc |
| │ │ ├── bin |
| │ │ ├── include |
| │ │ └── readme.txt |
| │ └── protoc.tar.gz |
| ├── run.sh |
| └── zshrc_tmp |
命令(注意最后的点)
docker build -f Dockerfile -t workspace:go .
dockerfile 部分代码
| 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 “即可
| ADD run.sh / |
| RUN chmod +x /run.sh |
| CMD ["/run.sh"] |
dockerfile中无交互生成ssh,并将本地公钥写进autthorized_key
| 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
| 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
ssh -T git@github.com
主机使用vscode;配置~/.ssh/config
| Host 来个名字 |
| HostName 127.0.0.1 |
| User docker用户名 |
| Port docker端口 |
dockerfile无交互创建新用户
| RUN useradd 来个用户名 -m \ |
| && echo "来个密码\n重复密码" | passwd 上面那个用户名 \ |
| && usermod -a -G 上面那个用户名,sudo 上面那个用户名 |
docker 推送镜像
- 先创建仓库,创建之后可以看到,仓库名 是以”docker用户名/“为前缀的,还有一部分是自定义的,记住这个仓库名;自定义部分可以取,需要推送的那个镜像名就好,不用加TAG
hub.docker.com/repositories
| PS C:\Users\Administrator\Desktop> docker login |
| Authenticating with existing credentials... |
| Login Succeeded |
- 将需要上传的 镜像A:A的TAG 重新生成一个与仓库名同名的镜像
docker tag 镜像A:A的TAG 仓库名:A的TAG
docker push 仓库名:A的TAG
容器使用主机的vpn,dockerfile中添加
| ENV https_proxy="http://host.docker.internal:主机的代理端口" \ |
| http_proxy="http://host.docker.internal:主机的代理端口" \ |