docker环境的创建过程-国内
一、注册阿里镜像服务账户
- 创建命名空间、仓库
- 设置Registry登录密码
进入容器镜像服务/实例列表,点击列表中的个人实例,点击访问凭证,设置固定密码。
二、在物理机上安装docker
- 在线安装命令:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
- 将当前用户(例如:justin)添加到docker组
sudo cat /etc/group #查看当前系统有哪些组
sudo groupadd docker #添加docker用户组
sudo gpasswd -a $XXX docker
newgrp docker #更新docker用户组
sudo service docker restart #重启docker服务
newgrp - docker #切换当前会话到新group或重启会话
三、配置国内docker镜像源
sudo mkdir /etc/docker
sudo vi /etc/docker/daemon.json
{
"registry-mirrors": [
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}
sudo systemctl daemon-reload
sudo service docker restart
四、登录阿里镜像仓库
sudo docker login --username=${your-own-name} registry.cn-hangzhou.aliyuncs.com
五、容器的启动与进入
在后台运行镜像
- 先使用
prune
命令清理已经停止的的容器,防止名称冲突 --name
给容器命名为u1-h
虚拟主机的名称为Ubuntu18-v
路径映射-d
后台运行容器
docker container prune
docker run --name=u1 -h Ubuntu18 -itd -v /home/zhongjun:/home/zhongjun -v /opt:/opt registry.cn-hangzhou.aliyuncs.com/zhongjun4/ubuntu-18.04:latest /bin/bash
进入容器:
- 以zhongjun用户名登录虚拟主机
- 容器名称是u1(这里可以替换为容器ID)
- 进入以后执行程序bash
docker exec -it -u zhongjun -w /home/zhongjun u1 bash
查看容器的版本:
cat /etc/issue
六、更新ubuntu镜像源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
七、使用dockerfile构建镜像
- 在空目录下创建sources.list文件和Dockerfile文件
FROM ubuntu:20.04
COPY sources.list /etc/apt/
RUN apt-get update; exit 0
RUN apt-get upgrade; exit 0
# 添加中文支持
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get install -y locales
RUN locale-gen zh_CN.UTF-8 && \
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales
RUN locale-gen zh_CN.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN:zh
ENV LC_ALL zh_CN.UTF-8
ENV LC_ALL="C.UTF-8" LANG="C.UTF-8"
# 安装gcc
RUN apt update; exit 0
RUN apt install -y build-essential
# 安装openjdk
RUN set -eux; \
apt-get install -y openjdk-8-jdk; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*; \
rm -rf /var/cache/oracle-jdk8-installer
RUN apt-get install -y ca-certificates-java; \
apt-get clean; \
update-ca-certificates -f; \
rm -rf /var/lib/apt/lists/*; \
rm -rf /var/cache/oracle-jdk8-installer
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
RUN java -version
- 构建ubuntu镜像
docker build -t zhongjun4/ubuntu20-gcc:v1 .
- 拉取镜像
docker pull zhongjun4/ubuntu20-gcc:v1
八、镜像的更新和提交
在运行的镜像中手动更新后,需要提交修改。方法如下:
1、查看容器ID
docker ps
2、提交修改
docker commit -a "zhongjun" -m "安装基础开发工具" 56b9be758ee3 http://registry.cn-hangzhou.aliyuncs.com/zhongjun4/ubuntu-18.04:v1
-a 提交者名称
-m 提交内容的说明
56b9be758ee3 是容器ID
3、推送到仓库
docker push http://registry.cn-hangzhou.aliyuncs.com/zhongjun4/ubuntu-18.04:latest
九、清理镜像节省存储空间
docker rmi $(docker images -f "dangling=true" -q)