Docker制作镜像、启动容器、查看版本、修改源、安装工具
1、制作镜像所需文件
比如想在容器中瞎捣腾python,那就需要制作一个python的镜像,当然,也可以pull别人的,这里当然选择自己make了
需要在宿主机,就是本机,创建一个目录,用来放制作镜像需要的文件
比如,我在根目录下随便创建了一个docker-file文件夹
[root@compute ~]# pwd
/root
[root@compute ~]# ll docker-file/
total 12
-rw-r--r-- 1 root root 313 Aug 19 16:58 app.py
-rw-r--r-- 1 root root 281 Aug 19 16:57 Dockerfile
-rw-r--r-- 1 root root 6 Aug 19 16:58 requirements.txt
文件夹里有三个文件
先来看Dockerfile:
[root@compute docker-file]# more Dockerfile
# 基于镜像基础
FROM python:3.7# 设置代码文件夹工作目录 /app。这条指令会在运行容器后,在容器内创建一个app的文件夹
WORKDIR /app# 复制当前代码文件到容器中 /app。docker-file中的文件都会复制到/app文件夹下
ADD . /app# 安装所需的包
RUN pip install -r requirements.txt# Run app.py when the container launches
CMD ["python", "app.py"]
这里看一下后面运行容器并进入到容器内后的app目录
root@fc2b44e12e2f:/app# pwd
/app
root@fc2b44e12e2f:/app# ll
total 12
-rw-r--r-- 1 root root 281 Aug 19 08:57 Dockerfile
-rw-r--r-- 1 root root 313 Aug 19 08:58 app.py
-rw-r--r-- 1 root root 6 Aug 19 08:58 requirements.txt
app.py就是随便的一个python文件,hello word就可以
requirements.txt里面是要提前预装的一些工具
2、创建容器
docker build -t docker-test-lzb . #注意后面的点
注意,上面这行指令要在docker-file路径下执行
docker-test-lzb这个就是自己定义的镜像的名字,不指定tag的话,默认是latest
可以查看一下
[root@compute docker-file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-test-lzb latest 375aa7f7acaa 13 hours ago 962MB
有了,说明镜像创建成功
3、启动容器
docker run --name lzb -it docker-test-lzb /bin/bash
--name后面的名字 lzb是自定义的容器的名字,如果不指定话,会随机一个名字,比如下面这样
[root@compute docker-file]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc2b44e12e2f docker-test-lzb "/bin/bash" 10 hours ago Up 5 hours peaceful_jackson
看到没,peaceful_jackson就是随机给的一个名字
-it 是进入交互的参数
-d 后台运行
-p 端口映射
等等吧
现在就是启动了容器,可以看到变化吧
root@fc2b44e12e2f:/app#
4、查看Linux版本
root@fc2b44e12e2f:/app# cat /etc/issue
Debian GNU/Linux 10 \n \l
这里为什么要查看Linux版本呢,原因很复杂,噗哈哈
因为这样启动的容器,你会发现好多命令都没有,比如vim没有,这没有那没有的
然后你就像apt-get install 安装,又会发现还要更改源,更改源的时候,就需要大致了解一下自己容器的Linux版本了
现在知道了是蝶变,还要更具体一点,不然源好多版本,如下
root@fc2b44e12e2f:/app# cat /etc/apt/sources.list.bak
# deb http://snapshot.debian.org/archive/debian/20200803T070000Z buster main
deb http://deb.debian.org/debian buster main
# deb http://snapshot.debian.org/archive/debian-security/20200803T070000Z buster/updates main
deb http://security.debian.org/debian-security buster/updates main
# deb http://snapshot.debian.org/archive/debian/20200803T070000Z buster-updates main
deb http://deb.debian.org/debian buster-updates main
那就知道了是Debian buster,然后找个buster对应的国内源就行了
比如,我找到了一个阿里的
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
可是没有vim,修改源都费劲
我先备份原来的source.list,然后新建一个文件,
echo "">sources.list
或者
touch sources.list
写文件的话,vim不能用的话,就只好用echo了
echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib">sources.list
好,完成之后,apt-get update一下,现在就可以愉快的install了
5、安装vim
apt-get install vim
6、修改~/.bashrc
为什么要修改这个,因为连ll命令都用不了,将下面ll那行的注释去掉,再执行一下source ~/.bashrc,就可以用ll了
root@fc2b44e12e2f:/app# cat ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
root@fc2b44e12e2f:/app# ll
total 12
-rw-r--r-- 1 root root 281 Aug 19 08:57 Dockerfile
-rw-r--r-- 1 root root 313 Aug 19 08:58 app.py
-rw-r--r-- 1 root root 6 Aug 19 08:58 requirements.txt
Docker制作镜像、启动容器、查看版本、修改源、安装工具相关推荐
- docker制作镜像篇(基于容器)
docker制作镜像可以有两种方式: 一.基于容器(使用busybox制作http镜像) 1.首先运行一个容器 2.在容器当中配置自己的http,添加web目录,增加主页文件等. 3.查看原busyb ...
- Docker 制作镜像
docker镜像制作的两种方法: 1,基于docker commit 制作镜像 2,基于dockerfile制作镜像,为主流镜像制作方式. [基于基础镜像定制后,使用commit制作自定义镜像] 1, ...
- Docker制作镜像(四)
Docker制作镜像(四) 众所周知,docker的镜像是有分层机制的,比如当你下载一个镜像时,你常常会看到以下画面 你发现pull complete了许多,这些全是文件的分层. 这样有什么优势呐? ...
- 在Centos7 更改Docker默认镜像和容器的位置
在Centos7 更改Docker默认镜像和容器的位置 一.Why? 通常,当你开始使用docker时,我们并不太关心Docker 用于存储镜像和容器的默认目录.当使用Docker进行更多实验时,Do ...
- Docker的镜像和容器的区别
Docker的镜像和容器的区别 作者:chszs,版权所有,未经同意,不得转载.博主主页:http://blog.csdn.net/chszs 一.Docker镜像 要理解Docker镜像和Docke ...
- Docker架构镜像及容器管理
Docker概述 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制, ...
- Docker篇之启动容器
Docker篇之启动容器 启动容器的两种方式 基于镜像新建一个容器并启动 将在终止状态(stopped)的容器重新启动 1.新建并启动 命令为 docker run 下面的命令输出一个 "l ...
- 解决:Docker 运行镜像创建容器后,使用docker ps 没有显示正在运行的容器
最近在使用docker在安装jenkins容器时,遇到了通过运行Jenkins镜像后,使用docker ps 没有显示正在运行的容器信息,解决方法如下: 1.使用mkdir -p /home/jenk ...
- Linux启动容器端口,docker安装 创建镜像 启动容器 的 使用方法
1.linux centos 安装docker yum -y install docker 2.启动docker systemctl start docker #启动 docker 服务 system ...
最新文章
- 信号系统的一些基本概念
- 不当免费技术支持的10种方法
- java实现线程间通信的四种方式
- 【正一专栏】欧陆烽火——珍惜像伊布一样的男人!
- C#项目打包,并自动安装SQL数据库(转)
- hdu - 1827 Summer Holiday (强连通)
- ansible图形化界面开源_MySQL常见的图形化工具
- 计算机知识探索怎么写,计算机基础知识及探索.doc
- Shell——输入/输出重定向
- 一文看懂 BDTC 2018:探秘大数据新应用(附 PPT 下载)
- [2019CCPC网络赛][hdu6704]K-th occurrence(后缀数组主席树)
- 如何写一个批量下载PDF文件的程序hp48
- 干货文:企业 IT 基础架构|(精华篇)
- bootstrap-toastr 消息通知插
- PMP考试重点总结四——规划过程组(2)
- 易语言x64驱动鼠标键盘操作技术-支持全系统(win7-win11)
- 阿里 java 电话面试_阿里Java开发电话面试经历--惨败
- 目前微型计算机的工作原理,微机基本工作原理
- 那些让人“口吐芬芳”的APP登录骚操作?!!
- Python全栈 MongoDB 数据库(聚合、二进制、GridFS、pymongo模块)