docker创建容器设置系统环境变量_Linux -- Docker创建容器

本文介绍了如何在Docker中创建容器并设置系统环境变量,包括如何以守护进程方式运行容器、指定容器名称、端口映射(固定与随机)以及挂载存储卷。同时,讲解了`-e`参数用于设置环境变量,例如设置NGINX_VERSION和MYSQL_ROOT_PASSWORD。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1ac00fcd7d2e7d2c11314a1e85a1d30c.png

容器

docker中对外提供服务的实例

创建容器 -- docker run [参数] [镜像名称] [命令]

特点

  • ①:检查本地是否有所需镜像(如果没有,立即去相应的镜像仓库下载)
  • ②:根据参数启动

容器启动参数

-d 以守护进程方式运行 
-p 指定映射端口 
-P 随机映射端口 
-i 保持标准输入打开 
-t 分配一个伪终端 
-v 设置挂载文件到主机上 
--rm 当容器关闭时自动删除 
--name 为启动的容器设置一个名字 
--network 指定使用哪个网络 
-e 设置容器中的环境变量 
--link 链接到另一个容器 
-h 指定容器内的主机名

创建第一个容器

  • [root@localhost ~]# docker run nginx:1.19.5
    • [root@localhost ~]# docker exec relaxed_nobel curl 127.0.0.1
    • relaxed_nobel -- 是刚生成的容易的docker ps 对应的NAMES字段

1644b7e7cda9b01954e9e52340f375d5.png

参数

  • -d : 以守护进程的方式运行一个容器
    • [root@localhost ~]# docker run -d nginx:1.19.5
    • 不占用命令行窗口
[root@localhost network-scripts]# docker run -d nginx:1.19.5
02c8a56e109f00749d52127e3af3bf536d16e029572cd47beb1208a4b7d3233a
[root@localhost network-scripts]# 
  • --name : 指定一个容器的名称(将名称加入到Docker DNS当中)
    • [root@localhost ~]# docker run -d --name nginx_bak nginx:1.19.5

7a3764a2ba6d98e0fda8b7c572a0ccfa.png
  • -p(小写) : 指定固定端口映射[宿主主机端口号:容器内部端口号]
    • [root@localhost ~]# docker run -d --name nginx_p -p 8090:80 nginx:1.19.5
    • 容器的8090端口映射成80端口

80705d6cd2ae7cc5a5559e75eb2c95ab.png
  • [root@localhost ~]# docker run -d -p 8099:80 nginx:1.19.5
    • 指定端口 , 修改hots文件映射本地
[root@localhost ~]# docker run -d -p 8099:80 nginx:1.19.5
b10238736242523b74a618a9d917216a7dcb84351cad1b38e7f56b3a6f35023c
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
b10238736242        nginx:1.19.5        "/docker-entrypoin..."   17 seconds ago      Up 16 seconds       0.0.0.0:8099->80/tcp   stupefied_aryabhata

fb9339895d9bb17061896a943a15dad7.png
  • -P(大写) : 指定随机端口映射
    • [root@localhost ~]# docker run -d -P nginx:1.19.5
[root@localhost ~]# docker run -d -P nginx:1.19.5
ca025dec1a527601bfa1c3bc0263de68a54e37cb5f0b0960dd8779241ec9f379
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
ca025dec1a52        nginx:1.19.5        "/docker-entrypoin..."   3 seconds ago       Up 3 seconds        0.0.0.0:32768->80/tcp   confident_kalam
[root@localhost ~]# 

3e27a2542fb712526381953cf64ae5f2.png
  • -v : 挂载存储卷
    • docker run -d -v [宿主主机的目录]:[容器内的目录] [镜像名称] [启动命令]
    • [root@localhost yum.repos.d]# docker run -d -v /root/django/:/usr/share/nginx/html nginx:1.19.5
[root@localhost yum.repos.d]# docker run -d -v /root/django/:/usr/share/nginx/html nginx:1.19.5
195b13d6edb2d66330af640166b5c13c20c2e8e8b8393b263142d2df9e973a78
[root@localhost yum.repos.d]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
195b13d6edb2        nginx:1.19.5        "/docker-entrypoin..."   14 seconds ago      Up 14 seconds       80/tcp              optimistic_brown
  • --rm : 当一个容器的生命周期结束时,立即删除该容器
    • docker run -d --rm [镜像名称或ID]
[root@localhost django]# docker run -d --rm -P redis    -- 创建容器使用  --rm
03295d361dcc3664c69f2506a913b1231f0c1b7dd1fd2c676b196f6d99b4e467
[root@localhost django]# docker ps     -- 查看运行中的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
03295d361dcc        redis               "docker-entrypoint..."   6 seconds ago       Up 5 seconds        0.0.0.0:32770->6379/tcp   elated_williams
99babe638e0f        nginx:1.19.5        "/docker-entrypoin..."   5 minutes ago       Up 5 minutes        0.0.0.0:32769->80/tcp     quizzical_minsky
[root@localhost django]# docker stop elated_williams   -- 停止容器
elated_williams
[root@localhost django]# docker ps -a   -- 查看全部容器 , 没有03295d361dcc , 已经被删除了
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                   NAMES
99babe638e0f        nginx:1.19.5        "/docker-entrypoin..."   6 minutes ago       Up 6 minutes                0.0.0.0:32769->80/tcp   quizzical_minsky
073fdd3e309d        nginx:1.19.5        "/docker-entrypoin..."   22 minutes ago      Exited (0) 6 minutes ago                            sharp_wescoff
195b13d6edb2        nginx:1.19.5        "/docker-entrypoin..."   30 minutes ago      Exited (0) 19 minutes ago                           optimistic_brown
[root@localhost django]# 
  • -e : 在容器内设置一个环境变量
    • docker run -d -e NGINX_VERSION=1.19.5000 nginx:1.19.5
    • docker run -d -P -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
[root@localhost /]# docker run -d -e NGINX_VERSION=1.19.5000 nginx:1.19.5
WARNING: IPv4 forwarding is disabled. Networking will not work.
4d117f11d1449c10ae3a92f57d8cc1366a4b7987ba2dc64a6831dec6cb6dbe07
[root@localhost /]# docker run -d -P -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
0bc13d638463382a2c8c727c6ac9664add96b5970b3b6a6d3f8ced55f325e82b
[root@localhost /]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                               NAMES
0bc13d638463        mysql:latest        "docker-entrypoint..."   6 seconds ago       Up 5 seconds        0.0.0.0:32776->3306/tcp, 0.0.0.0:32775->33060/tcp   wizardly_mestorf
4d117f11d144        nginx:1.19.5        "/docker-entrypoin..."   14 seconds ago      Up 14 seconds       80/tcp                                              gracious_jennings
[root@localhost /]# docker exec -it 0bc13d638463 bash
root@0bc13d638463:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.05 sec)

mysql> 
  • -i : 打开标准输出
  • -t : 创建一个伪终端 -- 一般连用
    • docker run -it [镜像]bash
[root@localhost /]# docker run -it nginx:1.19.5 bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
root@4ed338a2fd4b:/# nginx -V
nginx version: nginx/1.19.5
built by gcc 8.3.0 (Debian 8.3.0-6) 
built with OpenSSL 1.1.1d  10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.5/debian/debuild-base/nginx-1.19.5=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
root@4ed338a2fd4b:/# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值