Docker學習筆記1-從0創建並發布一個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
發布鏡像
先在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 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鏡像