In this post, I intent to outline the how to set up a Docker container for Atmos SFTP and use SSH Keys to login to it.Atmos is an easy to use SFTP with OpenSSH support.
Step 1: Generate SSH Keys
The first step would be to generate the SSH Keys. You can do so in Windows using
ssh-keygen -t rsa -b 2048 -m PEM -f %USERPROFILE%\.ssh\id_rsa
The -m PEM ensures we are create PEM output files directly.
Step 2: Dockerfile for Atmost SFTP
In the next step we need to prepare our SFTP image. We need to ensure our public keys, created in the previous step is loaded into the container. Let us go ahead and create a dockerfile for the purpose.
FROM atmoz/sftp:latest
# Define the SFTP user and UID
ENV SFTP_USER=sftpuser
ENV SFTP_UID=1001
ENV SFTP_PASSWORD=my_password
# Create the .ssh directory and set proper permissions
RUN mkdir -p /home/$SFTP_USER/.ssh && \
chmod 700 /home/$SFTP_USER/.ssh
# Add the public keys for the SFTP user
COPY ssh-keys/authorized_keys /home/$SFTP_USER/.ssh/authorized_keys
# Set the proper file permissions for the .ssh directory and authorized_keys
RUN chmod 600 /home/$SFTP_USER/.ssh/authorized_keys && \
chown -R $SFTP_UID:$SFTP_UID /home/$SFTP_USER/.ssh
# Add the user via the SFTP_USERS environment variable
# Format: user:password:uid:gid:home_directory
# In this case, using authorized keys and setting a home directory
ENV SFTP_USERS="$SFTP_USER:$SFTP_PASSWORD:$SFTP_UID:1001:/home/$SFTP_USER"
# Ensure the home directory has correct ownership
RUN chown -R $SFTP_UID:$SFTP_UID /home/$SFTP_USER
# Optionally create a directory for file uploads and set ownership and permissions
RUN mkdir -p /home/$SFTP_USER/upload && \
chown -R $SFTP_UID:$SFTP_UID /home/$SFTP_USER/upload && \
chmod 755 /home/$SFTP_USER/upload
# Expose the SSH port for SFTP (default: 22)
EXPOSE 22
Thanks to ChatGPT we can create one easily. Core role of docker file is to ensure we load the public key for the SFTP user ‘sftpuser’. For the purpose, we first create an SSH folder for the user under /home/sftpuser/..ssh. The public key which we will store in a file name authorized_keys in our local folder would be now copied over to the container. Additionally, We have also created a folder for uploading data.
Step 3 : Docker Compose
Complimenting the dockerfile is the docker compose file.
version: '3'
services:
sftp:
build: .
container_name: sftp-server
ports:
- "2222:22"
volumes:
- ./data:/home/sftpuser/upload # Local 'data' directory mapped to 'upload' directory in container
restart: always
We can now start our container using docker compose
docker-compose up -d
You can test your SFTP using clients like FileZilla.