1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 搭建vmware虚拟机 搭建docker环境

搭建vmware虚拟机 搭建docker环境

时间:2021-11-11 01:55:03

相关推荐

搭建vmware虚拟机 搭建docker环境

搭建vmware虚拟机

操作系统:windows10企业版

vmware版本:16.0.0

centos镜像版本:CentOS-7-x86_64-DVD-.iso

使用vmware新建虚拟机没有ip地址

1.编辑/etc/sysconfig/network-scripts/ifcfg-ens33文件,将其中的ONBOOT设置为yes

2.用service network restart重启网络

搭建docker环境

设置base源

curl -o /etc/yum.repos.d/CentOS-Base.repo /repo/Centos-7.repo

设置epel源

yum install epel-release -y

安装工具包

yum install -y yum-utils

设置远程仓库

yum-config-manager --add-repo /docker-ce/linux/centos/docker-ce.repo

安装 yum install docker-ce

可以用yum list docker-ce --show-dumplcates先查看可用版本

配置文件

vim /etc/docker/daemon.json{"graph": "/data/docker","storage-driver": "overlay2","insecure-registries": ["quay.io"],"registry-mirrors": ["https://q2gr04ke."],"bip":"172.152.128.1/24","exec-opts": ["native.cgroupdriver=systemd"],"live-restore": true}

mkdir -p /data/docker

systemctl enable docker

启动 systemctl start docker

查看是否启动正常docker info

查看版本 docker version

校验 docker run hello-world,出现如下信息就说明docker安装成功!

[root@localhost docker]# docker run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world0e03bdcc26d7: Pull complete Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391dStatus: Downloaded newer image for hello-world:latestHello from Docker!This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.

docker基本操作

一、镜像

1.docker login docker.io#用户名密码需要到dockerhub官网上注册2.docker search alpine3.docker pull alpine4.docker images[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kB5.docker tag [root@localhost docker]# docker tag 7731472c3f2a docker.io/library/alpine:v3.10.3[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest 7731472c3f2a 5 weeks ago5.61MBalpine v3.10.3 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kB6.docker push [root@localhost docker]# docker tag 7731472c3f2a docker.io/shenzy/alpine:v.3.10.3[root@localhost docker]# docker imagesREPOSITORYTAG IMAGE ID CREATED SIZEalpine latest7731472c3f2a 5 weeks ago5.61MBalpine v3.10.3 7731472c3f2a 5 weeks ago5.61MBshenzy/alpine v.3.10.3 7731472c3f2a 5 weeks ago5.61MBhello-world latestbf756fb1ae65 13 months ago 13.3kB[root@localhost docker]# docker push docker.io/shenzy/alpine:v.3.10.3The push refers to repository [docker.io/shenzy/alpine]c04d1437198b: Mounted from library/alpine v.3.10.3: digest: sha256:d0710affa17fad5f466a70159cc458227bd25d4afb39514ef662ead3e6c99515 size: 5287.docker rmi[root@localhost docker]# docker rmi -f 7731472c3f2aUntagged: alpine:latestUntagged: alpine:v3.10.3Untagged: alpine@sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378Untagged: shenzy/alpine:v.3.10.3Untagged: shenzy/alpine@sha256:d0710affa17fad5f466a70159cc458227bd25d4afb39514ef662ead3e6c99515Deleted: sha256:7731472c3f2a25edbb9c085c78f42ec71259f2b83485aa60648276d408865839Deleted: sha256:c04d1437198bc178b49fdb9a90a9c0e362ffbd63c4be5d19fec406d1c9d6a03c[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhello-world latest bf756fb1ae65 13 months ago 13.3kB

二、容器

1.docker ps -a查看本地容器进程[root@localhost docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATEDSTATUS PORTSNAMES01be67535b82 hello-world "/hello" 41 minutes ago Exited (0) 41 minutes ago affectionate_maxwell2.启动容器docker run [OPTIONS] IMAGE [COMMAND] [ARG..]OPTIONS选项:-i : 启动一个交互式的容器,并持续打开标准输入-t : 使用终端关联到容器的标准输入输出上-d : 将容器放置到后台运行--rm : 退出后即删除容器--name : 定义容器的唯一名称[root@localhost docker]# docker run -it alpine:latest /bin/sh/ # ip add1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:98:80:02 brd ff:ff:ff:ff:ff:ffinet 172.152.128.2/24 brd 172.152.128.255 scope global eth0valid_lft forever preferred_lft forever/ # cat /etc/issueWelcome to Alpine Linux 3.13Kernel \r on an \m (\l)/ # exit[root@localhost docker]# docker run --rm alpine:latest /bin/echo hellohello[root@localhost docker]# docker run -d --name myalpine alpine:lateste676a230622eceaf3eb719ac266241b2e5e805e8faad98b68a3a7a74647b[root@localhost docker]# docker run -d --name myalpine1 alpine:latest /bin/sleep 300b4207ad50f93f6d9cd55ae4614a475c9adc27bca06c35025b4a645013a3e1038[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUSPORTSNAMESb4207ad50f93 alpine:latest "/bin/sleep 300" 4 seconds ago Up 3 secondsmyalpine1e676a230622e alpine:latest "/bin/sh"About a minute ago Exited (0) About a minute ago myalpine6d95395bad56 alpine:latest "/bin/sh"5 minutes ago Exited (0) 3 minutes ago naughty_burnell01be67535b82 hello-world"/hello" 2 hours agoExited (0) 2 hours agoaffectionate_maxwell3.查看宿主机进程[root@localhost docker]# ps aux|grep sleep |grep -v greproot16655 0.0 0.0 1568 248 ? Ss 11:20 0:00 /bin/sleep 3004.进入容器[root@localhost docker]# docker exec -ti 6e763856a350 /bin/sh/ # ps -aPID USERTIME COMMAND1 root0:00 /bin/sleep 3007 root0:00 /bin/sh13 root0:00 ps -a/ # exit5.启停容器[root@localhost docker]# docker stop 6e763856a3506e763856a350[root@localhost docker]# docker start 6e763856a3506e763856a350[root@localhost docker]# docker restart 6e763856a3506e763856a3506.删除容器6e763856a350[root@localhost docker]# docker rm b4207ad50f93b4207ad50f93[root@localhost docker]# docker rm myalpinemyalpine[root@localhost docker]# docker rm -f myalpine2myalpine2[root@localhost docker]# for i in `docker ps -a|grep -i exit|sed '1d'|awk '{print $1}'`;do docker rm -f $i;done01be67535b827.提交/修改容器[root@localhost docker]# docker ps -aCONTAINER ID IMAGECOMMAND CREATED STATUS PORTSNAMES[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kB[root@localhost docker]# docker run -d --name myalpine alpine:latest /bin/sleep 3001e5304c74ebc034778ffb43ea44d0946ccaa04b966beb2e36d3d8c67dc90e3c2[root@localhost docker]# docker exec -ti myalpine /bin/sh/ # lsbin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var/ # echo hello>1.txt/ # ls1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var/ # exit[root@localhost docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTSNAMES1e5304c74ebc alpine:latest "/bin/sleep 300" About a minute ago Up About a minute myalpine[root@localhost docker]# docker commit -p myalpine alpine:3.10.3_with_1.txtsha256:e459545351027405a3286f417736c990421f550265c3d94eefa131ed34555d04[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine 3.10.3_with_1.txt e45954535102 5 seconds ago 5.61MBalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kB8.导出/导入容器[root@localhost docker]# docker save e45954535102 > alpine:3.10.3_with_1.txt.tar [root@localhost docker]# lsalpine:3.10.3_with_1.txt.tar buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes[root@localhost docker]# docker rmi -f alpine:3.10.3_with_1.txtUntagged: alpine:3.10.3_with_1.txtDeleted: sha256:e459545351027405a3286f417736c990421f550265c3d94eefa131ed34555d04Deleted: sha256:880621b71d1d945e002ec832ef04684b09d82385c5f64fad43d457770a2c4ac4[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kB[root@localhost docker]# docker load < alpine\:3.10.3_with_1.txt.tar d2302c958420: Loading layer [==================================================>] 3.584kB/3.584kBLoaded image ID: sha256:e459545351027405a3286f417736c990421f550265c3d94eefa131ed34555d04[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE<none> <none> e45954535102 5 minutes ago 5.61MBalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kB[root@localhost docker]# docker tag e45954535102 myalpine:3.10.3_with_1.txt[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyalpine3.10.3_with_1.txt e45954535102 6 minutes ago 5.61MBalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kB9.查看容器日志[root@localhost docker]# docker run hello-world 2>&1 >>/dev/null[root@localhost docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATEDSTATUS PORTSNAMESaec01b5a34e1 hello-world"/hello" 8 seconds ago Exited (0) 6 seconds ago strange_williams1e5304c74ebc alpine:latest "/bin/sleep 300" 16 minutes ago Exited (0) 11 minutes ago myalpine[root@localhost docker]# docker logs aec01b5a34e1Hello from Docker!This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.

三、docker容器的高级操作

1.映射端口

docker run -p容器外端口:容器内端口

[root@localhost docker]# docker pull nginx:1.12.21.12.2: Pulling from library/nginxf2aa67a397c4: Pull complete e3eaf3d87fe0: Pull complete 38cb13c1e4c9: Pull complete Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728Status: Downloaded newer image for nginx:1.12.2docker.io/library/nginx:1.12.2[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyalpine3.10.3_with_1.txt e45954535102 42 hours ago 5.61MBalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kBnginx 1.12.2 4037a5562b03 2 years ago108MB[root@localhost docker]# docker tag 4037a5562b03 shenzy/nginx:v1.12.2[root@localhost docker]# docker run --rm --name mynginx -d -p81:80 shenzy/nginx:v1.12.2f6cddb0be8869e6be230e962c13afededebed2cfce208017ab8810839f6eb892[root@localhost docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUSPORTSNAMESf6cddb0be886 shenzy/nginx:v1.12.2 "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:81->80/tcp mynginxaec01b5a34e1 hello-world"/hello" 42 hours ago Exited (0) 42 hours ago strange_williams1e5304c74ebc alpine:latest "/bin/sleep 300" 42 hours ago Exited (0) 42 hours ago myalpine[root@localhost docker]# netstat -luntp|grep 81tcp 00 0.0.0.0:81 0.0.0.0:*LISTEN25059/docker-proxy [root@localhost docker]# curl 127.0.0.1:81<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="/"></a>.<br/>Commercial support is available at<a href="/"></a>.</p><p><em>Thank you for using nginx.</em></p></body></html>

2.挂在数据卷

docker run -v 容器外目录:容器内目录[root@localhost html]# wget ---02-22 15:07:44-- /正在解析主机 ()... 180.101.49.11, 180.101.49.12正在连接 ()|180.101.49.11|:80... 已连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:2381 (2.3K) [text/html]正在保存至: “index.html”100%[==================================================================================================================================================================================================>] 2,381 --.-K/s 用时 0s-02-22 15:07:44 (104 MB/s) - 已保存 “index.html” [2381/2381])[root@localhost html]# ll总用量 4-rw-r--r--. 1 root root 2381 2月 22 15:07 index.html[root@localhost ~]# docker run --rm -d --name mynginx_with_baidu -p82:80 -v/root/html:/usr/share/nginx/html shenzy/nginx:v1.12.251c4eeb798cd791e1560ee1a45d61d6ebfc88803a3356cdd01d2e25b691db302[root@localhost ~]# docker inspect mynginx_with_baidu[{"Id": "51c4eeb798cd791e1560ee1a45d61d6ebfc88803a3356cdd01d2e25b691db302","Created": "-02-22T07:13:37.19643Z","Path": "nginx","Args": ["-g","daemon off;"],

3.传递环境变量

docker run -e 环境变量key=环境变量value

[root@localhost ~]# docker run --rm -e E_OPTS=adf alpine:latest printenvPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOSTNAME=3d81dd19ac2bE_OPTS=adfHOME=/root[root@localhost ~]# docker run --rm -e E_OPTS=adf -e JAVA_HOME=/root alpine:latest printenvPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOSTNAME=2dc6a715cb7cE_OPTS=adfJAVA_HOME=/rootHOME=/root

4.容器内安装软件(工具)等

yum/apt-get/apt等

root@51c4eeb798cd:/# tee /etc/apt/sources.list << EOF> deb /debian/ jessie main non-free contrib> deb /debian/ jessie-update main non-free contrib> EOFdeb /debian/ jessie main non-free contribdeb /debian/ jessie-update main non-free contribroot@51c4eeb798cd:/# apt-get updateIgn:1 /debian jessie InReleaseIgn:2 /debian jessie-update InReleaseGet:3 /debian jessie Release [77.3 kB]...root@51c4eeb798cd:/# apt-get install curl -yReading package lists... DoneBuilding dependency tree Reading state information... Done...root@51c4eeb798cd:/# curl -k <!DOCTYPE html>...[root@localhost ~]# docker commit -p 51c4eeb798cd shenzy/nginx:curl sha256:feff1fcfeff914dce944877238944d777c4d963a110122eb8119ba090c4fc6a6[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEshenzy/nginx curlfeff1fcfeff9 8 seconds ago 136MBmyalpine 3.10.3_with_1.txt e45954535102 2 days ago5.61MBalpine latest 7731472c3f2a 5 weeks ago5.61MBhello-world latest bf756fb1ae65 13 months ago 13.3kBnginx 1.12.2 4037a5562b03 2 years ago108MBshenzy/nginx v1.12.2 4037a5562b03 2 years ago108MB[root@localhost ~]# docker push docker.io/shenzy/nginx:curlThe push refers to repository [docker.io/shenzy/nginx]4b152d34b795: Pushed 4258832b2570: Mounted from library/nginx 683a28d1d7fd: Mounted from library/nginx d626a8ad97a1: Pushed curl: digest: sha256:b3d1ada2a0586b165728f7612250d8996a5a6b7a10a347c663869a405d01b47b size: 1160

容器的生命周期

1.检查本地是否存在镜像,如果不存在即从远端仓库检索

2.利用镜像启动容器

3.分配一个文件系统,并在只读的镜像层外挂载一层可读写层

4.从宿主机配置的网桥接口钟桥接一个虚拟接口到容器

5.从地址池配置一个ip地址给容器

6.执行用户指定的指令

7.执行完毕后容器终止

Docker镜像制作的途径

docker commit

Dockerfile

About DockerfileDockerfile is nothing but the source code for building Docker image.1.Docker can build images automatically by reading the instructions from Dockerfile2.A Dockerfile is a text document that contains all the conmands a user could call on the command line to assemble an image.3.Using docker build,users can create an automated build that executes several command line instructions in succession.

4组核心的Dockerfile指令

USER/WORKDIR指令

ADD/EXPOSE指令

RUN/ENV指令

CMD/ENTRYPOINT指令

[root@localhost dockerfile]# tee /data/dockerfile/Dockerfile <<EOF> FROM docker.io/shenzy/nginx:v1.12.2> USER nginx> WORKDIR /usr/share/nginx/html> EOFFROM docker.io/shenzy/nginx:v1.12.2USER nginxWORKDIR /usr/share/nginx/html[root@localhost dockerfile]# docker build . -t shenzy/nginx:v1.12.2_with_dockerfileSending build context to Docker daemon 2.048kBStep 1/3 : FROM docker.io/shenzy/nginx:v1.12.2---> 4037a5562b03Step 2/3 : USER nginx---> Running in 2e3216f5ba82Removing intermediate container 2e3216f5ba82---> ef7eb46d9905Step 3/3 : WORKDIR /usr/share/nginx/html---> Running in 31553d12c541Removing intermediate container 31553d12c541---> 927011eab2f9Successfully built 927011eab2f9Successfully tagged shenzy/nginx:v1.12.2_with_dockerfile[root@localhost dockerfile]#

实验

[root@localhost dockerfile]# vim Dockerfile FROM shenzy/nginx:v1.12.2USER rootENV WWW /usr/share/nginx/htmlENV CONF /etc/nginx/conf.dRUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezoneWORKDIR $WWWADD index.html $WWW/index.htmlADD .conf $CONF/.confEXPOSE 80CMD ["nginx","-g","daemon off;"][root@localhost dockerfile]# vim .confserver {listen 80;server_name ;root /usr/share/nginx/html;}

Docker网络模型

1.NAT

2.None

3.Host

4.联合网络

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。