Docker学习笔记-从0创建并发布一个docker镜像

Docker安装略

运行环境centos7

先创建一个本地的目录

[root@localhost /]# mkdir nginx &&cd nginx

下载示例的配置文件留着备用

[root@localhostnginx]# wget http://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf

 

[root@localhostnginx]# wget http://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf

 

扫描二维码关注公众号,回复: 1214517 查看本文章

创建一个Dockerfile

 

注意文件名一定要写正确

 

[root@localhostnginx]# vi Dockerfile

 

输入下面的内容

 

FROMubuntu:14.04

MAINTAINERlilei "[email protected]"

ENVREFRESHED_AT 2018-2-2

RUNapt-get update

RUNapt-get -y -q install nginx

RUNmkdir -p /var/www/html

ADDglobal.conf /etc/nginx/conf.d/

ADDnginx.conf /etc/nginx/nginx.conf

EXPOSE80

 

制作镜像

 

然后执行docker build进行镜像的制作,这里注意后面的/nginx文件夹,如果有加,可能会报push image denied requested access to the resource is denied错误

 

[root@localhostnginx]# docker build -t lilei/nginx /nginx

Sendingbuild context to Docker daemon  5.12 kB

Step 1/9: FROM ubuntu:14.04

 ---> 8cef1fa16c77

Step 2/9: MAINTAINER lilei "[email protected]"

 ---> Using cache

 ---> 7013e02dde1c

Step 3/9: ENV REFRESHED_AT 2018-2-2

 ---> Using cache

 ---> a4cdf457c1b2

Step 4/9: RUN apt-get update

 ---> Using cache

 ---> e87bf1be2cc2

Step 5/9: RUN apt-get -y -q install nginx

 ---> Using cache

 ---> fef2d696c4bc

Step 6/9: RUN mkdir -p /var/www/html

 ---> Using cache

 ---> 838bdc0fafb0

Step 7/9: ADD global.conf /etc/nginx/conf.d/

 ---> b6f6d5de2a9f

Removingintermediate container e14a54a0cb76

Step 8/9: ADD nginx.conf /etc/nginx/nginx.conf

 ---> b52578fa9810

Removingintermediate container 2203a47feb23

Step 9/9: EXPOSE 80

 ---> Running in a938db4b48d1

 ---> 5e3141e91571

Removingintermediate container a938db4b48d1

Successfullybuilt 5e3141e91571

 

创建完成后,用docker images查看所有镜像

 

[root@localhostwebsite]# docker images

 

运行镜像

 

执行命令docker run运行此镜像

 

[root@localhostnginx]# docker run -d -p 80 --name website \

> -v$PWD/website:/var/www/html/website \

>lilei123/nginx nginx

 

执行docker ps –l查看网站的端口,可以看到对外映射的端口是32768。

[root@localhostnginx]# docker ps -l

CONTAINERID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES

60f9f90b1f87        lilei/nginx         "nginx"             2 minutes ago       Up 2 minutes        0.0.0.0:32768->80/tcp   website

 

在宿主机的/nginx/website目录下添加index.html文件,输入helloworld。

访问http://宿主IP:32768

 

发布镜像

 

先在https://hub.docker.com上注册一个帐号,然后在本地登录到hub上

 

如果未登录,会报denied: requested access to the resource is denied错误

 

[root@localhostwebsite]# docker login

Login withyour Docker ID to push and pull images from Docker Hub. If you don't have aDocker ID, head over to https://hub.docker.com to create one.

Username:lilei123

Password:

LoginSucceeded

 

然后执行push操作,注意镜像的名称要符合格式注册用户名/镜像名 

 

可以用docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)修改镜像名称

[root@localhostwebsite]# docker push lilei123/nginx

The pushrefers to a repository [docker.io/lilei123/nginx]

7f92bc2c1763:Pushed

4348d854faed:Pushed

b10190830b1e:Pushed

f1f90a01ed71:Pushed

7d48d55701f7:Pushed

98bb41f25d33:Pushed

0404d129c384:Pushed

5081cf9eb266:Pushed

f9dfc87a2e75:Pushed

ed9fd767a1ff:Pushed

0fea3c8895d3:Pushed

d626a8ad97a1:Pushed

1.0:digest: sha256:7a33a8bd719ff0fed4f75ba9f0b6e4963c88f6547039a92070ca5ff4ddc4289fsize: 2828

 

Push完成后可以在页面上看到push上来的镜像

用docker pull 可以下载我们上传的镜像

 

[root@localhostwebsite]# docker pull lilei123/nginx:1.0

Tryingto pull repository docker.io/lilei123/nginx ...

1.0:Pulling from docker.io/lilei123/nginx

 

如果本地存在会报已存在的错误,不过也可以证明我们的镜像是可以用的。

 

关注微信公众号“挨踢学霸”,更多技术姿势在等你

 

猜你喜欢

转载自blog.csdn.net/lileihappy/article/details/80511287