docker项目实战

1、使用mysql:5.6和 owncloud 镜像,构建一个个人网盘

1)拉取mysql:5.6和owncloud镜像

[root@master ~]# docker pull mysql:5.6
5.6: Pulling from library/mysql
35b2232c987e: Pull complete 
fc55c00e48f2: Pull complete 
0030405130e3: Pull complete 
e1fef7f6a8d1: Pull complete 
1c76272398bb: Pull complete 
f57e698171b6: Pull complete 
f5b825b269c0: Pull complete 
dcb0af686073: Pull complete 
27bbfeb886d1: Pull complete 
6f70cc868145: Pull complete 
1f6637f4600d: Pull complete 
Digest: sha256:20575ecebe6216036d25dab5903808211f1e9ba63dc7825ac20cb975e34cfcae
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6


[root@master ~]# docker pull owncloud
Using default tag: latest
latest: Pulling from library/owncloud
177e7ef0df69: Pull complete 
9bf89f2eda24: Pull complete 
350207dcf1b7: Pull complete 
a8a33d96b4e7: Pull complete 
c0421d5b63d6: Pull complete 
f76e300fbe72: Pull complete 
af9ff1b9ce5b: Pull complete 
d9f072d61771: Pull complete 
a6c512d0c2db: Pull complete 
5a99458af5f8: Pull complete 
8f2842d661a0: Pull complete 
3c71c5361f06: Pull complete 
baeacbad0a0c: Pull complete 
e60049bf081a: Pull complete 
0619078e32d3: Pull complete 
a8e482ee2313: Pull complete 
174d1b06857d: Pull complete 
4a86c437f077: Pull complete 
5e9ed4c3df2d: Pull complete 
8a1479477c8e: Pull complete 
8ab262044e9e: Pull complete 
Digest: sha256:173811cb4c40505401595a45c39a802b89fb476885b3f6e8fe327aae08d20fe8
Status: Downloaded newer image for owncloud:latest
docker.io/library/owncloud:latest

2)后台运行容器

#mysql
[root@master ~]# docker run -itd --name mysql1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6 
40402b2c100b13607927946db1c09d2556db03460a33a1dd7a38fa069ea8ab33

#owncloud
[root@master ~]# docker run -itd -p5900:80 --name owncloud1 owncloud:latest 
862deb3f6e97585e1c6800062774f73f485b90327a27feee0af0ca769e473421

 3)通过ip:端口的方式访问owncloud

跟据向导信息完成部署

#上传文件成功即可开始使用

docker项目实战_第1张图片

 2、安装搭建私有仓库 Harbor

1)首先准备所需包

[root@master ~]# ls
harbor-offline-installer-v2.8.4.tgz docker-compose-linux-x86_64

2)安装harbor

mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose 

#检查docker-compose版本
docker-compose --version

tar xf harbor-offline-installer-v2.8.4.tgz -C /usr/local/
cd /usr/local/
cd harbor/

#备份配置文件
cp harbor.yml.tmpl harbor.yml

#修改配置文件(按需修改)
vim harbor.yml

#安装启动harbor
./install.sh 
[+] Running 10/10
 ✔ Network harbor_harbor        Created  0.0s 
 ✔ Container harbor-log         Started  0.3s 
 ✔ Container registry           Started  0.6s 
 ✔ Container harbor-portal      Started  0.6s 
 ✔ Container registryctl        Started  0.4s 
 ✔ Container redis              Started  0.7s 
 ✔ Container harbor-db          Started  0.8s 
 ✔ Container harbor-core        Started  0.9s 
 ✔ Container harbor-jobservice  Started  1.4s 
 ✔ Container nginx              Started  1.4s 
✔ ----Harbor has been installed and started successfully.----

3)使用ip地址访问

#因为使用的80端口映射,所以直接访问ip地址
[root@master harbor]# docker ps
CONTAINER ID   IMAGE                                COMMAND                   CREATED          STATUS                    PORTS                                       NAMES
ad357892e9b7   goharbor/nginx-photon:v2.8.4         "nginx -g 'daemon of…"   14 minutes ago   Up 14 minutes (healthy)   0.0.0.0:80->8080/tcp, :::80->8080/tcp       nginx

docker项目实战_第2张图片

4)推送镜像

#命令行登录harbor
[root@master ~]# docker login harbor.com.cn
Username: admin   
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

#打标签
[root@master ~]# docker tag mysql:5.6 harbor.com.cn/own-cloud/mysql:5.6
#推送
[root@master ~]# docker push harbor.com.cn/own-cloud/mysql:5.6
The push refers to repository [harbor.com.cn/library/mysql]
7137327a7221: Pushed 
49a1ca1cd2b8: Pushed 
7c5a5c1986b1: Pushed 
eba393347f89: Pushed 
2612088e90f6: Pushed 
e3dce1c82d4e: Pushed 
7ea96a4e341b: Pushed 
4085e588967d: Pushed 
d414fdead0b9: Pushed 
2e1029557391: Pushed 
2b83e5699838: Pushed 
5.6: digest: sha256:897086d07d1efa876224b147397ea8d3147e61dd84dce963aace1d5e9dc2802d size: 2621

#退出登录
[root@master ~]# docker logout
Removing login credentials for https://index.docker.io/v1/

docker项目实战_第3张图片
3、编写Dockerfile制作Web应用系统nginx镜像,生成镜像nginx:v1.1,并推送其到私有仓库

具体要求如下:

(1)基于centos基础镜像
(2)指定作者信息
(3)安装nginx服务,将提供的dest目录(提供默认主页index.html)传到镜像内,并将dest目录内的前端文件复制到nginx的工作目录
(4)暴露80端口
(5)设置服务自启动
(6)验证镜像

1)制作镜像 

#创建默认主页
[root@master ~]# mkdir nginx
[root@master ~]# cd nginx/
[root@master nginx]# vim index.html

docker nginx build successful

#编写Dockerfile文件(最好放在空目录) [root@master ~]# mkdir /dockerfile/web -p [root@master ~]# cd /dockerfile/web/ [root@master web]# vim Dockerfile #指定使用镜像及版本 FROM centos:7 #标明作者和邮箱 MAINTAINER "test " #下载wget配置epel源下载nginx RUN yum install -y wget RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo RUN yum install -y nginx #将提前写好的主页替换掉默认的 COPY index.html /usr/share/nginx/html/index.html #暴露80端口 EXPOSE 80/tcp #设置开机自启服务 CMD ["nginx", "-g", "daemon off;"] #构建nginx:v1.1镜像 [root@master nginx]# docker build -t nginx:v1.1 -f /dockerfile/web/Dockerfile . [+] Building 42.0s (10/10) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 320B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:7 0.0s => CACHED [1/5] FROM docker.io/library/centos:7 0.0s => [internal] load build context 0.0s => => transferring context: 77B 0.0s => [2/5] RUN yum install -y wget 18.9s => [3/5] RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 0.7s => [4/5] RUN yum install -y nginx 21.6s => [5/5] COPY index.html /usr/share/nginx/html/index.html 0.0s => exporting to image 0.7s => => exporting layers 0.7s => => writing image sha256:c35cac64c3d272124c91e46fd52b9c9935fcb5686890b29241035d7474f03182 0.0s => => naming to docker.io/library/nginx:v1.1 0.0s #查看成功构建的镜像 [root@master nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v1.1 c35cac64c3d2 4 minutes ago 684MB centos 7 eeb6ee3f44bd 23 months ago 204MB #使用构建的镜像启动一个容器 [root@master ~]# docker run -d -p 3000:80 --name test1 nginx:v1.1 b22cc4550e85e0bf4512467abfbbfb310c07d18246c78ad1f8e5909acec1176c [root@master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b22cc4550e85 nginx:v1.1 "nginx -g 'daemon of…" 4 seconds ago Up 4 seconds 0.0.0.0:3000->80/tcp, :::3000->80/tcp test1

docker项目实战_第4张图片

2)推送镜像 

#登录自己的dockerhub镜像仓库
[root@master ~]# 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: workl
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

#打包镜像
[root@master ~]# docker tag nginx:v1.1 workl/test1:my_nginx

#推送镜像
[root@master ~]# docker push workl/test1:my_nginx
The push refers to repository [docker.io/workl/test1]
fab04c3e6af6: Pushed 
c5cd136625a9: Pushed 
bac427254136: Pushed 
e7bdba266517: Pushed 
174f56854903: Pushed 

 

 4、Dockerfile快速搭建自己专属的LAMP环境,生成镜像lamp:v1.1,并推送到私有仓库

具体要求如下

(1)基于centos:6基础镜像
(2)指定作者信息
(3)安装httpd、mysql、mysql-server、php、php-mysql、php-gd
(4)暴露80和3306端口
(5)设置服务自启动
(6)验证镜像



[root@master dockerfile]# mkdir lamp
[root@master ~]# cd dockerfile/lamp/
[root@master lamp]# ls
Dockerfile
[root@master lamp]# vim Dockerfile

#注意:宿主机使用的版本尽量与docker拉取的镜像版本一致
FROM centos:6
MAINTAINER "test "

RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo && sed -i 's|http://mirrors.aliyun.com/centos|https://mirrors.aliyun.com/centos-vault/centos|g' /etc/yum.repos.d/CentOS-Base.repo
RUN yum install -y wget && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
COPY run.sh /run.sh
RUN chmod +x /run.sh
RUN yum install -y httpd mysql mysql-server php php-mysql php-gd
ADD wordpress-4.8-zh_CN.tar.gz /var/www/html

EXPOSE 80 3306
CMD ["./run.sh"]


[root@master ~]# mkdir lamp
[root@master ~]# cd lamp/
[root@master lamp]# vim run.sh
#!/bin/bash
systemctl start mysql
sleep 3
mysql -e "create database wordpress;"
mysql -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"
sleep 3
systemctl start httpd


[root@master lamp]# chmod +x run.sh 
[root@master lamp]# ls
run.sh                                              wordpress-4.8-zh_CN.tar.gz


[root@master lamp]# docker build -t lamp:v1.1 -f /dockerfile/lamp/Dockerfile .
[+] Building 31.1s (11/11) FINISHED                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 615B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                        0.0s
 => [1/6] FROM docker.io/library/centos:7                                                                          0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 76B                                                                                   0.0s
 => CACHED [2/6] RUN yum install -y wget && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epe  0.0s
 => CACHED [3/6] COPY run.sh /run.sh                                                                               0.0s
 => CACHED [4/6] RUN chmod +x /run.sh                                                                              0.0s
 => [5/6] RUN yum install -y httpd mysql mysql-server php php-mysql php-gd                                        29.6s
 => [6/6] ADD wordpress-4.8-zh_CN.tar.gz /var/www/html                                                             0.2s 
 => exporting to image                                                                                             1.2s 
 => => exporting layers                                                                                            1.2s 
 => => writing image sha256:6197e9c54d05cf3f2aeda27ea570102d2a30d73f0f49b89f6817fc85e923468c                       0.0s 
 => => naming to docker.io/library/lamp:v1.1                                                                       0.0s 
[root@master lamp]# docker images
REPOSITORY    TAG        IMAGE ID       CREATED          SIZE
lamp          v1.1       6197e9c54d05   15 seconds ago   823MB
nginx         v1.1       c35cac64c3d2   3 hours ago      684MB
workl/test1   my_nginx   c35cac64c3d2   3 hours ago      684MB
centos        6          5bf9684f4720   23 months ago    194MB
centos        7          eeb6ee3f44bd   23 months ago    204MB


[root@master lamp]# docker run -itd --name test1 lamp:v1.1 /bin/bash
4f0a785b22fd7241ab777ff493e8537eb332444144bdb13b86d376016916fa7e
[root@master lamp]# docker ps
CONTAINER ID   IMAGE       COMMAND       CREATED          STATUS          PORTS              NAMES
4f0a785b22fd   lamp:v1.1   "/bin/bash"   16 seconds ago   Up 16 seconds   80/tcp, 3306/tcp   test1

你可能感兴趣的:(docker,docker,容器,运维)