1、制作镜像所需文件
比如想在容器中瞎捣腾python,那就需要制作一个python的镜像,当然,也可以pull别人的,这里当然选择自己make了
需要在宿主机,就是本机,创建一个目录,用来放制作镜像需要的文件
比如,我在根目录下随便创建了一个docker-file文件夹
[[email protected] ~]# pwd
/root
[[email protected] ~]# 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:
[[email protected] 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目录
[email protected]:/app# pwd
/app
[email protected]:/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
可以查看一下
[[email protected] 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是自定义的容器的名字,如果不指定话,会随机一个名字,比如下面这样
[[email protected] 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 端口映射
等等吧
现在就是启动了容器,可以看到变化吧
4、查看Linux版本
[email protected]:/app# cat /etc/issue
Debian GNU/Linux 10 \n \l
这里为什么要查看Linux版本呢,原因很复杂,噗哈哈
因为这样启动的容器,你会发现好多命令都没有,比如vim没有,这没有那没有的
然后你就像apt-get install 安装,又会发现还要更改源,更改源的时候,就需要大致了解一下自己容器的Linux版本了
现在知道了是蝶变,还要更具体一点,不然源好多版本,如下
[email protected]:/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 -e "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了
[email protected]:/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'
[email protected]:/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