Docker安装略

运行环境centos7


先创建一个本地的目录


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


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


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

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


创建一个Dockerfile


注意文件名一定要写正确

[root@localhost nginx]# vi Dockerfile

输入下面的内容

FROM ubuntu:14.04

MAINTAINER lilei "[email protected]"

ENV REFRESHED_AT 2018-2-2

RUN apt-get update

RUN apt-get -y -q install nginx

RUN mkdir -p /var/www/html

ADD global.conf /etc/nginx/conf.d/

ADD nginx.conf /etc/nginx/nginx.conf

EXPOSE 80


制作镜像


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

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

Sending build 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

Removing intermediate container e14a54a0cb76

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

---> b52578fa9810

Removing intermediate container 2203a47feb23

Step 9/9 : EXPOSE 80

---> Running in a938db4b48d1

---> 5e3141e91571

Removing intermediate container a938db4b48d1

Successfully built 5e3141e91571

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

[root@localhost website]# docker images


运行镜像


执行命令docker run运行此镜像

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

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

> lilei123/nginx nginx

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

[root@localhost nginx]# docker ps -l

CONTAINER ID 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文件,输入hello world。

访问http://宿主IP:32768

Docker学习笔记1-从0创建并发布一个docker镜像_第1张图片


发布镜像


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

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

[root@localhost website]# docker login

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

Username: lilei123

Password:

Login Succeeded

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

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

[root@localhost website]# docker push lilei123/nginx

The push refers 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:7a33a8bd719ff0fed4f75ba9f0b6e4963c88f6547039a92070ca5ff4ddc4289f size: 2828

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

Docker学习笔记1-从0创建并发布一个docker镜像_第2张图片

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

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

Trying to pull repository docker.io/lilei123/nginx ...

1.0: Pulling from docker.io/lilei123/nginx

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

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

Docker学习笔记1-从0创建并发布一个docker镜像_第3张图片