[root@server1 ~]# docker run -d -p 1022:22 --name centos7_sshd centos:7 /bin/bash
5231d44687e7e11c3aa839855ff21925988b4d23965dae098255c9fe01ca882c
查看容器是否运行
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5231d44687e7 centos:7 "/bin/bash" 5 seconds ago Up 3 seconds 0.0.0.0:1022->22/tcp centos7_sshd
进入容器
[root@server1 ~]# docker attach 5231d44687e7
下载openssh
[root@5231d44687e7 /]# yum -y install openssh-server
配置验证信息
[root@5231d44687e7 /]# echo "root:123" | chpasswd
启动ssh
[root@5231d44687e7 /]# whereis sshd
sshd: /usr/sbin/sshd
[root@5231d44687e7 /]# /usr/sbin/sshd
宿主机检测是否连接正常
[root@server1 docker]# ssh 10.30.161.200 -p 1022
root@10.30.161.200's password:
[root@5231d44687e7 ~]# exit
commit后面跟的第一个是当前部署好的镜像,后面的参数是提交后的新镜像名称
[root@server1 docker]# docker commit centos7_sshd 7_sshd
sha256:3a25c24a95c55d31f124d09688919271908b75cae9ed98b1ef5241bd471ecc49
将以下所需要用到的文件都放到/docker(自定义的目录)下
mkdir /docker //创建一个镜像目录
ls
[ nginx-1.16.1.tar.gz ] NGINX的源码包
[ Dockerfile ] dockerfile文件
[ run.sh ] 启动服务的脚本
[root@server1 docker]# cat run.sh
#!/bin/bash
/usr/sbin/sshd &
/usr/local/nginx/sbin/nginx -g "daemon off;"
# This is my first nginx Dockerfile
# author: sihye-hwang
# date: 2019-11-05
# Base images,以上面部署好的sshd镜像为基础
FROM 7_sshd:latest
# MAINTAINER
MAINTAINER sihyehwang@yeah.net
# ENV
ENV PATH /usr/local/nginx/sbin:$PATH
# ADD
ADD nginx-1.16.1.tar.gz /usr/local/
# RUN
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum -y install gcc gcc-c++ make ncurses ncurses-devel openssl-devel pcre-devel pcre lftp openssl zlib zlib-devel
RUN groupadd nginx
RUN useradd -s /sbin/nologin -g nginx nginx
# WORKDIR
WORKDIR /usr/local/nginx-1.16.1
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-pcre && make && make install
RUN chown -R nginx:nginx /usr/local/nginx
RUN /usr/local/nginx/sbin/nginx
ADD run.sh /usr/local/sbin/run.sh
RUN chmod 755 /usr/local/sbin/run.sh
EXPOSE 22
EXPOSE 80
# CMD
CMD ["/usr/local/sbin/run.sh"]
[root@server1 docker]# docker build -t centos_ngixn:7 .
Sending build context to Docker daemon 3.366GB
...
---> f321f998c62e
Successfully built f321f998c62e
Successfully tagged centos_ngixn:7
查看当前镜像
[root@server1 docker]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_ngixn 7 f321f998c62e 13 minutes ago 554MB
7_sshd latest 3a25c24a95c5 2 hours ago 284MB
[root@server1 docker]# docker run -d -p 2222:22 -p 8788:80 centos_ngixn:7
b6cb700197dab593647b6dfa0b3de6e7f428ca7b9f12922cfc05f881a8de1fe8
# 检测是否正常运行
[root@server1 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6cb700197da centos_ngixn:7 "/usr/local/sbin/run…" 4 seconds ago Up 3 seconds 0.0.0.0:2222->22/tcp, 0.0.0.0:8788->80/tcp laughing_elbakyan
宿主机访问测试
[root@server1 docker]# curl -I localhost:8788
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 06 Nov 2019 02:45:05 GMT
[root@server1 docker]# ssh localhost -p 2222
root@localhost's password:
到此, 基本的ngixn编译访问以实现
[root@server1 docker]# mkdir /nginx
[root@server1 docker]# echo 'nice to meet U :)' > /nginx/index.html
容器内的发布目录 : /usr/local/nginx/html
[root@server1 docker]# docker run -d -p 2222:22 -p 8788:80 -v /nginx:/usr/local/nginx/html --name nginx02 centos_ngixn:7
cd005fbaaf4455de7579dee75b7a4d1ce8fae4f9e40c9f4e32abfc80c2cf617c
[root@server1 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd005fbaaf44 centos_ngixn:7 "/usr/local/sbin/run…" 3 seconds ago Up 2 seconds 0.0.0.0:2222->22/tcp, 0.0.0.0:8788->80/tcp nginx02
测试
[root@server1 docker]# curl localhost:8788
nice to meet U :)
注: 可使用一个卷创建多个容器