Docker安装配置运行

下载

使用centos7虚拟机操作
基本步骤

3C106C399016A30DCE15D7C5B9A62D7B
如果镜像源地址用不了 就手动创建 /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/docker-ce.repo
然后写入内容:
(1)vi /etc/yum.repos.d/Centos-7.repo

[base]
name=CentOS-7.9.2009 - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.9.2009/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-7.9.2009 - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.9.2009/updates/$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-7.9.2009 - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.9.2009/extras/$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

(2)vi /etc/yum.repos.d/docker-ce.repo

[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

启动docker

#设置开机自启动
systemctl enable docker
#启动
systemctl start docker
#查看状态 
systemctl status docker

配置加速器

1、阿里云镜像加速器控制台页面:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
2、DaoCloud 加速文档页:https://www.daocloud.io/mirror#accelerator-doc

配置加速器

# 创建docker配置目录
sudo mkdir -p /etc/docker

# 写入Docker镜像加速配置
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://t57hdrx1.mirror.aliyuncs.com",
    "http://f1361db2.m.daocloud.io"
  ]
}
EOF
# 重载系统服务配置、重启Docker使镜像配置生效
systemctl daemon-reload
systemctl restart docker

注:现在没有免费的加速器网址用了 现在拉取镜像可以使用下面的方法:
# 标准格式:docker pull docker.m.daocloud.io/library/镜像名:标签 docker pull docker.m.daocloud.io/library/ubuntu:22.04

搜索镜像

#查看镜像
docker images
#搜索镜像 
docker search 镜像名
#下载镜像
docker pull 镜像名

网卡转发

网卡转发

cat <<EOF > /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.ip_forward=1
EOF

sysctl -p /etc/sysctl.d/docker.conf

自定义镜像

(1)创建Dockerfile,内容如下 flask项目
 #Dockerfile
# Base images 基础镜像
FROM ubuntu:18.04

#MAINTAINER 维护者信息
LABEL MAINTAINER=2921509437@qq.com

#RUN 执行以下命令
RUN apt update
RUN apt install python3  python3-pip  -y
RUN pip3 install flask
RUN mkdir -p /data/www/

#拷贝文件至工作目录
COPY app.py  /data/www/app.py

#工作目录
WORKDIR /data/www/

#容器启动时执行命令
CMD ["python3","-u","app.py"]

django项目

FROM centos:7.6.1810
LABEL maintainer wupeiqi@live.com

RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && sed -i '/mirrorlist/d' /etc/yum.repos.d/CentOS-Base.repo && sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/CentOS-Base.repo && yum makecache
# 系统依赖批量安装(gcc、Python编译依赖、wget、git,合并yum减少镜像层数)
RUN yum install gcc wget git -y
RUN yum install -y zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline-devel openssl openssl-devel xz lzma xz-devel sqlite sqlite-devel gdbm gdbm-devel tk tk-devel mysql-devel python-devel libffi-devel

# git全局配置
RUN git config --global user.name "武沛齐"
RUN git config --global user.email "wupeiqi@live.com"

# 创建公共数据目录、设置基础工作目录
RUN mkdir -p /data/
WORKDIR /data/
# Git拉取项目源码
RUN git clone https://gitee.com/wupeiqi/blog.git

# SQLite版本升级
RUN wget https://www.sqlite.org/2023/sqlite-autoconf-3420000.tar.gz --no-check-certificate
RUN tar -zxvf sqlite-autoconf-3420000.tar.gz
WORKDIR /data/sqlite-autoconf-3420000
RUN ./configure
RUN make && make install
ENV LD_LIBRARY_PATH="/usr/local/lib"

# Python3.9源码编译安装
WORKDIR /data/
RUN wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
RUN tar -xvf Python-3.9.5.tgz
WORKDIR /data/Python-3.9.5/
RUN ./configure
RUN make && make install
# 清华稳定镜像源,附带可信host避免ssl断连
RUN pip3.9 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip3.9 config set global.trusted-host pypi.tuna.tsinghua.edu.cn


# 创建Python虚拟环境、安装Django依赖
RUN pip3.9 install virtualenv
RUN virtualenv /envs/dj --python=python3.9
RUN /envs/dj/bin/pip3.9 install django==3.2

# Django项目容器启动配置
WORKDIR /data/blog
CMD ["/envs/dj/bin/python", "manage.py", "runserver", "0.0.0.0:9000"]

(2)创建app.py文件,要和dockerfile放在同一级
(3)创建镜像,命令要在这两个文件的目录下执行

 docker build -t zzq/webi:1.0 . -f Dockerfile

自定义镜像

要确保容器有前台进程,不然 容器一运行 就立刻销毁了,然后宿主机要有后台进程,不然会卡住,不能进行下一步
添加前台进程的操作:

7

对于那些进入系统的容器,如果我们想要退出系统后自动删除容器记录的话,我们可以在-ti后面加上--rm
6

启动/终止容器

#加入-d可在后台一直运行,返回的是一串值
docker run -d -p 8080:8000 zzq/webi:1.0
#修改容器名字
docker run -d -p 8080:8000 --name 名字 zzq/webi:1.0
#查看运行的容器
docker ps
#查看所有状态的容器
docker ps -a
#查看所有容器的id
docker pa -aq
#停止/删除全部的容器
docker stop/rm `docker ps -aq`
#停止容器,可以用空格 隔开放多个id
docker stop 容器id的前三位/名字  
#删除容器
docker rm 容器id前三位/名字
#如果想要再次进入已经存活的容器
docker exec -ti 容器ID bash

posted on 2026-07-09 17:08  fall_auto  阅读(5)  评论(0)    收藏  举报

导航