如何通过FTP连接Microsoft SQL Server Docker容器?
Hey there! Let's break down your problem step by step since you're new to Docker and working with the SQL Server image.
You can't run a new container with the same name (sql1) as an existing one—Docker prevents this to avoid conflicts. You have two straightforward options here:
Option 1: Remove the existing container (if you don't need it anymore)
First stop the running container:sudo docker stop sql1Then delete it:
sudo docker rm sql1Now you can run your new container command with the
-p 21:21port mapping included.Option 2: Rename the existing container (if you want to keep it for reference)
Rename the old container to something likesql1-old:sudo docker rename sql1 sql1-oldNow you can safely launch your new
sql1container with the FTP port mapped.
The official SQL Server Docker image doesn't include an FTP server by default. You have two reliable ways to add this functionality:
Option A: Build a custom Docker image (recommended for long-term use)
This method creates a reusable image with both SQL Server and FTP pre-configured—your settings will persist across container restarts.
Create a file named
Dockerfilewith the following content:FROM mcr.microsoft.com/mssql/server:2019-latest USER root # Install vsftpd (a popular, lightweight FTP server) RUN apt-get update && apt-get install -y vsftpd \ && rm -rf /var/lib/apt/lists/* # Configure vsftpd for secure, local user access RUN echo "listen=YES" >> /etc/vsftpd.conf \ && echo "listen_ipv6=NO" >> /etc/vsftpd.conf \ && echo "anonymous_enable=NO" >> /etc/vsftpd.conf \ && echo "local_enable=YES" >> /etc/vsftpd.conf \ && echo "write_enable=YES" >> /etc/vsftpd.conf \ && echo "local_umask=022" >> /etc/vsftpd.conf \ && echo "dirmessage_enable=YES" >> /etc/vsftpd.conf \ && echo "use_localtime=YES" >> /etc/vsftpd.conf \ && echo "xferlog_enable=YES" >> /etc/vsftpd.conf \ && echo "connect_from_port_20=YES" >> /etc/vsftpd.conf \ && echo "chroot_local_user=YES" >> /etc/vsftpd.conf \ && echo "allow_writeable_chroot=YES" >> /etc/vsftpd.conf \ && echo "pasv_min_port=30000" >> /etc/vsftpd.conf \ && echo "pasv_max_port=30010" >> /etc/vsftpd.conf # Create a dedicated FTP user (replace the password with your own strong one) RUN useradd -m ftpuser \ && echo "ftpuser:<YourFtpStrong@Passw0rd>" | chpasswd # Switch back to the default mssql user (required for SQL Server to run correctly) USER mssql # Start both SQL Server and vsftpd when the container launches CMD /bin/bash -c "/opt/mssql/bin/sqlservr & /usr/sbin/vsftpd & wait"Build your custom image:
sudo docker build -t mssql-with-ftp:2019-latest .Run the new container with all required ports (including FTP passive mode ports):
sudo docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong@Passw0rd>" \ -p 1433:1433 -p 21:21 -p 30000-30010:30000-30010 \ --name sql1 --hostname sql1 \ -d mssql-with-ftp:2019-latest
Option B: Install FTP temporarily in the existing container (for testing only)
This works for quick tests, but all changes will be lost when the container restarts—Docker containers are ephemeral by default.
Access the container as root (since you need admin rights to install software):
sudo docker exec -it -u root sql1 bashInstall vsftpd:
apt-get update && apt-get install -y vsftpdConfigure vsftpd using the same settings as in the Dockerfile above (edit
/etc/vsftpd.confwith a text editor likenano).Create an FTP user and start the service:
useradd -m ftpuser echo "ftpuser:<YourFtpStrong@Passw0rd>" | chpasswd /usr/sbin/vsftpd
- Use passive mode: Most FTP clients use passive mode by default. Make sure you've mapped the passive port range (30000-30010) and opened those ports on your server's firewall (e.g., with
ufw:sudo ufw allow 21/tcp && sudo ufw allow 30000:30010/tcp). - Use the dedicated FTP user: Don't try to connect with the
saaccount—that's only for SQL Server authentication. Use theftpuseryou created instead. - Enforce strong passwords: Both your
SA_PASSWORDand FTP user password must meet complexity requirements (mix of uppercase, lowercase, numbers, and special symbols) to avoid container startup failures or security risks.
内容的提问来源于stack exchange,提问作者Laurent K

