linux cpu占用率如何看
273
2022-10-19
Docker基础:从入门到进阶全流程教程应用管理
一、Docker部署安装
1、清理本地环境,卸载旧版本
卸载较旧的 Docker 版本称为 docker 或 docker-engine yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
2、安装docker需要用到的yum工具类依赖
yum install yum-utils device-mapper-persistent-data lvm2 -y
3、添加docker仓库源地址(阿里云)
yum-config-manager \ --add-repo \ install docker-ce docker-ce-cli containerd.io -y
5、启动并设置自启动docker服务
[root@docker ~]# systemctl enable docker --now [root@docker ~]# docker --version Docker version 20.10.14, build a224086
6、镜像加速配置
mkdir -p /etc/docker vim /etc/docker/daemon.json { "registry-mirrors": [" } systemctl daemon-reload systemctl restart docker
二、Docker:容器部署管理
1、运行nginx服务
[root@docker docker]# docker run --name=mynginx001 -d -P nginx
2、查看容器运行状态
[root@docker docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a340b9026c21 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:49153->80/tcp, :::49153->80/tcp mynginx001
3、访问默认页面
[root@docker docker]# curl 127.0.0.1:49153
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@docker docker]#4、进入容器内修改默认页面
[root@docker docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a340b9026c21 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:49153->80/tcp, :::49153->80/tcp mynginx001 [root@docker docker]# docker exec -it a340b9026c21 /bin/bash root@a340b9026c21:/# cd /usr/share/nginx/html/ root@a340b9026c21:/usr/share/nginx/html# ls 50x.html index.html root@a340b9026c21:/usr/share/nginx/html# echo "leoheng08" > index.html root@a340b9026c21:/usr/share/nginx/html# exit exit
5、再次访问测试
[root@docker docker]# curl 127.0.0.1:49153 leoheng08 [root@docker docker]#
6、停止某个容器运行
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a340b9026c21 nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp mynginx001 e7ac7fa4ff9d nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 80/tcp mynginx002 [root@docker ~]# docker stop mynginx002 mynginx002 [root@docker ~]# docker ps -a |grep mynginx002 e7ac7fa4ff9d nginx "/docker-entrypoint.…" 5 hours ago Exited (0) About a minute ago mynginx002 [root@docker ~]#
7、提交修改后的容器镜像
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a340b9026c21 nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp mynginx001 [root@docker ~]# docker commit -m "更换默认页" a340b9026c21 nginx:v1.0 sha256:b6be6f97fa0be8301faef8280e4fe67a364e97630f3e9c6c902a1ba65e75c699 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v1.0 b6be6f97fa0b 12 seconds ago 141MB
7、保存修改后的容器镜像
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v1.0 b6be6f97fa0b 12 seconds ago 141MB nginx latest 605c77e624dd 3 months ago 141MB [root@docker ~]# docker save -o nginx.tar nginx:v1.0 [root@docker ~]# ls nginx.tar
8、导入tar格式的镜像
[root@docker ~]# docker load -i nginx.tar Loaded image: nginx:v1.0 [root@docker ~]# [root@docker ~]# [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v1.0 b6be6f97fa0b 6 minutes ago 141MB
9、将镜像推送到镜像仓库dockerhub
登陆账号 [root@docker ~]# 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 to create one. Username: leoheng 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@docker ~]# 推送本地镜像到仓库 [root@docker ~]# docker tag nginx:v1.0 leoheng/nginx:v1 [root@docker ~]# docker push leoheng/nginx:v1 The push refers to repository [docker.io/leoheng/nginx] 058cdd951506: Pushed d874fd2bc83b: Mounted from library/nginx 32ce5f6a5106: Mounted from library/nginx f1db227348d0: Mounted from library/nginx b8d6e692a25e: Mounted from library/nginx e379e8aedd4d: Mounted from library/nginx 2edcec3590a4: Mounted from library/nginx v1: digest: sha256:26afea34ea621556c9c86f0583ec96cd1e2f76fafcfd5fe9b026ace214c35a4c size: 1778 [root@docker ~]#
10、挂载存储卷/映射卷
[root@docker ~]# mkdir -p /server/nginx [root@docker ~]# docker run --name=nginx01 -p 88:80 -v /server/nginx:/usr/share/nginx/html:ro -d leoheng/nginx:v1 74a4e48b52ac4a7d31b10d32408298fd085b4add6c0343ecea56491d6435c57b [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74a4e48b52ac leoheng/nginx:v1 "/docker-entrypoint.…" 20 seconds ago Up 18 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp nginx01 a340b9026c21 nginx "/docker-entrypoint.…" 12 hours ago Up 12 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp mynginx001 [root@docker ~]# curl 127.0.0.1:88
11、查看容器日志
[root@docker ~]# docker logs nginx01 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2022/04/08 15:54:33 [notice] 1#1: using the "epoll" event method 2022/04/08 15:54:33 [notice] 1#1: nginx/1.21.5 2022/04/08 15:54:33 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2022/04/08 15:54:33 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64 2022/04/08 15:54:33 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2022/04/08 15:54:33 [notice] 1#1: start worker processes 2022/04/08 15:54:33 [notice] 1#1: start worker process 24 2022/04/08 15:54:33 [notice] 1#1: start worker process 25 2022/04/08 15:55:03 [error] 24#24: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1:88" 172.17.0.1 - - [08/Apr/2022:15:55:03 +0000] "GET / HTTP/1.1" 403 153 "-" "curl/7.29.0" "-" 172.17.0.1 - - [08/Apr/2022:15:56:15 +0000] "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0" "-" [root@docker ~]#
12、复制本机文件到容器里
[root@docker ~]# touch test [root@docker ~]# echo "leoheng test " >> test [root@docker ~]# docker cp ./test 74a4e48b52ac:/etc/nginx/ [root@docker ~]# docker exec -it 74a4e48b52ac /bin/bash root@74a4e48b52ac:/# cd /etc/nginx/ root@74a4e48b52ac:/etc/nginx# ls conf.d fastcgi_params mime.types modules nginx.conf scgi_params test uwsgi_params root@74a4e48b52ac:/etc/nginx#
三、Docker:redis部署管理
1、拉取redis镜像,并配置redis启动外部挂载文件
[root@docker ~]# docker pull redis [root@docker ~]# mkdir /server/redis [root@docker ~]# cd /server/redis/ [root@docker redis]# vim redis.conf [root@docker redis]# cat redis.conf appendonly yes
2、启动redis容器
docker run -v /server/redis/redis.conf:/etc/redis/redis.conf \ -v /server/redis/data:/data \ -d --name=myredis \ -p 6379:6379 \ redis redis-server /etc/redis/redis.conf
3、查看redis容器状态
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 951dc5fff4ae redis "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp myredis [root@docker ~]# netstat -tunlp |grep 6379 tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 19549/docker-proxy tcp6 0 0 :::6379 :::* LISTEN 19554/docker-proxy [root@docker ~]#
4、测试连接redis服务
[root@docker ~]# yum install epel-release [root@docker ~]# yum install -y redis [root@docker ~]# redis-cli -h 127.0.0.1 127.0.0.1:6379>
5、设置redis的访问密码
[root@docker redis]# vim redis.conf [root@docker redis]# cat redis.conf appendonly yes requirepass leoheng00 [root@docker redis]# docker restart myredis myredis [root@docker redis]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> ECHO "test" (error) NOAUTH Authentication required. 127.0.0.1:6379> exit [root@docker redis]# redis-cli -h 127.0.0.1 -p 6379 -a leoheng00 127.0.0.1:6379> echo "test" “test" 127.0.0.1:6379>
四、Docker:编写Dockerfile构建nginx应用
1、创建Dockerfile文件
[root@docker ~]# cd /server/ [root@docker server]# mkdir file [root@docker server]# cd file/ [root@docker file]# vim Dockerfile [root@docker file]# cat Dockerfile #构建容器的基础镜像 FROM centos:centos7.7.1908 #容器镜像作者信息 MAINTAINER leoheng #添加URL下载的压缩包到当前目录 ADD . #运行必要的依赖服务组件 RUN yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget net-tools make #添加nginx服务运行的用户 RUN useradd -M -s /sbin/nologin nginx && yum clean all #解压编译安装过程 RUN tar -zxvf nginx-1.16.1.tar.gz RUN mkdir -p /usr/local/nginx RUN cd nginx-1.16.1 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install #软链接默认的nginx路径 RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #nginx服务运行后需要暴露的端口 EXPOSE 80 443 CMD ["nginx","-g","daemon off;"] [root@docker file]#
2、构建nginx镜像
[root@docker file]# docker build --rm -t nginx:v1 . [root@docker file]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v1 c5324ed1af79 27 seconds ago 475MB [root@docker file]# docker run -d -p 80:80 nginx:v1 [root@docker file]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES df2153d04f04 nginx:v12 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp stupefied_kirch [root@docker file]# docker exec -it df2153d04f04 /bin/bash [root@df2153d04f04 /]# ps aux |grep nginx root 1 0.0 0.0 20556 1556 ? Ss 15:43 0:00 nginx: master process nginx -g daemon off; nginx 7 0.0 0.0 21004 1560 ? S 15:43 0:00 nginx: worker process root 23 0.0 0.0 9092 680 pts/0 S+ 15:46 0:00 grep --color=auto nginx [root@df2153d04f04 /]# exit [root@docker file]# curl 127.0.0.1
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@docker file]#版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~