Operations 4 min read

How to Enable SSH Access in a Docker Container Using a CentOS Image

Learn step‑by‑step how to download a CentOS base image, create a Dockerfile that installs and configures OpenSSH, build a new SSH‑enabled image, run the container with port mapping, and verify connectivity via an SSH client, turning a closed container into a remote‑accessible environment.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Enable SSH Access in a Docker Container Using a CentOS Image

After pulling an Ubuntu image and starting a container, the container is isolated; this guide shows how to bridge communication by configuring SSH using a CentOS image.

Steps

Download the CentOS base image: docker pull centos Create a Dockerfile that installs OpenSSH and sets up users and passwords.

Build a new image from the Dockerfile: docker build -rm -t dys/centos:ssh . Run a container from the new image with port mapping: docker run -d -p 22 dys/centos:ssh Test the SSH connection: ssh [email protected] -p 1035 The Dockerfile content:

FROM centos
MAINTAINER dys "[email protected]"
RUN yum install -y openssh openssh-server openssh-clients
RUN mkdir -p /var/run/sshd
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN /bin/echo 'root:aaaaaa' | chpasswd
RUN useradd dys
RUN /bin/echo 'dys:aaaaaa' | chpasswd
RUN /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
EXPOSE 22
CMD /usr/sbin/sshd -D

After building, docker images shows the new dys/centos:ssh image. Running docker ps confirms the container is up, and the port mapping (e.g., 0.0.0.0:1035->22/tcp) allows SSH access.

Connecting with the provided user dys and password aaaaaa logs into the container’s shell, as shown in the screenshots.

Docker container list
Docker container list
SSH into container
SSH into container

The crucial part is writing the Dockerfile; further details will be covered later.

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.

DockerDevOpsContainerDockerfileCentOS
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.