dockerfile ssh
时间: 2023-08-24 16:09:47 浏览: 79
A Dockerfile with SSH can be used to create a Docker image that allows SSH access to the container. Here is an example Dockerfile:
```
FROM ubuntu:latest
# Install SSH server
RUN apt-get update && apt-get install -y openssh-server
# Create a new user with password 'password'
RUN useradd -m -s /bin/bash docker && \
echo 'docker:password' | chpasswd && \
mkdir /home/docker/.ssh && \
chown docker:docker /home/docker/.ssh && \
chmod 700 /home/docker/.ssh
# Copy public key to authorized_keys file
COPY id_rsa.pub /home/docker/.ssh/authorized_keys
# Start SSH service
CMD ["/usr/sbin/sshd", "-D"]
```
This Dockerfile installs the SSH server on an Ubuntu image, creates a new user 'docker' with a password 'password', creates a .ssh directory for the user, copies the public key to the authorized_keys file, and starts the SSH service when the container is run. To build the image, run the command `docker build -t ssh-image .` in the directory where the Dockerfile is located. To run the container, use the command `docker run -p 2222:22 --name ssh-container -d ssh-image`, which maps port 2222 on the host to port 22 in the container, names the container 'ssh-container', and runs it in detached mode. Once the container is running, you can SSH into it using the command `ssh -p 2222 docker@localhost` and entering the password 'password'.
阅读全文