https://tecadmin.net/setting-up-ubuntu-docker-container-with-ssh-access/
Docker is an open-source platform that allows developers to automate the deployment, scaling, and management of applications. It does so by creating lightweight, self-sufficient containers, that can run virtually anywhere. SSH (Secure Shell) is a protocol that provides a secure channel over an unsecured network, typically used to log into another computer over a network and execute commands. This article provides a step-by-step guide on setting up an Ubuntu Docker container with SSH access.
Prerequisites
Before we begin, you need the following software installed:
- Docker: You can install Docker by following the instructions in the official Docker documentation.
- SSH client: For Linux and MacOS, the SSH client is usually pre-installed. Windows users can use PuTTY or the built-in SSH client in Windows 10 and newer versions.
Step 1: Pull the Ubuntu Image
The first step is to pull the Ubuntu image from Docker Hub. Docker Hub is a cloud-based registry service that allows you to link code repositories, build details and more. Use the following command:
docker pull ubuntu
Step 2: Create a Dockerfile
The Dockerfile is a text document that contains all the commands a user can call on the command line to assemble an image. You can create a new Dockerfile and open it in a text editor. Here is a sample Dockerfile to set up SSH in an Ubuntu container:
# Use the official image as a parent image
FROM ubuntu
# Update the system
RUN apt-get update && apt-get upgrade -y
# Install OpenSSH Server
RUN apt-get install -y openssh-server
# Set up configuration for SSH
RUN mkdir /var/run/sshd
RUN echo 'root:secret_password' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise, user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
# Expose the SSH port
EXPOSE 22
# Run SSH
CMD ["/usr/sbin/sshd", "-D"]
Dockerfile
Replace “secret_password” with the password you want to use for SSH. Save this Dockerfile.
Step 3: Build the Docker Image
Once your Dockerfile is ready, you can build your Docker image. Use the following command:
docker build -t ubuntu-ssh .
This command will build an image from your Dockerfile and tag it as ubuntu-ssh.
Step 4: Run the Docker Container
Now you’re ready to run your Docker container with the following command:
docker run -d -p 2222:22 ubuntu-ssh
This will run your Docker container in detached mode (-d) and map your machine’s port 2222 to your Docker container’s port 22, which is the default SSH port.
Step 5: Connect to the Docker Container via SSH
Finally, you can connect to your Docker container via SSH. Use the following command:
ssh root@localhost -p 2222
You’ll be prompted to enter the password you set earlier in the Dockerfile. Once you’ve entered it, you’ll be connected to your Ubuntu Docker container via SSH!
Conclusion
Docker and SSH are powerful tools in the realm of devOps and development. By integrating them, you can create a more flexible, more secure, and more powerful development environment. This comprehensive guide walked you through the process of setting up an Ubuntu Docker container with SSH access. With this knowledge, you can now create and access Docker containers with ease and confidence. Always remember to exercise good security practices, like changing default passwords and regularly updating software, to keep your environments safe and secure.
标签:container,Dockerfile,ssh,install,docker,Docker,your,SSH From: https://www.cnblogs.com/adolfmc/p/17711416.html