一、Alpline base镜像
vim Dockerfile
FROM alpine:latest
RUN apk update
RUN echo "root:123456" | chpasswd
RUN apk add -U tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/shanghai" >> /etc/timezone
#RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
RUN mkdir -p /etc/init.d/
RUN echo "#!/bin/bash" >>/etc/init.d/init.sh
RUN echo "source /etc/profile" >>/etc/init.d/init.sh
RUN echo "top -b" >>/etc/init.d/init.sh
RUN chmod a+x /etc/init.d/init.sh
RUN cat /etc/init.d/init.sh
CMD sh /etc/init.d/init.sh
docker build -t alpine-base:v1 .
docker run -itd --name=test alpine-base:v1 sh /etc/init.d/init.sh
二、Centos7 ssh 制作镜像:
在一个干净的目录下制作,没有其他任何文件
[root@localhost ~]#mkdir -p /data
[root@localhost ~]#cd /data
[root@localhost data]# vim Dockerfile,内容见下:
# 生成的新镜像以centos镜像为基础
FROM ansible/centos7-ansible
# 指定作者信息
MAINTAINER by Toyix
# 安装openssh-server
RUN yum -y install openssh-server
RUN yum install yum-utils -y
RUN yum install make -y
RUN yum install net-tools -y
RUN yum install wget -y
RUN yum install lrzsz vim -y
RUN mkdir -p /var/run/sshd
# 指定root密码
RUN /bin/echo 'root:123456'|chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
EXPOSE 22
CMD /usr/sbin/sshd -D
也可以使用下面脚本:
#2020年8月8日 10:57:21
#make docker images centos
#by author toyix
FROM ansible/centos7-ansible
MAINTAINER by toyix
RUN rm -rf /etc/yum.repos.d/*
RUN curl https://mirrors.aliyun.com/repo/Centos-7.repo -o /etc/yum.repos.d/CentOS-Base.repo
RUN curl http://mirrors.aliyun.com/repo/epel-7.repo -o /etc/yum.repos.d/epel.repo
RUN yum install net-tools -y
RUN yum install openssh-server openssh -y
RUN yum install vim wget lrzsz -y
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 60/g' /etc/ssh/sshd_config
RUN sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 3/g' /etc/ssh/sshd_config
COPY ssh_host_rsa_key /etc/ssh/
COPY ssh_host_ecdsa_key /etc/ssh/
COPY ssh_host_ed25519_key /etc/ssh/
RUN chmod 600 -R /etc/ssh/
WORKDIR /root
RUN echo "123456"|passwd --stdin root
RUN yum remove ansible* -y; find / -name "*ansible*" |xargs rm -rf {}
CMD /usr/sbin/sshd;/bin/bash
# 注: 这里这将3个key文件放到当前目录下
推介使用下面方式(本人使用方法)
此方法,可以在启动容器时,同时启动要启动的服务
#2020年12月15日 10:46:12
#make docker images centos
#by author toyix
FROM ansible/centos7-ansible
MAINTAINER by toyix
RUN rm -rf /etc/yum.repos.d/*
RUN curl http://mirrors.163.com/.help/CentOS7-Base-163.repo -o /etc/yum.repos.d/CentOS-Base.repo
RUN yum install net-tools -y
RUN yum install openssh-server openssh -y
RUN yum install vim wget lrzsz -y
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 60/g' /etc/ssh/sshd_config
RUN sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 3/g' /etc/ssh/sshd_config
RUN mkdir -p /etc/init.d/sh
#COPY init.sh /etc/init.d/sh/
RUN echo "#!/bin/bash" >>/etc/init.d/sh/init.sh
RUN echo "source /etc/profile" >>/etc/init.d/sh/init.sh
RUN echo "/usr/sbin/sshd -D" >>/etc/init.d/sh/init.sh
RUN echo "/bin/bash" >>/etc/init.d/sh/init.sh
RUN chmod a+x /etc/init.d/sh/init.sh
RUN cat /etc/init.d/sh/init.sh
RUN ssh-keygen -A
WORKDIR /root
RUN echo "123456"|passwd --stdin root
RUN yum remove ansible* -y; find / -name "*ansible*" |xargs rm -rf {}
RUN sed -i '$a \source /etc/profile' ~/.bashrc
CMD /etc/init.d/sh/init.sh
# 注: 直接docker build -t bigdata/centos7-ssh:v7.3 - <filename即可
#这是centos7.3的镜像,如需要7.9,则可以在前部分加一行 RUN yum update -y
mkdir -p /data/soft/sh
vim /data/soft/sh/init.sh
#!/bin/bash
source /etc/profile
/usr/sbin/sshd -D
/bin/bash
#其他启动服务,在下面添加
制作完成后启动镜像
[root@localhost sh]# docker run -itd --privileged -v /data/soft/sh:/etc/init.d/sh --name=centos_test --restart=always centos7_ssh:latest
制作镜像
[root@localhost data]# docker build -t bigdata/centos7-ssh:v7.7 .
成功提示
Successfully built 610b4d876117
查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bigdata/centos7-ssh v7.7 610b4d876117 11 minutes ago 250 MB
docker.io/centos latest 831691599b88 6 weeks ago 215 MB
创建并启动容器
[root@localhost data]# docker run -itd bigdata/centos7-ssh:v7.7
97f5c23b70e119549a58fe98b52d260d61eea4273d45c5289d3fe5a18e47741a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
97f5c23b70e1 bigdata/centos7-ssh:v7.7 "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 22/tcp blissfu
登录容器
[root@localhost ~]# ssh -l root 172.17.0.2
导出镜像,其他机器可使用
[root@localhost ~]# docker save 610b4d876117 >centos7.7_ssh.tar
导入镜像
[root@localhost ~]# docker load <centos7.7_ssh.tar
如果导入后,发现镜像信息为none
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 610b4d876117 24 minutes ago 250 MB
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker tag 610b4d876117 centos7_ssh:v7.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7_ssh v7.7 610b4d876117 25 minutes ago 250 MB
创建容器
[root@localhost ~]# docker run -itd centos7_ssh:v7.7
78e85b076f1917316fb7ff511626331afb9faf623f89fc0da394ba4994e25670
[root@localhost data]# ssh -l root 172.17.0.2
root@172.17.0.2's password:
常用命令
创建随docker进程启动的容器
[root@localhost ~]# docker run -itd --privileged=true --net=none --name=bigdata_192.168.1.72 --restart=always centos7_ssh:latest
改IP
[root@localhost ~]# pipework br0 bigdata_192.168.1.72 192.168.1.72/24@192.168.1.112
进入容器
当不知道ROOT密码或没有开启SSH时,可以先进入,再改密码
docker exec -it amazing_volhard bash
删除正在运行容器
docker container rm -f 97f5c23b70e1
删除镜像
docker rmi centos7_ssh
导出镜像
[root@localhost ~]# docker save 610b4d876117 >centos7.7_ssh.tar
导入镜像
[root@localhost ~]# docker load <centos7.7_ssh.tar
制作镜像
docker build -t bigdata/centos7-ssh:v7 .
制作容器为镜像
docker commit b492f5d6d628 centos7_nginx:v1
如果导入后,发现镜像信息为
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 610b4d876117 24 minutes ago 250 MB
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker tag 610b4d876117 centos7_ssh:v7.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7_ssh v7.7 610b4d876117 25 minutes ago 250 MB
注:
一般缺少环境支持,如ifconfig,安装
如果pull慢,可以使用国内加速
[root@localhost data]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://nl3v7ax3.mirror.aliyuncs.comm"]
}
注:这里需要在阿里云中申请,请不要照抄
也可以使用其他国内源
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
yum install net-tools.x86_64
yum install -y initscripts
yum install openssh-clients -y
注:
例如启动jenkins是可以使用:
[root@localhost local]# docker run -itd \
--privileged=true \
--publish 64080:8080 \
--name jenkins \
--restart unless-stopped \
-v /usr/local/talent:/usr/local/talent/ \
-v /home/jenkins_ThinBackup:/home/jenkins_ThinBackup \
-v /etc/init.d/DockerJenkins_startsh:/etc/init.d/sh \
jenkins:latest
这里最后面也可以加 /usr/sbin/init,即启动systemd
mysql
FROM centos7_nothing:v1
COPY mysqld /etc/init.d/
COPY mysql /usr/local/mysql/
RUN useradd -u 1000 otoyix \
&& useradd -u 1001 mysql \
&& yum install net-tools perl autoconf libaio numactl -y \
&& mkdir -p mkdir -p /var/log/mysql \
&& rm -rf /opt/ansible \
&& echo -e "/etc/init.d/mysqld start\ntop" >/usr/init.sh \
&& echo "export PATH=\$PATH:/usr/local/mysql/bin" >>/etc/profile \
&& source /etc/profile
WORKDIR /root
CMD sh /usr/init.sh
注意上面的mysql目录为安装目录
--------------------------------end