Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH

(2020/09/25) The troubleshooting part has been separated into the following articles. Cannot connect to VM with SSH connectable Docker container

Overview

I've been a little interested in Docker lately, but I haven't had a chance to come into contact with Docker at work so far, so I decided to study for myself. Google teachers and ancestors will introduce a lot of articles to build around CentOS 7, but since CentOS 8 has been released for a while (quite?), It's a new one, so it's a trial and error process. Let's play a little with the knowledge. [^ 1] This time, the goal is to create a container that can be connected by SSH, and to enable SSH access from the host to the container.

[^ 1]: CentOS 8 seems to come with a Docker-compatible system called Podman as standard equipment, so you may not need to use Docker.

environment

--Windows10 Home (1909) (host)

procedure

VM boot

It may be easy to install by default, but for the time being, I decided to try how far it can be built with Minimal (minimum configuration). CentOS 8 uses a full-size image, and during installation there will be a place to select the configuration, so select the minimum configuration there. The construction procedure in VirtualBox is omitted. I referred to the following articles.

Reference: Build CentOS8 in Virtualbox environment Reference: CentOS8 at first glance

SSH connection related settings

After installing the OS, bring it from TeraTerm on the host side to a place where SSH connection is possible.

First, set up port forwarding on the VirtualBox side. It would be nice if SSH access was possible, but with the image below, I would like to be able to access the guest VM by specifying port 2222 and the container by specifying port 22222 when connecting.

Host --- Guest --- Container 2222 ----- 22 22222 --- 2222 --- 22

Therefore, in VirtualBox, set the association between 2222 and 22 and 22222 and 2222. The association between guest VM No. 2222 and container No. 22 will be performed later in the Docker-related settings. キャプチャ.PNG

Move to the settings on the guest VM side. First, set the firewall to allow SSH connections.

# firewall-cmd --add-service=ssh --permanent
# firewall-cmd --reload

Enable network interface.

# nmcli connection up enp0s3
# nmcli connection modify enp0s3 connection.autoconnect yes
# nmcli connection show enp0s3

Disable SELinux and restart.

# vi /etc/selinux/config
SELINUX=disabled
# reboot

Now you can SSH from the host side TeraTerm to the guest VM. When connecting, specify the IP address for the host name (192.168.11.4 in our environment), and specify the port as set for port forwarding in Virtual Box (2222 this time).

Introducing Docker

~~ This time, I will try it on the assumption that it will be built in an offline environment. ~~ </ font> I was thinking of building it in an offline environment until the middle of the process, but I ended up working in an online environment because external communication was absolutely necessary when starting the Docker container in the latter stage. Therefore, please forgive me though I am taking unnecessary steps for offline to the middle. (Maybe all the steps are rewritten online)

In addition, the following article was helpful for the offline introduction procedure.

Reference: Docker environment construction-think about how to use it in an offline environment-

First, download the necessary materials to the host side.

Docker 19.03.8 https://download.docker.com/linux/static/stable/x86_64/docker-19.03.8.tgz Docker Compose 1.25.4 https://github.com/docker/compose/releases/download/1.25.4/docker-compose-Linux-x86_64

Send materials to the guest VM with WinSCP etc. and deploy with the following command.

$ tar zxvf docker-19.03.8.tgz
-bash: tar:Command not found

…… I see, there is no tar command in the minimum configuration installation. I introduced the command and tried again, wondering why the tar was not included.

Reference: List of ridiculous things after installing CentOS8 (Minimum)

$ sudo yum install tar
$ tar zxvf docker-19.03.8.tgz

Since the download material itself is an executable file for Docker Compose, you can rename it, put it in a predetermined position, and make it executable.

$ sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

From now on, it is troublesome to add sudo and type the docker command, so grant authority.

$ sudo usermod -aG docker $USER

Container startup

Create Dockerfile and docker-compose.yml in the guest VM's home directory to run ssh in the container. Alpine Linux, a lightweight OS, is used for the image file of the container. In addition, I referred to the following article for how to make ssh resident in the container.

Reference: SSH without systemd in Docker container

Dockerfile


FROM alpine

#Introduced ssh and others
RUN set -x && apk update && apk upgrade \
  && apk add --no-cache openssh openrc \
  && apk add --no-cache bash \
  && apk add --update busybox-suid
RUN rc-update add sshd && rc-status
RUN mkdir -p /run/openrc/ && touch /run/openrc/softlevel

#sshd settings
RUN sed -i 's/^AllowTcpForwarding no/AllowTcpForwarding yes/g' /etc/ssh/sshd_config
RUN sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config

#Created by general user
RUN adduser -D test

#Password setting
RUN echo 'root:root' | chpasswd
RUN echo "test:test" | chpasswd

#Hide banner when logging in
RUN echo "" > /etc/motd

#Start sshd and leave it as it is
CMD /etc/init.d/sshd start && tail -f /dev/null

docker-compose.yml


version: '3.8'

services:
  test:
    build: .
    container_name: test
    hostname: test
    ports:
      - "2222:22" #Setting to associate guest VM with container port
    tty: true

After creating the above file, finally start the container.

[centos@dockertest ~]$ docker-compose build
[centos@dockertest ~]$ docker-compose up -d

When the container starts up, try accessing the container with SSH from the host.

test:~$

With the above, I thought that I was able to create a Docker container with SSH access.

(Continued to Problem Occurrence)

Summary

While groping, I was able to run a Docker container that can be connected to SSH for the time being. Even so, I was surprised that Minimal's CentOS 8 doesn't include tar. How was it decided that it was unnecessary?

Recommended Posts

Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
I can no longer connect to a VM with a Docker container that can be connected via SSH
Create a private key / public key in CentOS8.2 and connect to SSH with VS Code
How to quickly create a reverse proxy that supports HTTPS with Docker
[Memo] Create a CentOS 8 environment easily with Docker
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
Let's create a gcloud development environment on a centos8 container
Create a Docker container to convert EPS to PGF source
[Docker] How to see the contents of Volumes. Start a container with root privileges.
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC
Ssh connect from container to git with VSCode Remote Container
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
Create a page control that can be used with RecyclerView
Create a Docker image with the Oracle JDK installed (yum
Wait for the container service to start with docker healthcheck
[Docker] Delete only the volume associated with a specific container
I tried to create a padrino development environment with Docker
One file of Docker x Laravel threat! Build a local development environment with the minimum configuration
How to build an environment with Docker, which is the minimum required to start a Rails application
[Swift5] How to create a .gitignore file and the code that should be written by default
Mechanism for converting to a language that the browser can recognize
A little happy that Nginx's Docker container defaults to graceful shutdown
Create a Vue3 environment with Docker!
The story of making it possible to build a project that was built by Maven with Ant
Command line that can create a directory structure for building a Laravel environment with Docker in one shot
Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image
How to interact with a server that does not crash the app
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
[Rails] "pry-rails" that can be used when saving with the create method
Create a program to post to Slack with GO and make it a container
Create a simple gateway server by setting masquerade with firewall-cmd of CentOS8
Minimum configuration sample to automatically release Lambda by Java with Code pipeline
How to start a Docker container with a volume mounted in a batch file
Connect to Amazon EC2 with SSH (Ubuntu)
Create a MySQL environment with Docker from 0-> 1
Connect with VS Code from a Windows client to Docker on another server
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
[Android] I want to create a ViewPager that can be used for tutorials
Let's make a calculator application with Java ~ Create a display area in the window
How to display the amount of disk used by Docker container for each container
Up to the point of launching a Docker container built using RedHat Quarkus
I was addicted to not being able to connect to AWS-S3 from the Docker container
Build a development environment to create Ruby on Jets + React apps with Docker
[Part 1] Creating a Docker container that delivers Markdown in HTML with Apache / Pandoc
Point memo that was moss when building a docker container created by a colleague
Try to save the data that can be read by JavaFX as PNG
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment