docker基础
一、dockers 安装前的准备工作:
1、关闭防火墙:
关闭:systemctl stop firewalld
禁止开自启动:systemctl disable firewalld
查看状体:systemctl status firewalld
2、修改主机名:
hostnamectl set-hostname MB-200
3、查看selinux是否被关闭
[root@docker-5 ~]# getenforce Disabled [root@docker-5 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
4、配置yum源
#1、配置base源 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#2、配置epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
5、yum常用安装命名:
#1、安装 yum install net-tools -y #2、查看包 yum list docker-ce --showduplicates yum list docker --showduplicates
6、linux 主机信任命令
#1、生产密钥 ssh-keygen -t rsa #2、分发密钥: ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.4.7.5
7、docker的daemon.json文件
cd /etc/docker/ [root@docker-5 docker]# cat daemon.json { "graph": "/data/docker/", "storage-driver": "overlay2", "insecure-registries": ["registry.access.redhat.com","quay.io"], "registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"], "bip": "172.7.5.1/24", "exec-opts": ["native.cgroupdriver=systemd"], "live-restore": true }
8、指定版本安装docker
#查看docker包 yum list docker-ce --showduplicates #指定版本安装docker yum install -y docker-ce-19.03.4-3.el7 #默认安装最新的版本 yum install -y docker
9、安装dockeryum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
10、对时
#1、安装对时命令: yum -y install ntpdate #2、对时 ntpdate asia.pool.ntp.org
11、启动docker
#1、开机自启动 systemctl enable docker.service #2、重置错误 systemctl reset-failed docker.service #3、启动docker systemctl start docker.service
二、docker常用命令
1、docker info
# 查看docker 安装信息
[root@docker-5 docker]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Docker Buildx (Docker Inc., v0.8.1-docker) scan: Docker Scan (Docker Inc., v0.17.0) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 33 Server Version: 19.03.4 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: systemd Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 3df54a852345ae127d1fa3092b95168e4a88e2f8 runc version: v1.0.3-0-gf46b6ba init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-1062.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.934GiB Name: docker-5 ID: NF2Z:SZIE:IB4W:WCEZ:WHMQ:QOND:RDJR:EL4K:CIM4:PXFT:SADM:2OAL Docker Root Dir: /data/docker Debug Mode: false Username: alexma110 Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: quay.io registry.access.redhat.com 127.0.0.0/8 Registry Mirrors: https://q2gr04ke.mirror.aliyuncs.com/ Live Restore Enabled: true WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled
2、启动第一个docker实例
[root@docker-5 docker]# docker run hello-world Hello 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 the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
3、登录远程镜像仓库:已dockerhub为例:
docker login docker.io
4、docker镜像的常用命令:
#1、拉去镜像 docker pull alpine #2、查找镜像 docker search alpine #3、拉取特定的镜像 docker pull alpine:3.10.3 #4、查看镜像 docker image ls docker images #5、给镜像打标签 docker tag feb5d9fea6a5 docker.io/alexma110/alpine:v3.10.10 #6、推镜像到镜像仓库 docker push docker.io/alexma110/alpine:v3.10.10 #7、登录远程镜像仓库 docker login docker.io #7.1、查看登录账号密码 cat /root/.docker/config.json echo 'YWxleG1hMTEwOkFsZXhNYUAxMjM=' |base64 -d #8、推镜像到镜像仓库 docker push docker.io/alexma110/alpine:v3.10.10 #9、删除镜像 docker rmi -f 965ea09ff2eb
5、容器常用命令
#1、查看所有容器 docker ps -a #2、启动一个容器 docker run -it alpine:3.15 /bin/sh #3、查看容器日志 docker logs -f fb5d44fef97c #4、容器后台运行 docker run -d --name myalpine_1 alpine:3.14 /bin/sleep 100 #5、登录容器 docker exec -it 12762f5b6d4b /bin/sh
#6、 -d 后台运行 --name 给容器起名字
docker run -d --name myalpine_3 alpine:3.14 /bin/sleep 100
#7、启停容器
docker stop 32683642aad6
docker start 32683642aad6
docker restart 32683642aad6
#8、删除容器
docker rm myalpine
#9、批量删除退出的容器
for i in `docker ps -a|grep -i exit|awk '{print $1}'`;do docker rm -f $i;done
#10、生产新的容器
docker commit -p myalpine alexma110/alpine:V3.15_with_1.txt
#11、交互式启动容器
docker run -it alexma110/alpine:V3.15_with_1.txt /bin/sh
#12、保存镜像
docker sava 40086be00458 >alpine:V3.15_with_1.txt.tar
#13、查看镜像
docker images
#14、load 镜像
docker load <alpine\:V3.15_with_1.txt.tar
#15、查看容器日志
docker logs some_taussig
#16、指定映射端口启动容器
docker run --rm --name mynginx -d -p81:80 alexma110/nginx:v1.12.2
#17、指定存储卷启动容器
docker run --rm --name mynginx_with_html -d -p82:80 -v/root/html:/usr/share/nginx/html alexma110/nginx:v1.12.2
#18、容器内注入环境变量
docker run --rm -e E_OPTS=swdxcf -e C_OPTS=ojngf alpine:latest printenv
#19、进入容器
docker exec -it mynginx_with_html /bin/bash
#20、将容器打包成镜像
docker commit -p f14758bea733 alexma110/nginx:v1.12.2_curl
#21、将镜像推到镜像仓库
docker push alexma110/nginx:v1.12.2_curl
6、Dockerfile
#1、打镜像 docker build . -t docker.io/alexma110/nginx:v1.12.2_with_user_workdir #常用dockerfile [root@docker-5 backup]# cat Dockerfile_add_expose FROM docker.io/alexma110/nginx:v1.12.2 ADD index.html /usr/share/nginx/html/index.html EXPOSE 80 [root@docker-5 backup]# cat Dockerfile_centos:7_httpd FROM centos:7 RUN yum install -y httpd CMD ["httpd","-D","FOREGROUND"] [root@docker-5 backup]# cat Dockerfile_entrypoint FROM centos:7 ADD entrypoint.sh /entrypoint.sh RUN yum install -y epel-release -q -y && yum install -y nginx ENTRYPOINT /entrypoint.sh [root@docker-5 backup]# cat Dockerfile_ENV_RUN FROM centos:7 ENV VER 9.11.4-26.P2.el7 RUN yum install bind-$VER -y [root@docker-5 backup]# cat Dockerfile_user_workdir FROM docker.io/alexma110/nginx:v1.12.2 USER nginx WORKDIR /user/share/nginx/html [root@docker-5 dockerfile]# cat Dockerfile FROM alexma110/nginx:v1.12.2 USER root ENV WWW /usr/share/nginx/html ENV CONF /etc/nginx/conf.d RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\ echo 'Asia/Shanghai' >/etc/timezone WORKDIR $WWW ADD index.html $WWW/index.html ADD demo.od.com.conf $CONF/demo.od.com.conf EXPOSE 80 CMD ["nginx","-g","daemon off;"] [root@docker-5 dockerfile]# cat demo.od.com.conf server { listen 80; server_name demo.od.com; root /usr/share/nginx/html; } [root@docker-5 dockerfile]# cat entrypoint.sh #!/bin/bash /sbin/nginx -g "daemon off;" #3、打镜像 docker build . -t docker.io/alexma110/nginx:v1.12.2_with_user_workdir #4、启动镜像 docker run --rm -it --name nginx_user_workdir alexma110/nginx:v1.12.2_with_user_workdir /bin/bash #5、交互式启动 docker run --rm -it --name nginx_index_expose -P alexma110/nginx:v1.12.2_with_index_expose /bin/bash #6、后台启动 docker run --rm -d --name nginx_index_expose -P alexma110/nginx:v1.12.2_with_index_expose #指定网络 docker run -it --rm --name lhwl_2 --net=container:c50b1691ab98 alexma110/nginx:v1.12.2_curl /bin/bash
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)