https://ubuntu.com/
中文:https://cn.ubuntu.com/
安装docker
ubuntu@ubuntu:~$ curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
Command 'curl' not found, but can be installed with:
sudo apt install curl
ubuntu@ubuntu:~$ sudo apt install curl
权限问题-不能直接使用docker命令 需要加sudo
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/images/json: dial unix /var/run/docker.sock: connect: permission denied
解决方法
- 使用sudo获取管理员权限,运行docker命令。如:sudo docker ps
- docker守护进程启动的时候,会默认赋予名字为docker的用户组读写Unix socket的权限,因此只要创建docker用户组,并将当前用户加入到docker用户组中,那么当前用户就有权限访问Unix socket了,进而也就可以执行docker相关命令
sudo groupadd docker #添加docker用户组
sudo gpasswd -a $USER docker #将登陆用户加入到docker用户组中 ($USER当前登录的用户名)
newgrp docker #更新用户组
docker ps #测试docker命令是否可以使用sudo正常使用
添加权限实际操作
ubuntu@ubuntu:~$ docker images
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/images/json: dial unix /var/run/docker.sock: connect: permission denied
ubuntu@ubuntu:~$ sudo groupadd docker
groupadd: group 'docker' already exists
ubuntu@ubuntu:~$ sudo gpasswd -a ubuntu docker
Adding user ubuntu to group docker
ubuntu@ubuntu:~$ newgrp docker
ubuntu@ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 8 weeks ago 13.3kB
ubuntu@ubuntu:~$
跑一个镜像helloworld,执行命令后会默认拉取一个latest镜像
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
Docker Hub 的登陆
没有账户?先注册
文中所有s***都是自己的账户名
ubuntu@ubuntu:~$ docker login -u s*** -p d*****
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
++WARNING! Using --password via the CLI is insecure. Use --password-stdin.++
警告:要以非交互方式运行docker login命令,可以将 --password-stdin标志设置为通过STDIN提供密码。使用STDIN可以防止密码出现在shell的历史记录或日志文件中。
我们用文件的方式来处理
创建一个文件,把密码写里面,我们通过–password-stdin 这个方式,将密码传入
首先在自己的根目录下 /home/ubuntu/ 下创建文件mydocker.txt,这里使用终端去创建。
进入目录,创建mydocker
ubuntu@ubuntu:~$ cd /home/ubuntu
ubuntu@ubuntu:~$ touch mydocker
ubuntu@ubuntu:~$ ls
Desktop Downloads mydocker Pictures snap Videos
Documents Music Public Templates
ubuntu@ubuntu:~$
编辑mydocker,不要带有空格或换行,填写好后,先按“ESC”,再“:wq” 保存退出
ubuntu@ubuntu:~$ vi mydocker
可以预览
ubuntu@ubuntu:~$ cat mydocker
之后登陆, 使用 --password-stdin <~/mydocker
ubuntu@ubuntu:~$ docker login -u s*** --password-stdin <~/mydocker
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
拉取镜像
- 可以先进行搜索,docker search xxx
- docker pull xxx
推送到自己的镜像仓库
必须给自己的镜像标记tag,且标记名需以用户名/开头,不然就是会提示没有授权被拒绝 unauthorized
实际默认是推送到这了:docker.io/library/hello-world,肯定不对啦
ubuntu@ubuntu:~$ docker push hello-world:v1
The push refers to repository [docker.io/library/hello-world]
f22b99068db9: Layer already exists
errors:
denied: requested access to the resource is denied
unauthorized: authentication required
这里重新标记 以自己Docker Hub的用户名开头哦,用户名/镜像名:tag。
ubuntu@ubuntu:~$ docker tag hello-world s***/hello-world:v1
ubuntu@ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7e0aa2d69a15 11 days ago 72.7MB
s***/hello-world v1 d1165f221234 2 months ago 13.3kB
ubuntu@ubuntu:~$ docker push s***/hello-world:v1
The push refers to repository [docker.io/s***/hello-world]
f22b99068db9: Mounted from library/hello-world
v1: digest: sha256:1b2***602946860c279fce658f31050cff2c596583af237d971f4629b57792 size: 525
远程没有这个镜像,所以它给我创建了
Dockerfile
… … …