基本思路:先下载nginx镜像,然后创建nginx容器,再进入容器对配置进行设置

docker的基本命令前面帖子中有整理,可以参考查看

docker的常用命令(镜像、容器常用操作)_咛果果的博客-CSDN博客每次学习docker,都会因为docker的命令太多苦恼,经常记不住,有时候去官网查一下,有时候在网上找一些帖子,特别麻烦,以下是对一些常用的命令的用法做了一些归纳和说明,当然,以后如果使用的多了,肯定就都记住了,这个帖子也就没什么意义了;不过它还会适合初学docker现阶段的我这个阶段的你。一、帮助命令docker version #显示docker的版本信息docker info #查看docker的系统信息,包括镜像和容器...https://blog.csdn.net/rengn/article/details/123515069

1、查看是否有nginx镜像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   6 months ago   231MB

2、如果docker中无nginx镜像,下载镜像

我们这儿直接下载最新版本

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

3、启动创建nginx容器

启动命令:docker run -d --name ce-nginx155 -p 3344:80 nginx

# -d 后台运行

# --name 容器命名

# -p 宿主机端口:容器内部端口(文章末尾 有关于端口映射的详细讲解)

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    605c77e624dd   2 months ago   141MB
centos       latest    5d0da3dc9764   6 months ago   231MB
[root@localhost ~]# docker run -d --name ce-nginx155 -p 3344:80 nginx
971dd8810da2b1099aba5545c110878d4000289b5262d3df4f04c6b114743ae9
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
971dd8810da2   nginx     "/docker-entrypoint.…"   14 seconds ago   Up 13 seconds   0.0.0.0:3344->80/tcp, :::3344->80/tcp   ce-nginx155
[root@localhost ~]# curl 127.0.0.1:3344

 访问验证:使用外部端口3344可以访问该容器nginx

 

4、进入容器,可以对相关配置进行设置

[root@localhost ~]# docker exec -it ce-nginx155 /bin/bash
root@971dd8810da2:/# ll
bash: ll: command not found
root@971dd8810da2:/# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@971dd8810da2:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@971dd8810da2:/# cd /etc/nginx/

root@971dd8810da2:/etc/nginx# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

 ps:端口映射暴露原理

容器nginx01和nginx02分别是独立的容器,也是绝对隔离的,容器内的端口使用是互不影响的,但是所开放的端口如果需要被外部访问,需要配置对外的映射规则

docker run -d --name nginx01 -p 3344:80 nginx

docker run -d --name nginx02 -p 3355:80 nginx

如图所示,访问服务器宿主机防火墙的端口3344和3355会分别映射到nginx01和nginx02两个不同的容器中。

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐