首页 > 系统相关 >docker install ubuntu ssh

docker install ubuntu ssh

时间:2023-09-18 11:35:02浏览次数:39  
标签:container Dockerfile ssh install docker Docker your SSH

https://tecadmin.net/setting-up-ubuntu-docker-container-with-ssh-access/

 

Docker is an open-source platform that allows developers to automate the deployment, scaling, and management of applications. It does so by creating lightweight, self-sufficient containers, that can run virtually anywhere. SSH (Secure Shell) is a protocol that provides a secure channel over an unsecured network, typically used to log into another computer over a network and execute commands. This article provides a step-by-step guide on setting up an Ubuntu Docker container with SSH access.

Prerequisites

Before we begin, you need the following software installed:

  • Docker: You can install Docker by following the instructions in the official Docker documentation.
  • SSH client: For Linux and MacOS, the SSH client is usually pre-installed. Windows users can use PuTTY or the built-in SSH client in Windows 10 and newer versions.

Step 1: Pull the Ubuntu Image

The first step is to pull the Ubuntu image from Docker Hub. Docker Hub is a cloud-based registry service that allows you to link code repositories, build details and more. Use the following command:

docker pull ubuntu 

Step 2: Create a Dockerfile

The Dockerfile is a text document that contains all the commands a user can call on the command line to assemble an image. You can create a new Dockerfile and open it in a text editor. Here is a sample Dockerfile to set up SSH in an Ubuntu container:

ADVERTISEMENT
<iframe frameborder="no" height="240" id="vli_sf_vi_1879779679_1" marginheight="0" marginwidth="0" scrolling="no" src="https://assets.vlitag.com/plugins/safeframe/src/html/r.html" width="320"></iframe> <iframe frameborder="no" height="16" marginheight="0" marginwidth="0" scrolling="no" width="16"></iframe>

 


# Use the official image as a parent image
FROM ubuntu

# Update the system
RUN apt-get update && apt-get upgrade -y

# Install OpenSSH Server
RUN apt-get install -y openssh-server

# Set up configuration for SSH
RUN mkdir /var/run/sshd
RUN echo 'root:secret_password' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise, user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# Expose the SSH port
EXPOSE 22

# Run SSH
CMD ["/usr/sbin/sshd", "-D"]

Dockerfile

Replace “secret_password” with the password you want to use for SSH. Save this Dockerfile.

Step 3: Build the Docker Image

Once your Dockerfile is ready, you can build your Docker image. Use the following command:

ADVERTISEMENT
<iframe data-google-container-id="b" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="1" id="google_ads_iframe_/22651645586/tecadmin.net_vli79679_4" marginheight="0" marginwidth="0" scrolling="no" src="https://cc80e005e0e0dc232b300c869dcbee48.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html" title="3rd party ad content" width="1"></iframe> <iframe frameborder="no" height="16" marginheight="0" marginwidth="0" scrolling="no" width="16"></iframe>

 

docker build -t ubuntu-ssh . 

This command will build an image from your Dockerfile and tag it as ubuntu-ssh.

Step 4: Run the Docker Container

Now you’re ready to run your Docker container with the following command:

docker run -d -p 2222:22 ubuntu-ssh 

This will run your Docker container in detached mode (-d) and map your machine’s port 2222 to your Docker container’s port 22, which is the default SSH port.

ADVERTISEMENT
<iframe data-google-container-id="2" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="90" id="google_ads_iframe_/22651645586/tecadmin.net_vli79679_0" marginheight="0" marginwidth="0" scrolling="no" src="https://cc80e005e0e0dc232b300c869dcbee48.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html" title="3rd party ad content" width="728"></iframe>

 

Step 5: Connect to the Docker Container via SSH

Finally, you can connect to your Docker container via SSH. Use the following command:

ssh root@localhost -p 2222 

You’ll be prompted to enter the password you set earlier in the Dockerfile. Once you’ve entered it, you’ll be connected to your Ubuntu Docker container via SSH!

Conclusion

Docker and SSH are powerful tools in the realm of devOps and development. By integrating them, you can create a more flexible, more secure, and more powerful development environment. This comprehensive guide walked you through the process of setting up an Ubuntu Docker container with SSH access. With this knowledge, you can now create and access Docker containers with ease and confidence. Always remember to exercise good security practices, like changing default passwords and regularly updating software, to keep your environments safe and secure.

标签:container,Dockerfile,ssh,install,docker,Docker,your,SSH
From: https://www.cnblogs.com/adolfmc/p/17711416.html

相关文章

  • k8s之docker基础+部署
    docker基础+部署1、docker基础概念1、统一标准应用构建程序打包dockerbuild...images应用分享镜像存放仓库dockerhub应用运行统一镜像dockerrun容器化2、资源隔离cpu、内存资源隔离网络隔离用户、用户组隔离...2、架构docker_host安......
  • Docker分享
    前置知识本篇文章主要分享容器技术依赖的Namespace,在开始之前,有一些前置知识需要先阐明,也许它们很零碎,但开始之前我还是希望你能够完全理解这些概念。前置知识这一段除了补短之外,还有一个目的,把一个很多人没解释清楚的问题解释清楚:什么是容器,和虚拟机有什么区别?进程树模型:fo......
  • 讲解和介绍VNC的使用,以及和ssh的对比
    VNC(VirtualNetworkComputing)是一种远程桌面协议,它允许用户通过网络访问和控制远程计算机。通过VNC,用户可以在自己的计算机上查看和操纵远程计算机的桌面界面,就像在本地使用该计算机一样。VNC的使用非常简单。首先,需要在远程计算机上安装VNC服务器软件。然后,用户可以在本地计算机......
  • Docker中如何将容器重新打包成镜像
    最近做项目,建好了不少不同规则的容器。需要以后重复利用,于是重新打包为镜像。使用以下命令,查看容器名称:dockerps-aCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES3e08cb5b24f8gareenbeam/kyv10:v4......
  • 使用TestContainers在Docker中进行集成测试
    现代软件应用很少独立工作。典型的应用程序会与几个外部系统进行通信,如:数据库、消息系统、缓存提供商其他第三方服务。你应该编写测试确保一切正常运行。单元测试有助于隔离地测试业务逻辑,不涉及任何外部服务。它们易于编写并提供几乎即时的反馈。有了单元测试还不够,集......
  • ⛳ Docker 安装、配置和详细使用教程-Win10专业版
    ⛳Docker安装、配置和详细使用教程-Win10专业版......
  • Linux学习之CentOS(三十一)--Linux远程管理之SSH、VNC
    您可以通过点击右下角的按钮来对文章内容作出评价,也可以通过左下方的关注按钮来关注我的博客的最新动态。如果文章内容对您有帮助,不要忘记点击右下角的推荐按钮来支持一下哦如果您对文章内容有任何疑问,可以通过评论或发邮件的方式联系我:[email protected]......
  • Git的ssh方式如何配置,如何通过ssh方式拉取和提交代码
    大家通过git拉取代码的时候,一般都是通过http的方式,简单方便。但是细心的童鞋肯定也注意到Git也是支持ssh方式的。可能很多人也试过使用这个方式,但是好像没有那么简单。那么什么是SSH呢?为啥要使用SSH方式呢?HTTPS和SSH的区别Git支持使用HTTPS和SSH两种方式与远程仓库进行通信和代码......
  • Linux ssh命令详解
      SSH(远程连接工具)连接原理:ssh服务是一个守护进程(demon),系统后台监听客户端的连接,ssh服务端的进程名为sshd,负责实时监听客户端的请求(IP22端口),包括公共秘钥等交换等信息。ssh服务端由2部分组成:openssh(提供ssh服务)   openssl(提供加密的程序)ssh的客户端可以用XS......
  • Docker-compose容器编排
    Docker容器编排的作用是管理和协调多个Docker容器的部署、启动、停止和扩展等操作。它可以确保应用程序在分布式环境中以可靠、高效和可伸缩的方式运行。1)安装步骤参考官网文档,选择对应的系统版本即可,官网提供两种安装方式请根据需要自行选择。https://docs.docker.com/desktop/in......