2024年最新docker入门教程,docker compose教程,docker搭建lnmp环境,docker搭建java开发环境

这是一篇2024年的Docker详细教程,涵盖从安装Docker,配置镜像源,到使用Docker搭建LNMP(Nginx、MySQL、PHP)、Java开发环境。内容包括容器命令、数据卷管理、镜像发布到阿里云和私有仓库,以及使用docker-compose进行服务编排。
摘要由CSDN通过智能技术生成

2024年最新整理docker入门教程,docker compose教程,docker搭建lnmp环境,docker搭建java开发环境。只需记住docker、image、container三个单词,再知道怎么使用,docker就够了。

docker入门教程

1. 前序

1.1 终端安装jdk软件

# 第1步 切换到家目录
cd

# 第2步 下载jdk
wget -c https://repo.huaweicloud.com/java/jdk/8u151-b12/jdk-8u151-linux-x64.tar.gz

# 第3步 新建/opt/java目录
mkdir /opt/java

# 第4步 解压jdk至/opt/java目录下
tar -xvf jdk-8u151-linux-x64.tar.gz -C /opt/java

# 第5步 设置软链接 PATH
ln -s /opt/java/jdk1.8.0_151/bin/java /usr/sbin/java

# 第6步 java命令是否可用
java -version

# 第7步 删除下载软件压缩包
rm -f jdk-8u151-linux-x64.tar.gz

1.2 shell脚本安装jdk软件

#!/usr/bin/env bash

# 遇到错误会直接退出,不会继续往下执行脚本
set -e

# 定义变量
# 定义软件安装目录
soft_dir="/opt/java"
soft_file="jdk-8u151-linux-x64.tar.gz"
config_file="/etc/profile.d/java.sh"

# cd到家目录
cd

# 下载软件
wget -c https://repo.huaweicloud.com/java/jdk/8u151-b12/${soft_file}

# 新建/opt/java目录
if [ -e ${soft_dir} ];then
	rm -rf ${soft_dir}
	mkdir ${soft_dir}
else
	mkdir ${soft_dir}
fi

# 指定目录进行解压
tar -xvf ${soft_file} -C ${soft_dir}

# 新建/etc/profile.d/java.sh
if [ -e ${config_file} ];then
	rm -f ${config_file}
	touch ${config_file}
else
	touch ${config_file}
fi

JAVA_HOME=${soft_dir}'/jdk1.8.0_151'
CLASSPATH='.:'${JAVA_HOME}'/lib'
PATH=${JAVA_HOME}'/bin:'${
   PATH}

echo "JAVA_HOME=${JAVA_HOME}" >> ${config_file}
echo "CLASSPATH=${CLASSPATH}" >> ${config_file}
echo "PATH=${
    PATH}" >> ${config_file}
echo "export JAVA_HOME CLASSPATH PATH" >> ${config_file}

# 刷新环境变量文件
source ${config_file}

# 检测java是否安装好了
java -version

if [ $? == 0 ];then
	echo "jdk安装成功"
fi

rm -f ${soft_file}

exit 0

在终端运行安装jdk脚本(假如这个脚本名称叫install_jdk.sh)

./install_jdk.sh

# 如果用上面方式会报错,那么就用这种方式执行脚本
source install_jdk.sh

小结:
1、小公司运维,采用上面两种方式安装项目环境,工作量也大不到哪里;如果大公司运维,需要安装环境特别多,需要安装的软件很多,累死;
2、同样的命令,同样的CentOS的系统,可能会出现不同的问题,还是需要Docker容器技术。

2. 初识Docker

在这里插入图片描述

2.1 Docker介绍

docker,名词,翻译成中文:码头工人

docker是一个软件

docker是一个运行于Linux系统上的软件,用于创建、管理和编排容器

在这里插入图片描述

docker容器与传统虚拟机比较

特性 容器 虚拟机
启动 秒级 分钟级
硬盘使用 一般为MB 一般为GB
性能 接近原生
单机支持量 上千容器 一般几十个

使用docker前后比较

在这里插入图片描述

在这里插入图片描述

2.2 Docker安装

docker安装前提

Docker使用了Linux内核的容器特性,依赖于Linux。在Windows和macOS 系统上,Docker得通过虚拟Linux内核的方式来完成任务。

Linux内核版本为3.8以上的64位系统

[root@hecs-141089 docker]# uname -r
3.10.0-1160.92.1.el7.x86_64

CentOs安装docker

卸载旧版本

https://docs.docker.com/engine/install/centos/

yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine

安装需要的软件

安装yum-utils包(提供yum-config-manager 实用程序)并设置稳定的存储库
# 官网要求
yum install -y yum-utils
# 推荐使用使用阿里的docker镜像仓库,国外的镜像仓库是比较慢的
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新yum软件包索引

# 更新yum软件包索引
yum makecache fast

安装docker-ce

yum -y install docker-ce docker-ce-cli containerd.io

卸载

systemctl stop docker 
yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

2.3 配置镜像源

在这里插入图片描述

镜像源可以理解为一台存放了很多镜像软件的服务器,可以通过URL进行访问下载。

国内常见镜像源

# Docker中国区官方镜像
https://registry.docker-cn.com

# 网易
http://hub-mirror.c.163.com

# ustc(中国科学技术大学)
https://docker.mirrors.ustc.edu.cn

配置镜像源流程

# 1、在/etc/docker目录中添加daemon.json文件,内容如下:
{
   
	"registry-mirrors": ["http://hub-mirror.c.163.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn"]
}

# 2、重启docker服务
systemctl daemon-reload
systemctl restart docker

# 3、查看是否配置成功
docker info

3 Docker服务相关命令

# Docker服务启动命令
systemctl start docker

# Docker服务停止命令
systemctl stop docker

# Docker服务状态查看命令
systemctl status docker

# 设置Docker服务开机自启
systemctl enable docker

docker帮助信息

[root@hecs-141089 ~]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.11.2)
  compose*    Docker Compose (Docker Inc., v2.21.0)
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Global Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default
                           context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket to connect to
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Run 'docker COMMAND --help' for more information on a command.

For more help on how to use Docker, head to https://docs.docker.com/go/guides/

4. Docker镜像相关命令

镜像帮助信息

[root@hecs-141089 ~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Download an image from a registry
  push        Upload an image to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

4.1 查看镜像

语法:

# 查看本地所有镜像
docker images
# 查看本地所有镜像id
docker images -q

示例:

[root@hecs-141089 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
debian       latest    0ce03c8a15ec   2 weeks ago   117MB

各个选项说明:

  • REPOSITORY:表示镜像的仓库源

  • TAG:镜像的标签版本号

  • IMAGE ID:镜像ID

  • CREATED:镜像创建时间

  • SIZE:镜像大小

同一个仓库源可以有多个TAG版本,代表这个仓库源的拥有镜像多个版本,可以使用REPOSITORY:TAG指定唯一镜像。如果不指定TAG版本,则默认是最新版本(latest)

4.2 查找镜像

语法:

# 从网络中查找镜像源
docker search 镜像名称

示例:

# 搜索官方镜像
[root@hecs-141089 ~]# docker search ubuntu --filter is-official=true
NAME                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu               Ubuntu is a Debian-based Linux operating sys…   16653     [OK]       
websphere-liberty    WebSphere Liberty multi-architecture images …   297       [OK]       
open-liberty         Open Liberty multi-architecture images based…   62        [OK]       
neurodebian          NeuroDebian provides neuroscience research s…   105       [OK]       
ubuntu-debootstrap   DEPRECATED; use "ubuntu" instead                52        [OK]       
ubuntu-upstart       DEPRECATED, as is Upstart (find other proces…   115       [OK]

# 搜索收藏大于等于60的镜像
[root@hecs-141089 ~]# docker search ubuntu --filter stars=60
NAME                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu              Ubuntu is a Debian-based Linux operating sys…   16653     [OK]       
websphere-liberty   WebSphere Liberty multi-architecture images …   297       [OK]       
open-liberty        Open Liberty multi-architecture images based…   62        [OK]       
neurodebian         NeuroDebian provides neuroscience research s…   105       [OK]       
ubuntu-upstart      DEPRECATED, as is Upstart (find other proces…   115       [OK]       
ubuntu/nginx        Nginx, a high-performance reverse proxy & we…   103                  
ubuntu/squid        Squid is a caching proxy for the Web. Long-t…   73                   
ubuntu/apache2      Apache, a secure & extensible open-source HT…   67                   
ubuntu/bind9        BIND 9 is a very flexible, full-featured DNS…   65

# 搜索官方镜像并且收藏数大于等于60
[root@hecs-141089 ~]# docker search ubuntu --filter stars=60 --filter is-official=true
NAME                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu              Ubuntu is a Debian-based Linux operating sys…   16653     [OK]       
websphere-liberty   WebSphere Liberty multi-architecture images …   297       [OK]       
open-liberty        Open Liberty multi-architecture images based…   62        [OK]       
neurodebian         NeuroDebian provides neuroscience research s…   105       [OK]       
ubuntu-upstart      DEPRECATED, as is Upstart (find other proces…   115       [OK]

4.3 拉取镜像

语法:

# 从Docker仓库拉取(下载)镜像到本地
# 镜像名称格式(名称:版本号),如果不指定版本号则是最新的版本,如果需要指定版本号,则可以去docker hub仓库搜索查看
docker pull 镜像名称

示例:

[root@hecs-141089 ~]# docker image pull --help

Usage:  docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

Download an image from a registry

Aliases:
  docker image pull, docker pull

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output
[root@hecs-141089 ~]# docker pull -a redis
# 下载过程省略....
[root@hecs-141089 ~]# docker images | grep redis
redis        2-32bit        19865a7ae96c   7 years ago   203MB
redis        2.8-32bit      19865a7ae96c   7 years ago   203MB
redis        2              481995377a04   7 years ago   186MB
redis        2.8            481995377a04   7 years ago   186MB
redis        2.6-32bit      62b0a5c3ea45   7 years ago   158MB
redis        2.6.17-32bit   62b0a5c3ea45   7 years ago   158MB
redis        2.6            a081f7d44c38   7 years ago   150MB
redis        2.6.17         a081f7d44c38   7 years ago   150MB
redis        2.8.19         dd9fe7db5236   8 years ago   111MB
redis        2.8.18         5f9a9a936de2   8 years ago   111MB
redis        
  • 22
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员buddha2080

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值