新银河是诚信为本,市场在变,我们的诚信永远不变...
背景
在上一周笔者对了解,仅限于这样认知:它能替代虚拟机,并且比虚拟机更省资源。
在老师和同事的感染下,感觉不学习好像就不能在IT圈混一样,于是,开始涌入的大潮中。但万事开头难,听了同事的推荐,看了宁皓网的基础的视频之后感觉仅是对基本的命令进行了了解。但是真拿出来用还是不够用的,于是开始搜罗更重资料,学习搭建LNMP环境。
终于功夫不负有心人,在今天终于是实验成功了,特此写下这篇笔记,为后来人提供一个示例。
实战下载nginx官方镜像和php-fpm镜像
docker pull nginx
docker pull bitnami/php-fpm
图片.png
笔者未进行更改源,依然是官方源。
当然,你可以使用中国源。
使用php-fpm镜像开启php-fpm应用容器
docker run -d --name myFpm -p -v /var/www/html:/usr/share/nginx/html bitnami/php-fpm
-d : 该参数为后台运行之意
-v : 指定宿主机与容器的映射关系。/var/www/html为宿主机的项目目录(自定义的),/usr/share/nginx/html为nginx服务器项目默认的路径。
使用nginx镜像开启nginx应用容器
docker run -d --name myNginx -p 8080:80 -v /var/www/html:/usr/share/nginx/html nginx
-p : 该参数设置端口对应的关系。所有访问宿主机8080端口的URL会转发到nginx容器的80端口。
查看对应的IP信息
docker ps -a
图片.png
可以看到,上述在一栏中显示UP,其含义为正在运行。
* 查看IP信息
docker inspect myFpm | grep "IPAddress"
图片.png
修改nginx的相关配置
在容器中是没有vim命令的,所以不能在容器中直接修改配置文件。所以我们必须通过变通的方式去解决这个问题,否则只能在每个容器中安装vim。
docker exec -it myNginx /bin/bash
图片.png
-i : --,交互模式。
-t : --tty,开启一个伪终端。
/bin/bash : 必须写,否则会报错。这是开始伪终端时,进入bash界面,也就是命令行界面。
/etc/nginx/conf.d/default.conf
图片.png
docker cp myNginx:/etc/nginx/conf.d/default.conf ./default.conf
这里用到了上一步查询到的配置文件路径信息
location ~ \.php$ {
fastcgi_pass 172.17.0.2:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
docker cp ./default.conf myNginx:/etc/nginx/conf.d/default.conf
docker exec -it myNginx /bin/bash
service nginx reload
成功了
我看了一下,用到的模块还都有。
图片.png
图片.png
图片.png
docker搭建环境 软件测试|使用docker搞定 Python环境搭建
前言
当我们在公司的电脑上搭建了一套我们需要的环境,比如我们的版本是3.8的,那我可能有一天换了一台电脑之后,我整套环境就需要全部重新搭建,不只是,我们一系列的第三方库都需要重新安装,那么我们有没有解决问题的方法,当然有,我们可以使用解决困扰我们的环境问题。
搜索镜像
: 从 Hub()中搜索指定的镜像,例如我们要搜索一个基于环境安装的.8版本。命令如下:
docker search python
输出如下:
[root@xxxx ~]# docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 4288 [OK]
django Django is a free web application framework, … 847 [OK]
pypy PyPy is a fast, compliant alternative implem… 193 [OK]
kaggle/python Docker image for Python scripts run on Kaggle 123 [OK]
arm32v7/python Python is an interpreted, interactive, objec… 37
centos/python-38-centos7 Platform for building and running Python 3.8… 36
joyzoursky/python-chromedriver Python with Chromedriver, for running automa… 33 [OK]
circleci/python Python is an interpreted, interactive, objec… 29
nikolaik/python-nodejs Python with Node.js 18 [OK]
arm64v8/python Python is an interpreted, interactive, objec… 17
centos/python-36-centos7 Platform for building and running Python 3.6… 17
centos/python-27-centos7 Platform for building and running Python 2.7… 15
iron/python Tiny Python Microcontainer 9
publicisworldwide/python-conda Basic Python environments with Conda. 6 [OK]
dockershelf/python Repository for docker images of Python. Test… 4 [OK]
i386/python Python is an interpreted, interactive, objec… 3
bitnami/python Bitnami Python Docker Image 3 [OK]
komand/python-plugin DEPRECATED: Komand Python SDK 2 [OK]
centos/python-34-centos7 Platform for building and running Python 3.4… 2
muccg/python-base Base images that use python 1 [OK]
amd64/python Python is an interpreted, interactive, objec… 1
ccitest/python CircleCI test images for Python 0 [OK]
saagie/python Repo for python jobs 0
qbtrade/python python 3.6.5 with requirements last update s… 0
openshift/python-33-centos7 DEPRECATED: A Centos7 based Python v3.3 imag… 0
[root@xxxx ~]#
拉取镜像
使用 pull可以拉取我们想要拉取的镜像,命令如下:
docker pull centos/python-38-centos7
输出如下:
[root@xxxx ~]# docker pull centos/python-38-centos7
Using default tag: latest
latest: Pulling from centos/python-38-centos7
8ba884070f61: Pull complete

c3dca185eb14: Pull complete
ee720ba20823: Pull complete
497ef6ea0fac: Pull complete
ebf1fb961f61: Pull complete
b8249f70ce00: Pull complete
ebd817e2efe7: Pull complete
d3d10dd0937c: Pull complete
a8ad47ec8182: Pull complete
Digest: sha256:d10c46b6db436357965a96716e9f5d230d9b1a58c6db1f0c4f43c1fb1994cd79
Status: Downloaded newer image for centos/python-36-centos7:latest
[root@xxxx ~]#
查看镜像
使用 查看本地已经下载好的镜像,命令如下:
docker images
输出如下:
[root@xxxx ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/python-38-centos7 latest b8d15efaa8ec 2 months ago 651MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
运行交互式的容器
会在隔离的容器中运行进程。当运行 run命令时,会启动一个进程,并为这个进程分配其独占的文件系统、网络资源和以此进程为根进程的进程组。
在容器启动时,镜像可能已经定义了要运行的二进制文件、暴露的网络端口等,但是用户可以通过 run命令重新定义
最基本的 run命令的格式如下:
$ sudo docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]
比如我这里要启动,进入交互模式,通过的两个参数 -i -t,让运行的容器实现"对话"的能力
docker run -i -t centos/python-38-centos7 /bin/bash
如下进入终端后,进交互环境打印"hello, i'm .",最后用exit退出,内容如下:
[root@xxxx ~]# docker run -i -t centos/python-36-centos7 /bin/bash
(app-root) python
Python 3.6.3 (default, Mar 20 2018, 13:50:41)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello, i'm Muller.")
hello, i'm Muller.
>>> exit()
(app-root) exit
后台模式启动
run加上-i -t是进交互模式,如果不想进交互模式直接执行脚本,可以直接使用run,比如echo "hello world",屏幕会输出"hello world"
docker run centos/python-38-centos7 /bin/echo "hello world"
如果不想在前台执行,一般我们运行环境会选择挂后台,加个-d 参数即可
docker run -d centos/python-38-centos7 /bin/echo "hello world"
[root@xxxx ~]# docker run centos/python-38-centos7 /bin/echo "hello world"
hello world
[root@xxxx ~]# docker run -d centos/python-38-centos7 /bin/echo "hello world"
1e5c22451bf2215f6c098e066b74363f1db9cde97e9ecea02947ccbbf2fa7e8f
[root@xxxx ~]#
使用-d后台执行后,会发现下面多了一长串,这个就是容器的唯一id,可以通过这个id找到容器
ps查看容器
先run下 /
docker run -d -p 5000:5000 training/webapp python app.py
使用 ps查看正在运行的容器
docker ps
输出如下:
[root@xxxx ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
c9e8a325b145 training/webapp "python app.py" 16 hours ago Up 16 hours 0.0.0.0:32768->5000/tcp
[root@xxxx ~]#
上面的echo "hello world"只是一个很简单的输出指令,执行完就关闭了,所以ps查找正在运行的查不到,可以加个-a参数,显示所有的容器,包括未运行的
ps 查找参数相关语法
logs查看日志
可以通过容器id或者容器name去查运行的日志
docker logs [容器id]
docker logs [容器name]
停止容器
停止容器的话,可以用stop容器的id或者容器NAME名称
docker stop [容器id]
docker stop [容器name]
启动容器
start启动容器
docker start [容器id]
正在运行的容器,可以使用 命令来重启
docker restart [容器id]
删除容器
rm 命令来删除不需要的容器
docker rm [容器id]
docker rm [容器name]
注:当删除运行中的容器时,需要先stop停止容器,再执行删除命令
总结
本文主要介绍了使用搭建环境,以及对于拉取镜像,容器的主要操作,包括运行容器,停止容器,删除容器等。希望对大家学习能有所帮助。