(2020/09/25) The troubleshooting part has been separated into the following articles. Cannot connect to VM with SSH connectable Docker container
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.
--Windows10 Home (1909) (host)
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
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.
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).
~~ 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
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)
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