Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SSH to connect docker containers in Docker

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use SSH to connect the docker container in Docker, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

What is Docker?

Docker is an open source project that was born in early 2013 as an amateur project within dotCloud. It is based on the Go language developed by Google Company. The project later joined the Linux Foundation, complied with the Apache 2.0 protocol, and the project code was maintained on GitHub.

Docker has received so much attention and discussion since its inception that dotCloud later changed its name to Docker Inc. Redhat has concentrated support for Docker;Google in its RHEL6.5 and is widely used in its PaaS products.

The goal of the Docker project is to implement a lightweight operating system virtualization solution. Docker is based on technologies such as Linux container (LXC).

Docker is further encapsulated on the basis of LXC, so that users do not need to care about the management of the container, making the operation easier. Users can manipulate Docker containers as easily as a fast and lightweight virtual machine.

The following picture compares the differences between Docker and traditional virtualization methods. It can be seen that the container is virtualized at the operating system level, directly reusing the operating system of the local host, while the traditional method is implemented at the hardware level.

Why use Docker?

As a new way of virtualization, Docker has many advantages compared with traditional virtualization.

First of all, the startup of the Docker container can be achieved in seconds, which is much faster than the traditional virtual machine approach. Secondly, Docker has a high utilization of system resources, and thousands of Docker containers can be run on one host at the same time.

In addition to running the application, the container basically does not consume additional system resources, so that the performance of the application is very high, and the cost of the system is as low as possible. It takes 10 virtual machines to run 10 different applications in traditional virtual machine mode, while Docker only needs to start 10 isolated applications.

Specifically, Docker has great advantages in the following aspects.

Faster delivery and deployment

For developers and operators (devop), what they want most is to create or configure it all at once, which can run smoothly anywhere.

Developers can use a standard image to build a set of development containers, and after development is complete, operators can directly use this container to deploy code. Docker can quickly create containers, quickly iterate over applications, and make the entire process visible, making it easier for other members of the team to understand how the application is created and works. Docker containers are very light and fast! The start-up time of the container is seconds, saving a lot of time in development, testing and deployment.

More efficient virtualization

The operation of the Docker container does not require additional hypervisor support, it is kernel-level virtualization, so higher performance and efficiency can be achieved.

Easier migration and expansion

Docker containers can run on almost any platform, including physical machines, virtual machines, public clouds, private clouds, personal computers, servers, and so on. This compatibility allows users to migrate an application directly from one platform to another.

Easier management

With Docker, you can replace a lot of previous update work with only a small change. All changes are distributed and updated incrementally, resulting in automated and efficient management.

Compare the summary of traditional virtual machines

Feature container virtual machine startup minutes, that is, hard disk use is generally MB, generally GB performance is close to native weaker than system support, single machine support thousands of containers, generally dozens of

Install Docker

I originally planned to install Docker on CentOS6, but eventually gave up because the low version of kernel that comes with CentOS6 caused Docker to fail to start (kernel upgrade is too cumbersome).

Below, use CentOS7 to install Docker. I install it in the way of a virtual machine. The installed virtual machine must be able to access the external network.

Docker is already included in the CentOS-Extras library of CentOS7 system, so you can install it directly:

$sudo yum install docker

Start the Docker service after installation and have it load automatically when the system starts:

$sudo service docker start$ sudo chkconfig docker on

Get the image

You can use the docker pull command to get the desired image from the repository.

The following example downloads a Centos6 from the Docker Hub repository and installs an image of jdk7:

$docker pull tcbenkhard/centos6-jdk7

List local images

Use docker images to display local images that already exist.

$docker images

Start the container

There are two ways to start a container, one is to create a new container based on the image and start it, and the other is to restart the container in the stopped state.

Because Docker containers are so lightweight, users often delete and create new containers at any time.

The following command starts a bash terminal that allows the user to interact.

$docker run-t-I docker.io/tcbenkhard/centos6-jdk7 / bin/bash [root@ffe81683c404 /] #

The-t option lets Docker assign a pseudo terminal (pseudo-tty) and bind it to the standard input of the container, and-I keeps the standard input of the container open.

When using docker run to create containers, standard operations for Docker to run in the background include:

(1) check whether the specified image exists locally. Download it from the public repository if it does not exist.

(2) create and start a container using an image

(3) assign a file system and mount a read-write layer outside the read-only mirror layer

(4) bridging a virtual interface to the container from the bridge interface configured by the host host

(5) configure an ip address from the address pool to the container

(6) execute user-specified applications

(7) the container is terminated after execution

You can use the following command to view CentOS version information:

$cat / etc/redhat-release

Modify root password

Use the passwd password to change the password (if prompted without this command line to use yum install passwd installation):

$passwd xxx password xxx confirm password

Install Openssh

Install ssh server/ssh client using the following command:

$sudo yum-y install openssh-server$ sudo yum-y install openssh-clients

Modify the SSH configuration file with the following options, remove the # comment, and enable four options:

$vi / etc/ssh/sshd_configRSAAuthentication yes # enable RSA authentication PubkeyAuthentication yes # enable public key private key pairing authentication AuthorizedKeysFile .ssh / authorized_keys # public key file path (same as the file generated above) PermitRootLogin yes # root can log in using ssh

Restart the ssh service and set it to boot:

$service sshd restart$ chkconfig sshd on

Exit the container and save changes

Use the exit command or ctrl+C to exit the currently running container:

[root@ffe81683c404 /] # exit

Note: the above ffe81683c404 is the ID of the container, and the only ID used to save after exit.

When it's over, we use exit to exit, and now that our container has been changed, use the docker commit command to submit the updated copy.

$sudo docker commit-m 'install openssh'-a' Docker Newbee' ffe81683c404 centos6-jdk7:ssh4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c

Where-m specifies the submitted description information, just like the version control tool we use;-a can specify the updated user information; then the ID; used to create the image container finally specifies the warehouse name and tag information of the target image. The ID information of this image will be returned after the image is successfully created.

After the submission, there will be an extra centos6-jdk7:ssh image in the docker.

Start the new container and get through port 22

Start the new image and map port 50001 of the docker server to port 22 of the container:

$docker run-d-p 50001usr/sbin/sshd 22 centos6-jdk7:ssh / usr/sbin/sshd-D

Ssh connection container:

The above is how to use SSH to connect the docker container in Docker. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report