华子目录
- docker实例
- 1.为Ubuntu镜像添加ssh服务
- 2.Docker安装mysql
docker实例
1.为Ubuntu镜像添加ssh服务
(1)访问https://hub.docker.com,寻找合适的Ubuntu镜像
(2)拉取Ubuntu镜像
[root@server ~]# docker pull ubuntu:latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
(3)后台运行容器,并配置软件源
- Ubuntu的软件源必须以
.list
结尾,并且软件源放在/etc/apt/
下
[root@server ~]# docker run -itd --name ubuntu -p 2222:22 ubuntu:latest
871fe7e0e9a3c0d3f5d079911483cb890323d2dfd3c13d23f685aa2dd4c75944
[root@server ~]# docker exec -it ubuntu bash
root@871fe7e0e9a3:/# mv /etc/apt/sources.list /etc/apt/sources.list.backup #将原来的软件源置为备份root@871fe7e0e9a3:~# echo deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse >> /etc/apt/sources.list
root@871fe7e0e9a3:~# echo deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse >> /etc/apt/sources.list
root@871fe7e0e9a3:~# echo deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse >> /etc/apt/sources.list
root@871fe7e0e9a3:~# echo deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse >> /etc/apt/sources.list
root@871fe7e0e9a3:~# echo deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse >> /etc/apt/sources.list
root@871fe7e0e9a3:~# echo deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse >> /etc/apt/sources.list
root@871fe7e0e9a3:~# echo deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse >> /etc/apt/sources.list
root@871fe7e0e9a3:~# echo deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse >> /etc/apt/sources.listroot@871fe7e0e9a3:~# cd /etc/apt
root@871fe7e0e9a3:/etc/apt# ls
apt.conf.d auth.conf.d preferences.d sources.list sources.list.backup sources.list.d trusted.gpg.droot@871fe7e0e9a3:/etc/apt# apt update
Ign:1 https://mirrors.aliyun.com/ubuntu jammy InRelease
Ign:2 https://mirrors.aliyun.com/ubuntu jammy-security InRelease
Ign:3 https://mirrors.aliyun.com/ubuntu jammy-updates InRelease
Ign:4 https://mirrors.aliyun.com/ubuntu jammy-backports InRelease
Err:5 https://mirrors.aliyun.com/ubuntu jammy ReleaseCertificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 182.40.60.234 443]
Err:6 https://mirrors.aliyun.com/ubuntu jammy-security ReleaseCertificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 182.40.60.234 443]
Err:7 https://mirrors.aliyun.com/ubuntu jammy-updates ReleaseCertificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 182.40.60.234 443]
Err:8 https://mirrors.aliyun.com/ubuntu jammy-backports ReleaseCertificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 182.40.60.234 443]
Reading package lists... Done
W: https://mirrors.aliyun.com/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu/dists/jammy-security/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu/dists/jammy-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu/dists/jammy-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.aliyun.com/ubuntu/dists/jammy/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.aliyun.com/ubuntu jammy Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://mirrors.aliyun.com/ubuntu/dists/jammy-security/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.aliyun.com/ubuntu jammy-security Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://mirrors.aliyun.com/ubuntu/dists/jammy-updates/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.aliyun.com/ubuntu jammy-updates Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://mirrors.aliyun.com/ubuntu/dists/jammy-backports/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.aliyun.com/ubuntu jammy-backports Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
(4)安装和配置ssh服务
root@871fe7e0e9a3:~# apt install openssh-server -y
(5)如果需要正常启动ssh服务,则目录/var/run/sshd
必须存在
root@871fe7e0e9a3:~# mkdir -p /var/run/sshd
(6)启动ssh服务,并查看监听状态
root@871fe7e0e9a3:~# /usr/sbin/sshd -D &
root@871fe7e0e9a3:~# apt install iproute
root@871fe7e0e9a3:~# ss -lntup
(7)使用ssh连接容器
[root@node1 ~]# ssh root@192.168.80.129 -p 2222
2.Docker安装mysql
(1)访问https://hub.docker.com,寻找合适的mysql镜像
(2)拉取mysql镜像
[root@server ~]# docker pull mysql:latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 2 years ago 516MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
(3)后台运行容器,并使用exec进入容器
mariadb
和MySQL
指定-e
变量时都是MYSQL_ROOT_PASSWORD
[root@server ~]# docker run -itd --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
ea6beb680b7ae725d844bd3360e184e772bbbadc9df1e84f10a90b60a9625c52
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTSNAMES
ea6beb680b7a mysql:latest "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
871fe7e0e9a3 ubuntu:latest "bash" 44 minutes ago Up 44 minutesubuntu
[root@server ~]# docker exec -it mysql bash
root@ea6beb680b7a:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPLCopyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>