Docker Basics - 2
This article was last updated on: July 24, 2024 am
Container OS type
Busybox
A software toolbox that integrates more than a hundred of the most commonly used Linux commands and tools.
containcat
echo
grep
find
mount
telnet
wait
Busybox is the Swiss Army knife for Linux
Debian/Ubuntu
CentOS/Fedora
CoreOS
Linux distribution, for container technology.
Create an image of the custom operating system
Created based on the commit command
Users can submit their own modifications to the container and generate new images. The command format is:
docker commit CONTAINER [REPOSITORY[:TAG]]
Create a step
-
Use the OS image to create a container
-
Configure the software source to a domestic software source, such as ALI
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
-
execute
apt-get update
Update the package cache -
Pass
apt-get
Install services (e.g. ssh):apt-get install openssh-server
-
Create a directory: A directory is required
/var/run/sshd
exists, created manually:mkdir -p /var/run/sshd
. At this point, you are ready to start the service:/usr/sbin/sshd -D &
-
modify the service configuration, Remove PAM login restrictions:
sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd
-
Other actions: Created in the root directory
.ssh
directory, and copy the public key information that needs to be logged in (1. directly from the user directory .ssh/id_rsa.pub Copy of files 2.ssh-keygen -t rsa
generate) toauthorized_keys
Middle:mkdir -p /root/.ssh && vi /root/.ssh/authorized_keys
. An example of a public key information format is as follows:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCtuqN2zGhhVBTVCCoNa8hPvGu3xo8+UsqG+AxW0jEUvQYhr6/IEXiIAk41HzjeEZVYKGGr08Jh8n5xxmBW4AyH/1DaU1Ej3m0dOuZ09HAUJfY7WnrtO8GrZtQT2KhI6P2pwnOJU3fm6eRLLVzL2oSyhBQ8ca/njwAyHXOVJiPOpO3cokOPa2BzziWqslmFKyWQdaf6rBwYKF+2eoFrVk0QepoJtc6OfgIyuQEi+gJXste6QiPJRYgFQoYlv/bzYnnrG7Zs0qVCi6SfIRF7twVXUNW/hkPbGxsKZTLAvITS3tOR2nRt6pibT46RM/+ebiuT0fZ/e/xl3w4QygGTB2Xl casey@ubuntu
-
Additional steps: Create an executable file for the SSH service that starts automatically
run.sh
, and add the executable permission:vi /run.sh; chmod +x run.sh
1
2#!/bin/bash
/usr/sbin/sshd -D -
Finally, exit the container
exit
Save the image
sudo docker commit <container id> ubuntu-sshd
Start the image
sudo docker run -p 10122:22 -d ubuntu /run.sh
use
You can connect through the SSH service
ssh <container ip> -p 10122 -l root
Created with Dockerfile (focus)
Dockerfile example
1 |
|
Automate time zone modification
Before Ubuntu 16.04:
echo "Asia/Shanghai" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata
Ubuntu 16.04 and later:
&& ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
Prerequisite: tzdata package needs to be installed:
apt-get -yq install tzdata
ENV environment variableENV environment variables take effect globally, and sometimes there may be negative effects.
As:
ENV DEBIAN_FRONTEND noninteractive
All operations are set to non-interactive.Try not to use it as above, the recommended usage is: when necessary, execute the command together, such as:
1
2
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends tzdata # -q: quiet
.dockerignore file
## 忽略文件夹 .git/
.git
## 忽略临时文件
*.swp
run.sh script
1 |
|
Create an image
Execute in the Dockerfile directory:
sudo docker build -t caseycui/ubuntu-sshd .