Docker实例

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.list


root@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.d


root@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 Release
  Certificate 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 Release
  Certificate 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 Release
  Certificate 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 Release
  Certificate 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进入容器

  • mariadbMySQL指定-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          PORTS
                         NAMES
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 minutes
                         ubuntu
[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 - GPL

Copyright (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>

相关推荐

  1. Docker实战

    2024-03-31 14:16:04       50 阅读
  2. 5-Docker实例-nginx application

    2024-03-31 14:16:04       52 阅读
  3. 5-Docker实例-安装php

    2024-03-31 14:16:04       64 阅读
  4. 5-Docker实例-安装nginx

    2024-03-31 14:16:04       61 阅读
  5. 5-Docker实例-安装tomcat

    2024-03-31 14:16:04       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 14:16:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 14:16:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 14:16:04       87 阅读
  4. Python语言-面向对象

    2024-03-31 14:16:04       96 阅读

热门阅读

  1. PyTorch-----torch.flatten()函数

    2024-03-31 14:16:04       35 阅读
  2. js关于字符串的方法

    2024-03-31 14:16:04       41 阅读
  3. 2024年保安员职业资格考试真题题库

    2024-03-31 14:16:04       39 阅读
  4. 5.FileChannel

    2024-03-31 14:16:04       28 阅读
  5. ospf配置静态路由实验

    2024-03-31 14:16:04       40 阅读
  6. 数据结构--合并区间

    2024-03-31 14:16:04       37 阅读