docker映射端口与ssh访问或容器访问

映射端口
-d 后台执行
-p映射端口

--privileged 可以使用systemctl

# docker run --privileged -d -p 9000:80 jiqing9006/centos:httpd /sbin/init
e2e33ac1fb2acdef86c995c79eb746fb552417b112210bc5353de74a2f63e4e7

访问容器

root@ThinkPad:/home/jiqing# docker ps -q
e2e33ac1fb2a
root@ThinkPad:/home/jiqing# docker exec -it e2e33ac1fb2a /bin/bash

开启httpd服务

systemctl restart httpd

外部访问

$ sudo netstat -anpl|grep :9000
tcp6       0      0 :::9000                 :::*                    LISTEN      10352/docker-proxy

修改容器密码,默认root的密码是随机的,需要修改一下

[root@e2e33ac1fb2a /]# passwd root
Changing password for user root.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

配置完密码,把22端口映射出去,就可以远程访问了。

安装sshd

[root@e2e33ac1fb2a /]# yum install openssh-server
Loaded plugins: fastestmirror, ovl
base                                                     | 3.6 kB     00:00     
extras                                                   | 3.4 kB     00:00     
updates                                                  | 3.4 kB     00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.zju.edu.cn
 * updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:7.4p1-13.el7_4 will be installed
--> Processing Dependency: openssh = 7.4p1-13.el7_4 for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-7.4p1-13.el7_4.x86_64
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.4.1-6.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-6.el7.x86_64
---> Package openssh.x86_64 0:7.4p1-13.el7_4 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.4.1-6.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                 Arch         Version               Repository     Size
================================================================================
Installing:
 openssh-server          x86_64       7.4p1-13.el7_4        updates       458 k
Installing for dependencies:
 fipscheck               x86_64       1.4.1-6.el7           base           21 k
 fipscheck-lib           x86_64       1.4.1-6.el7           base           11 k
 openssh                 x86_64       7.4p1-13.el7_4        updates       509 k
 tcp_wrappers-libs       x86_64       7.6-77.el7            base           66 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 1.0 M
Installed size: 3.0 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): fipscheck-1.4.1-6.el7.x86_64.rpm                    |  21 kB   00:00     
(2/5): openssh-7.4p1-13.el7_4.x86_64.rpm                   | 509 kB   00:00     
(3/5): fipscheck-lib-1.4.1-6.el7.x86_64.rpm                |  11 kB   00:00     
(4/5): openssh-server-7.4p1-13.el7_4.x86_64.rpm            | 458 kB   00:00     
(5/5): tcp_wrappers-libs-7.6-77.el7.x86_64.rpm             |  66 kB   00:00     
--------------------------------------------------------------------------------
Total                                              1.2 MB/s | 1.0 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : fipscheck-lib-1.4.1-6.el7.x86_64                             1/5 
  Installing : fipscheck-1.4.1-6.el7.x86_64                                 2/5 
  Installing : openssh-7.4p1-13.el7_4.x86_64                                3/5 
  Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                          4/5 
  Installing : openssh-server-7.4p1-13.el7_4.x86_64                         5/5 
  Verifying  : openssh-7.4p1-13.el7_4.x86_64                                1/5 
  Verifying  : openssh-server-7.4p1-13.el7_4.x86_64                         2/5 
  Verifying  : fipscheck-1.4.1-6.el7.x86_64                                 3/5 
  Verifying  : fipscheck-lib-1.4.1-6.el7.x86_64                             4/5 
  Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                          5/5 

Installed:
  openssh-server.x86_64 0:7.4p1-13.el7_4                                        

Dependency Installed:
  fipscheck.x86_64 0:1.4.1-6.el7      fipscheck-lib.x86_64 0:1.4.1-6.el7       
  openssh.x86_64 0:7.4p1-13.el7_4     tcp_wrappers-libs.x86_64 0:7.6-77.el7    

Complete!

启动sshd

[root@e2e33ac1fb2a /]# systemctl restart sshd

外部ssh访问

root@ThinkPad:/home/jiqing# ssh root@172.17.0.1
root@172.17.0.1's password: 
Last login: Sun May  6 16:55:21 2018 from gateway
[root@e2e33ac1fb2a ~]# 

更新镜像

root@ThinkPad:/home/jiqing# docker ps 
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS                  NAMES
e2e33ac1fb2a        jiqing9006/centos:httpd   "/sbin/init"        16 minutes ago      Up 16 minutes       0.0.0.0:9000->80/tcp   peaceful_kilby
root@ThinkPad:/home/jiqing# docker commit e2e33ac1fb2a jiqing9006/centos:httpd
sha256:0824bbef68beae4530c9b6e8997bb4685339865bd521ec1262ffdacf6a9dd0f8
root@ThinkPad:/home/jiqing# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
jiqing9006/centos                 httpd               0824bbef68be        7 seconds ago       496MB

上传到hub中

root@ThinkPad:/home/jiqing# docker push jiqing9006/centos:httpd
The push refers to repository [docker.io/jiqing9006/centos]
611121574145: Pushed 
79655226b20c: Layer already exists 
6572279f7725: Layer already exists 
a4fc816997be: Layer already exists 
43e653f84b79: Layer already exists 
httpd: digest: sha256:47ae6da07149aecba4944351e4f61fa887b52ad4fad16e6e1e8c4fda0a457151 size: 1367

hub库会更新哦!

原文地址:https://www.cnblogs.com/jiqing9006/p/9000646.html