Ubuntu下安装Docker并登录hub及推送镜像到仓库-入门记录

https://ubuntu.com/
中文:https://cn.ubuntu.com/

菜鸟教程:Ubuntu安装Docker

安装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

解决方法

  1. 使用sudo获取管理员权限,运行docker命令。如:sudo docker ps
  2. 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 的登陆

没有账户?先注册

https://hub.docker.com/

文中所有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

拉取镜像

  1. 可以先进行搜索,docker search xxx
  2. 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

远程没有这个镜像,所以它给我创建了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xNJ5W1rP-1620463552224)(https://note.youdao.com/yws/res/a/WEBRESOURCEd7df4d21b0dc8e4e608b7ee60e69b22a)]

Dockerfile

… … …

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值