Installation and use of docker under ubuntu

Add apt repository

sudo apt-get update

sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    
sudo mkdir -m 0755 -p /etc/apt/keyrings
# 下载证书
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg


# 使用 echo 命令输出一个包含 Docker APT 源信息的字符串。
# 使用 dpkg 命令获取当前系统的架构信息,并将其插入到源信息字符串中。
# 使用 lsb_release 命令获取当前系统的发行版代号,并将其插入到源信息字符串中。
# 使用 tee 命令将源信息字符串写入到 /etc/apt/sources.list.d/docker.list 文件中,该文件将被 apt-get 命令用于更新 Docker 软件包。
# 使用重定向符号 > 将 tee 命令的输出重定向到 /dev/null,以便禁止在控制台上显示源信息字符串。
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install docker engine

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

File Directory

The file directory under ubuntu dockeris usually /var/lib/docker
but only the root can enter this directory, it needs to be converted to root permissions

sudo su -
cd /var/lib/docker

/var is a directory in the Linux system, which is used to store files that change frequently during system operation. These include:

  • Log files: For example, the log files of the system and applications are saved in the /var/log directory.

  • Cache files: For example, the cache files downloaded during software installation are stored in the /var/cache directory.

  • Runtime data: For example, the /var/run directory saves some system services and status information when the process is running.

  • Mail: For example, the mail of local users is saved in the /var/mail directory.

  • Printing tasks: For example, printing tasks are saved in the /var/spool directory.

In short, the /var directory is used to store data that needs to be dynamically modified, grown, or deleted. In contrast, the /etc directory is used to store static configuration files, and the /usr directory is used to store read-only data and applications.

The /var/lib/docker/containerfolder is used to store container, Docker

start off

Take mysql as an example

docker run -itd --name -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123123 mysql
# 启动一个mysql
# -p 3306:3306 使主机的3306映射到容器的3306 可以通过主机上的Mysql访问Mysql
# 每次docker run之后会生成一个容器,并且保存所有设定参数,这些参数很难修改,第二次直接start即可

closure

docket stop mysql

start up

docker start mysql

boot

The docker container can be made into a service, so that it starts with the system startup

sudo su -
cd /etc/systemed/system
touch docker-container.service
vim docker-container.service

Add as follows

[Unit]
Description=Docker Container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/bin/bash -c "/usr/bin/docker start mysql1 && /usr/bin/docker start -a mysql2"
ExecStop=/bin/bash -c "/usr/bin/docker start -t 2 mysql1 && /usr/bin/docker start -t 2 mysql2"

[Install]
WantedBy=multi-user.target
# 这里启动的是两个容器/多个容器如果是一个可以改为
###########################################
# ExecStart=/usr/bin/docker start -a mysql
# ExecStop=/usr/bin/docker stop -t 2 mysql

Container internal settings

If the container is an ubuntu system

You can docker exec -it image_name bashenter the container to perform some operations through

If there is a program that needs to be started together when the container starts, it can be configured inside the container

vim /root/.bashrc

in which to join

# startup run
if [ -f /root/startup_run.sh ]; then
      ./root/startup_run.sh
fi
touch /root/startup_run.sh
vim /root/startup_run.sh

You can add commands to be executed in this file

# 比如我的容器里有一个jupyter notebook
#可以使用
nohup jupyter notebook --allow-root >> /root/jupyter.log 2>&1 &

After this is set up, every time the container is started, ajupyter notebook

The physical location of the container

docker runAfterwards, a container will be generated, which is located /var/lib/docker/containersin a folder, but the name of the folder inside is very strange
insert image description here
. They are all like this. This is actually a container.Id

Need to use docker inspectview container id

docker inspect container_name | grep 'Id'
# 在这里 grep是一个命令行搜索程序,可以搜索后边的参数,
# | 字符称为管道符号,它用于将一个命令的输出重定向到另一个命令作为输入,是一种最基础的管道

insert image description here
In this way, we can enter the container directory through `Id
,

container modification

The folders under the directory are like this, you can make some necessary modifications to these files, and you can achieve the effect of modifying the container
insert image description here

can also use

docker container update 

to make some modifications, but with many restrictions

Another method that is less prone to problems is to encapsulate the container back into the image, and then docker runadd parameters when restarting

docker stop container_name
docker commit container_name new_name
docker run ......
# 在这里配置具体参数
# 比如端口 名称等

Guess you like

Origin blog.csdn.net/majiayu000/article/details/129564670