opened image

How to connect to a Docker container via sFTP

Connecting to a Docker container via sFTP can be useful for developers who work with files inside the container. For example, if you are developing a web application and want to upload files to the server, you can connect to the container via sFTP and upload files to the server from your development environment.
Additionally, when connecting to a Docker container via sFTP, you can use various sFTP functions to manage files. 



Deploying a Docker container via sFTP.



To connect to a Docker container via sFTP, you first need to create a container that will be running on a remote server. You can use the docker run command for this. For example, to run a container with nginx on port 2222 and install OpenSSH, you can use the following commands:
 

docker run -d -p 2222:22 --name nginx_latest nginx

 

docker exec -it nginx_latest apt-get update

 

docker exec -it nginx_latest apt-get install -y openssh-server



 

These commands will create a new container named "nginx_latest" based on the Nginx image and install OpenSSH inside the container. 



Set a password for the selected user.

Set a password for root (or any other user you want to connect with) by running the following command:

 

 

 

docker exec -it nginx_latest passwd root

 


Enter the password twice.
 


 

 

  • Edit the /etc/ssh/sshd_config file inside the container, replacing PermitRootLogin without-password with PermitRootLogin yes. To connect and edit it, use the following command:

 

docker exec -it nginx_latest bash


 

Use your favorite editor. If it's not available, install it.



 

 

  • Restart the SSH server by running the command:

 

 

/etc/init.d/ssh restart



 

Connecting to a Docker container via IP using sFTP.




To do this, you need to determine the IP address of the container and the port on which the SSH server is running. You can do this using the docker inspect command. For example, if the container is running on port 2222, you can use the following command to get the IP address:

 

 

 

docker inspect --format '{{ .NetworkSettings.IPAddress }}' nginx_latest

 




 

This command will return the IP address of the container. Then you can connect to the container via sFTP using the user credentials set inside the container. For example, using the sftp command:

 

 

 

 

sftp [email protected]

 


 

Where root is the username set inside the container, and ip-address is the IP address of the container (172.17.0.4) obtained using the docker inspect command.




 

Connecting with FileZilla.

 

  1. In the "File" menu, select "Site Manager" (or use the hotkey "Ctrl+S").

  2. In the "Site Manager" window, click the "New Site" button and enter the name of your site.
  3. In the "Host", "Port", "Username", and "Password" fields, enter the corresponding values using the information from your Docker container. By default, the username is "root" and the port is "2222".
  4. In the "Protocol" dropdown, select "SFTP - SSH File Transfer Protocol".
  5. Click "Connect". If all settings are correct, you should be connected to your Docker container via sFTP using the sFTP client.

 


 

 

Now you can connect to your Docker container via sFTP, transfer files, and perform other file operations on the remote server.

 

​​​​​​​We also suggest you other useful articles: