yum -y install docker
启动docker
systemctl start docker
设置开机启动
systemctl start docker
查找centos镜像
docker search centos
拉取centos镜像
docker pull centos
查看cenots镜像
docker images
docker run -i -t centos /bin/bash
yum -y install net-tools
[root@b5926410fe60 /]# yum install passwd openssl openssh-server -y
启动sshd:
# /usr/sbin/sshd -D //直接/usr/sbin/sshd启动也可以
这时报以下错误:
[root@ b5926410fe60 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
执行以下命令解决:
[root@b5926410fe60 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ‘‘
[root@b5926410fe60 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ‘‘
[root@b5926410fe60 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ‘‘
然后,修改 /etc/ssh/sshd_config 配置信息:
UsePAM yes 改为 UsePAM no
UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no
[root@b5926410fe60 /]# sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
[root@b5926410fe60 /]# sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
修改完后,重新启动sshd
[root@b5926410fe60 /]# /usr/sbin/sshd
可以查看到22端口已经起来
[root@597847f19876 /]# netstat -tulnp|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 90/sshd
tcp6 0 0 :::22 :::* LISTEN 90/sshd
[root@b5926410fe60 /]# passwd root
四. 查看容器ip地址(如果宿主机是linux操作系统则跳过这一步)
[root@b5926410fe60 /]# ip addr ls eth0
84: eth0@if85: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
[root@slave1 ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
597847f19876 centos "/bin/bash" 17 minutes ago Up 16 minutes suspicious_mccarthy
[root@slave1 ~]# docker commit 597847f19876 zhuzhou/centos-1
sha256:8bb2fccf7a1e08a8b3fa30ef6120ae401673180768260336e433a1d00b8c3029
[root@slave1 ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
597847f19876 centos "/bin/bash" 17 minutes ago Up 17 minutes suspicious_mccarthy
[root@slave1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhuzhou/centos-1 latest 8bb2fccf7a1e 10 seconds ago 280.4 MB
docker.io/centos latest 196e0ce0c9fb 7 weeks ago 196.6 MB
[root@slave1 ~]# docker run -i -t -p 32:22 centos /bin/bash
[root@0a186c0ffe7d /]# exit
exit
[root@slave1 ~]#
[root@slave1 ~]#
[root@slave1 ~]# docker run -i -t -p 32:22 zhuzhou/centos-1 /bin/bash
[root@26a868e6496e /]#
[root@741c0bf053d6 /]# /usr/sbin/sshd
WARNING: ‘UsePAM no‘ is not supported in Red Hat Enterprise Linux and may cause several problems.
[root@741c0bf053d6 /]#
(1) 可以通过xshell工具直接连上本机ip:32端口,可以通过sftp工具直接传输文件到容器内。
(2)通过cp命令将宿主机传送到容器中
[root@slave1 home]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
741c0bf053d6 zhuzhou/centos-1 "/bin/bash" 8 minutes ago Up 8 minutes 0.0.0.0:32->22/tcp romantic_bassi
[root@slave1 home]# docker cp /home/hadoop
hadoop/ hadoop-3.0.0-alpha4/ hadoop-3.0.0-alpha4.tar.gz
[root@slave1 home]# docker cp /home/hadoop-3.0.0-alpha4.tar.gz 741c0bf053d6:/home/
[root@slave1 home]#
[root@slave1 home]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
741c0bf053d6 zhuzhou/centos-1 "/bin/bash" 17 minutes ago Up 17 minutes 0.0.0.0:32->22/tcp romantic_bassi
[root@slave1 home]# docker commit 741c0bf053d6 zhuzhou/centos-tomcat
sha256:70a6934fbd03fb95dda31e8acf240c07ef8a0aed39853a9e582e34e7ee7ddded
[root@slave1 home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhuzhou/centos-tomcat latest 70a6934fbd03 6 seconds ago 1.067 GB
zhuzhou/centos-1 latest 8bb2fccf7a1e 25 minutes ago 280.4 MB
docker.io/centos latest 196e0ce0c9fb 7 weeks ago 196.6 MB
[root@slave1 ~]# docker run -d -p 8080:8080 -p 32:22 zhuzhou/centos-tomcat /usr/sbin/sshd -D
435a824136e5820c56fa86d78d1131495191a4c01330671cf546a8660b315062
[root@slave1 ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
435a824136e5 zhuzhou/centos-tomcat "/usr/sbin/sshd -D" 7 seconds ago Up 6 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:32->22/tcp fervent_euclid
[root@slave1 ~]#
[root@slave1 ~]# docker save -o /home/docker-centos-tomcat.tar zhuzhou/centos-tomcat
[root@slave1 ~]# cd /home/
[root@slave1 home]# ls
docker-centos-tomcat.tar hadoop hadoop-3.0.0-alpha4 hadoop-3.0.0-alpha4.tar.gz jdk-8u101-linux-x64.rpm joe
[root@slave1 home]#
[root@slave2 home]# docker load -i docker-centos-tomcat.tar
cf516324493c: Loading layer 205.2 MB/205.2 MB
d849bfac7f38: Loading layer 84.08 MB/84.08 MB
5c0e5e62d633: Loading layer 789.3 MB/789.3 MB
Loaded image: zhuzhou/centos-tomcat:latest MB
[root@slave2 home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhuzhou/centos-tomcat latest 70a6934fbd03 About an hour ago 1.067 GB
[root@slave1 docker-build]# cd /home/docker-build/
[root@slave1 docker-build]# ls
apache-tomcat-7.0.79 apache-tomcat-7.0.79.tar.gz jdk1.7.0_80 jdk-7u80-linux-x64.tar.gz
[root@slave1 docker-build]# vi Dockerfile
FROM centos
MAINTAINER linux
#指定RUN、CMD与ENTRYPOINT命令的工作目录
WORKDIR /home
#安装网络工具和ssh
RUN yum -y install net-tools
#把java与tomcat添加到容器中
ADD apache-tomcat-7.0.79.tar.gz /home
ADD jdk-7u80-linux-x64.tar.gz /home
#安装jdk和tomcat,实际测试中加下面两个解压语句会报错,docker会自动进行解压
#RUN tar -xzvf apache-tomcat-7.0.79.tar.gz
#RUN tar -xzvf jdk-7u80-linux-x64.tar.gz
#配置环境
ENV JAVA_HOME /home/jdk1.7.0_80
ENV CLASS_PATH $JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
#设置tomcat 自启动,测试tomcat没有起来
CMD /home/apache-tomcat-7.0.79/bin/startup.sh
#容器运行时监听的端口
EXPOSE 8080
docker build -t zhuzhou/centos-tomcat-2 .
注:如果要ADD本地文件,则本地文件必须在 docker build <PATH>,指定的<PATH>目录下。
原文地址:http://10676568.blog.51cto.com/10666568/1978744