Docker containers gained popularity as they enabled fast application deployment. This made them ideal solution for hosting applications and services. But to access these applications or services externally, we need to expose the docker ports.
While port forwarding is useful for end-users to access the container applications, developers and system administrators who manage the Docker infrastructure should have ways to connect to Docker containers from the backend.
Developers who code the applications in Docker containers would need to edit or modify these application files. Docker hosting providers who host services would have to adjust the container resources or perform administrative tasks in them.
All these activities require access to the containers. While SSH access is not possible to the Docker containers directly, the containers can be managed from the host machine with some simple steps.
[ You don’t have to lose your sleep to keep your customers happy. Our Hosting Support Specialists cover your servers and support your customers 24/7 at just $9.99/hour. ]
Today, we’ll discuss the 3 ways to connect to Docker containers from backend.
1. Connect to Docker container using ‘docker exec’
The main method to access Docker container from the host machine, is using the ‘docker exec’ command. For that, first login to the Docker host machine via SSH.
Then look up the Docker containers in the host machine and locate the one you want to access. You can do this with the ‘docker ps’ command:
Once the docker container ID is identified, you can connect to it using ‘docker exec -it [container-id] /bin/bash’ command:
You will then be presented with a bash console inside the docker container, where you can perform the tasks specific to that container.
[ Use your time to build your business. We’ll take care of your customers. Hire Our Hosting Support Specialists at $9.99/hr. ]
2. Connect to Docker container using ‘docker attach’
You can also use ‘docker attach [container-name]’ to connect to the container and execute commands in it:
However, the use of ‘docker attach’ is deprecated now and ‘docker exec’ is the more preferred way to access the containers.
3. Execute commands in Docker containers without direct access
The commands ‘docker exec’ and ‘docker attach’ helps to connect to the Docker containers and generate a bash session in it. Via this session, it is easy to manage the files in that specific container.
But, to execute commands directly in the Docker containers, it is not always required to generate a bash session. You can also run commands directly using ‘docker exec’ command.
The ‘docker exec’ command can be executed by giving the command to be executed, as a parameter to it:
docker exec -it 'container-name' 'command-to-execute'
For instance, this snippet shows how we executed a command to create a file inside the container ‘berserk_darwin’ from the host machine.
In short..
Today, we saw the basic commands to be executed inorder to connect to and manage the docker containers. But in a typical Docker setup with a lot of containers, many commands have to be executed multiple times, which is a time consuming process.
To reduce the effort and time spent on managing Docker containers, we’ve automated most container management tasks with our custom scripts.
Our 24/7 Docker management team at Bobcares monitors and manages the Docker infrastructure for our customers, whose businesses range from webhosting services to software development.
If you’d like to know how to manage your Docker system and to get the best out of them for your business, we’d be happy to talk to you.
0 Comments