Cloud Native 4 min read

Add Nginx to Your Docker SSH Image and Access It from Outside

This guide walks through installing Nginx inside a Docker container that already runs an SSH server, creating a new image, and launching a container with both SSH (port 22) and HTTP (port 80) exposed so the Nginx welcome page can be accessed externally.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Add Nginx to Your Docker SSH Image and Access It from Outside

Idea

(1) Log into the container via SSH and install Nginx.

(2) Create a new Nginx image.

(3) Start a container from the new image and test Nginx.

Steps

1. Install Nginx

Run a container with the SSH port exposed: docker run -d -p 22 dys/centos:ssh Find the host port mapped to the container's 22 (e.g., 1035) and SSH into the container: ssh [email protected] -p 1035 Inside the container (CentOS), install Nginx:

yum install gcc-c++
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
yum install wget
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
make && make install

Start Nginx: /usr/local/nginx/sbin/nginx Test locally: curl http://127.0.0.1 You should see the Nginx welcome page.

2. Create a New Nginx Image

Stop the running container and note its ID (e.g., 9708d2e3f613): docker stop 9708d2e3f613 Commit the container as a new image named centos_nginx:

docker commit 9708d2e3f613 centos_nginx

3. Run the New Image

Start a container from the new image, exposing both SSH (22) and HTTP (80) ports: docker run -d -p 22 -p 80 centos_nginx Check the port mapping (e.g., host 1041 → 22, host 1042 → 80): docker ps SSH into the container again if needed and start Nginx: /usr/local/nginx/sbin/nginx Now access Nginx from the host using the mapped port: http://<host_ip_or_domain>:1042 The Nginx welcome page should be displayed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ImageSSH
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.