Docker#Docker used as a virtual machine

Use docker container as a virtual machine, once and for all

Insert picture description here
Containers are actually a sandbox technology. As the name suggests, a sandbox is a technology that can "pack" your application like a container. In this way, applications and applications will not interfere with each other because there are boundaries; and applications that are packed into containers can also be moved around easily, which is actually the most ideal state of PaaS. The essence of the container is the process, and the container is the process in the future cloud computing system.
Cloud computing service type
Infrastructure as a service IaaS
Platform as a service (PaaS)
Software as a service (SaaS)

Comparison of docker container and virtual machine

Insert picture description here

The pain point of using docker container as a virtual machine

1. The basic commands of docker native container are ip a, ss, ssh, etc. 2. There is no
systemctl command of docker native container.
3. The docker startup command is too long to remember

solution

Use dockerfile to build a mirror, first install the packages required by the basic commands you can think of, solve problem 1, use privileged mode to build a container, solve problem 2, docker start command uses alias to set alias, solve problem 3. After completing the above steps, congratulations, the virtual machine can be turned on by you, and the whole process is painless

specific methods

1. First use dockerfile to build a custom basic image

[root@sun ~]# cd /mnt
[root@sun mnt]# mkdir centos7
[root@sun mnt]# cd centos7/
[root@sun centos7]# vim Dockerfile
[root@sun centos7]# cat Dockerfile 
FROM centos:7
RUN yum install -y \
    vim bash-com* openssh-clients openssh-server iproute cronie;\
    yum group install -y "Development Tools";yum clean all;\
    localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV LANG=zh_CN.UTF-8
构建自定义的docker镜像,名字自定义
[root@sun centos7]# docker build . -t base-centos
出现下面语句,表示构建成功
Successfully built 325f14a33587
Successfully tagged base-centos:latest
[root@sun centos7]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
base-centos         latest              325f14a33587        3 minutes ago       522MB
sunlizhen-ng        latest              380f3ce262e9        4 days ago          843MB
centos              7                   7e6257c9f8d8        2 months ago        203MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

可以看到base-centos基础镜像已经在我们的自己的镜像仓库里面
现在已经拥有了一个支持基本命令和支持中文的镜像了。

2. You can use the systemctl command to enter in privileged mode

docker run -it --name mydocker1 --rm --privileged=true base-centos /usr/sbin/init

--Name container name
--rm exit automatically delete container
--privileged privileged mode
/usr/sbin/init system No. 1 process command

A problem encountered may be my system environment problem:
docker run -it --name mydocker1 --rm --privileged=true base-centos /usr/sbin/init After
executing this command, it led to my centos7 real machine The system restarts.
Personal level is limited. I can’t solve it. I don’t know the reason. I can only use another method
. Run docker one after another, and then enter
docker run with exec -itd --name mydocker1 --rm --privileged=true base-centos /usr/ sbin/init
docker exec -it mydocker1 bash

3. Start the docker command and the parameters are too long. Set command alias
  First of all, it is a personal habit to give the container a name that is easy to remember and distinguish when building the container. The advantage is that it is easy to manage. So when setting the alias, set a location variable $1, which is used as the container name.

[root@sun ~]# vim /etc/profile.d/docker.sh
function mydocker(){
    
    
docker run -it --name $1 --rm --privileged=true base-centos /usr/sbin/init
}
alias ops='mydocker'
[root@sun ~]# source /etc/profile.d/docker.sh

Test example:
Insert picture description here
Insert picture description here
If you want to change the container hostname, add a –hostname parameter

[root@sun ~]# vim /etc/profile.d/docker.sh
function mydocker(){
    
    
docker run -it --name $1 --hostname $2 --rm base-centos
}
alias ops='mydocker'
[root@sun ~]# source /etc/profile.d/docker.sh

That is, to execute the command, you need to pass two location variables, the
container name and the container host name.
Test example So
Insert picture description here
far, you can easily have a new virtual machine.

Supplement 1: If you run the build directly and enter the container, the system restarts.

[root@sun ~]# vim /etc/profile.d/docker.sh
function mydocker(){
    
    
docker run -itd --name $1 --rm --privileged=true base-centos /usr/sbin/init
docker exec -it $1 bash
}
alias ops='mydocker'
[root@sun ~]# source /etc/profile.d/docker.sh

Supplement 2: Export and import of containers

Usage scenario: Install mysql and other services in a container. If you want to build a container that also has mysql and other services,
you can first export the current container as an arbitrarily named compressed package:
docker export container name/id> centos7-mysql.tar
Then copy the compressed package to the required server, and the machine needs to directly import it. Import
the container compressed package as a local image
docker import centos7-mysql.tar
Note that the import command is the image that the container snapshot is imported as
docker images. The
image label
docker needs to be redefined tag Image ID centos: 7
tag added image ID, add warehouse name and tag to the newly
imported image, repeat operation will not overwrite, it will be created (docker tag centos:7 sun:8)
docker run -it --rm sunlizhen bash The mirror imported by yourself, need to be followed by a command after run

Guess you like

Origin blog.csdn.net/kakaops_qing/article/details/109089858