Docker构建本地镜像

  1. 创建文件
sulei@sulei:~$ cd docker
bash: cd: docker: 没有那个文件或目录
sulei@sulei:~$ mkdir mydocker
sulei@sulei:~$ cd mydocker/
sulei@sulei:~/mydocker$ ls
sulei@sulei:~/mydocker$ touch Dockerfile

2.编辑文件

FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a |cowsay

#添加fortunes程序到镜像中

3.Dockerfile构建镜像

sulei@sulei:~/mydocker$ sudo docker build -t docker-whale .
 ---> Running in 0eaf8a62140a
 ---> 6b7de031519f
Removing intermediate container 0eaf8a62140a
Successfully built 6b7de031519f

4.运行自己的镜像docker-whale

sulei@sulei:~/mydocker$ sudo docker images#查看自己的镜像
#运行自己的镜像文件
sulei@sulei:~/mydocker$ sudo docker run docker-whale
 ______________________________________ 
/ But, officer, he's not drunk, I just \
\ saw his fingers twitch!              /
 -------------------------------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

此处可能出现构建本地镜像错误的问题:原因是镜像源可能出现错误,最好使用官方的镜像源
二.Docker push镜像到docker hub并验证
tag/push/pull 镜像
1.查看镜像
2.寻找镜像ID
3.使用docker login远程登录到docker hub

sudo docker login --username=yourhubusername --email=youremail@company.com
sulei@sulei:~/mydocker$ sudo docker login --username=shursulei --email=1368455669@qq.com
Flag --email has been deprecated, will be removed in 17.06.
Password: 
Login Succeeded

4.使用push命令上传镜像
这里写图片描述
确定名字空间
dockerhub帐号/镜像名:shursulei/hello-world
使用image id 和docker tag标记镜像

docker tag 7694d52625 maryatdocker/docker-whale:latest //例子
sulei@sulei:~/mydocker$ sudo docker tag 1815c82652c0 shursulei/hello-world:latest

再次查看镜像

sulei@sulei:~/mydocker$ sudo docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker-whale            latest              6b7de031519f        36 minutes ago      275MB
hello-world             latest              1815c82652c0        2 weeks ago         1.84kB
shursulei/hello-world   latest              1815c82652c0        2 weeks ago         1.84kB
docker/whalesay         latest              6b362a9f73eb        2 years ago         247MB
sulei@sulei:~/mydocker$ 

最后使用Push上传到自己的hub的仓库中

sulei@sulei:~/mydocker$ sudo docker push shursulei/hello-world
The push refers to a repository [docker.io/shursulei/hello-world]
45761469c965: Mounted from library/hello-world 
latest: digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f size: 524

三.Docker从docker hub pull镜像到本地
1.查看本地镜像sulei@sulei:~/mydocker$ sudo docker images

2.查看帮助

sudo docker 
  rm          Remove one or more containers
  rmi         Remove one or more images

2.删除本地的镜像文件

sulei@sulei:~/mydocker$ sudo docker rmi -f 1815c82652c0
Untagged: hello-world:latest
Untagged: hello-world@sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Untagged: shursulei/hello-world:latest
Untagged: shursulei/hello-world@sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Deleted: sha256:1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57
#也可以用使用仓库名删除

3.使用docker run下载镜像并使用

sulei@sulei:~/mydocker$ sudo docker run shursulei/hello-world
验证是否成功
sulei@sulei:~/mydocker$ sudo docker images
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐