Table of Contents
1. Install docker
2. Mirror operation
四.镜像的导入与导出
Home Operation and Maintenance Linux Operation and Maintenance Linux installation of docker and summary of basic operations of docker

Linux installation of docker and summary of basic operations of docker

Dec 30, 2021 pm 05:49 PM
linux

This article brings you how to install docker on Linux and the basic operations of docker. I hope it will be helpful to you.

Linux installation of docker and summary of basic operations of docker

1. Install docker

Docker is required to run on Centos 7, the system is required to be 64-bit, and the system kernel version is 3.10 or above

1.uname -an View the current system version

2.yum -y install docker Download and install docker

3.service docker start Start the docker service

4.docker version Check whether docker is installed successfully

When you see the information in the picture below, it means The local docker has been installed successfully, it is very simple

                                                 

2. Mirror operation

Creating a container must be based on the image, so first Let’s talk about the operation of docker images

Search for images

docker images ll Check whether the local machine already has an image

Currently there is no image in the machine, go to Docker Hub to download (the image can also be customized, I won’t go into details here)

docker search java , you can also specify a specific version to download,

For example: docker search Ubuntu: 1.2.5.4, you can search docker Hub and many images will be listed

Download the image

docker pull docker.io/nginx to download

The image downloaded locally is larger than the one searched on docker Hub because it is automatically decompressed during the download process , when viewing the image list, you will see the image you just downloaded

The list includes the warehouse name, version label, image ID, creation time and occupied space

##Delete image

Delete useless image docker rmi image id

3. Image creation and management

We have downloaded the Nginx image earlier, and then we will create a container with only Nginx application docker run -i -t /bin/bash: -i: standard input to the container -t: allocation A virtual terminal/bin/bash: execute the bash script,

docker run -idt --name container_nginx -p 8080:80  docker.io/nginx
Copy after login

Start a container using the image docker.io/nginx, named container_nginx, -p 8080:80 means mapping the container's port 80 to the host's 8080 port, so that we only need to access the 8080 port of the host to access the container's service.

Note: There are two - in front of the name, -p in front of the port, docker.io/nginx is the image name, 8080 is the port of the host, and 80 is the port of the Nginx application

A port on the host can only be mapped to one container port, and multiple container ports cannot correspond to one host port (if the container is installed on a centos-like system, the container port can be set casually, but if the container is It is just a simple application, then the container port should be the port of the application itself)

In this way we create and start a container!

exit 退出容器

docker ps 查看运行中的容器

docker ps -a  查看运行中和非运行中的所有容器

docker exec -it container_nginx /bin/bash  进入容器

如果容器还未启动 执行docker start container_nginx
Copy after login

Start Nginx after entering the container

whereis nginx 找Nginx的启动目录

[root@iz2zehzeir87zi8q99krk1z ~]# docker start container_nginx
container_nginx
[root@iz2zehzeir87zi8q99krk1z ~]# docker exec -it container_nginx /bin/bash
root@84683e425116:/# whereis  nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@84683e425116:/#  /usr/sbin/nginx
Copy after login

At this time, visit http://51.110.218.9:8080/ in the browser to directly access Nginx in the container

If the access is unsuccessful, it may be that the firewall of the host port is open. Execute the following command to close it

/ sbin / iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
Copy after login

Since I use the Alibaba Cloud server, I need to set port 8080 in Alibaba Cloud. Open

                             

Delete container

容器删除之前先将容器停止  

docker stop container_nginx 或者是容器的id

docker rm -f container_nginx  容器删除
Copy after login

The difference between docker start and docker run

docker start name 启动一个已经创建的容器

docker run 创建并启动一个容器
Copy after login
## The #docker run command is actually a combination of docker create and docker start. First execute docker create to create a container, and then docker start to start.

The host and container files copy each other

从主机复制到容器 sudo docker cp host_path containerID:container_path

从容器复制到主机 sudo docker cp containerID:container_path host_path
Copy after login

Please note that the above two commands are executed in the host and cannot be executed in the container

docker cp container_nginx:/usr/local/xin.txt /usr/local/software/ 容器向主机复制文件
docker cp /usr/local/xinzhifu.txt container_nginx:/usr/local/ 主机向容器复制文件

这样一个基础的docker容器就创建完了 。。。。。。。。。。。。

反过来大家再看一看docker的容器与镜像的区别 https://www.cnblogs.com/linjiaxin/p/7381421.html

那么其实镜像与容器的本质区别并不大,那么镜像可以生成容器 ,容器是否可以做成镜像呢?

docket commit container_nginx  image_nginx:v1
             
              容器名            自己起一个镜像的名字:版本号
Copy after login

用当前的容器生成了redis镜像

例如:A、B两台机器都想安装redis,A机器上创建容器并在容器中做好redis的一切配置,让后将这个容器docker commit 成镜像image_redis,B机器也想要安装redis,直接用镜像image_redis创建容器就行了,docker就是做这样一劳永逸的事情。

而且传统方式得在每台机器上安装配置redis非常麻烦

四.镜像的导入与导出

镜像压缩打包 (主机上进行操作),有两种方式 docker save 与 docker load 和 docker export 与 docker import

docker save nginx | gzip > nginx_xin_image.tar.gz  将现有的镜像压缩打包

docker load -i nginx_xin_image.tar.gz  压缩的镜像解压

docker images 进行查看
Copy after login

docker save 是直接将镜像进行打包 docker save <镜像名>或<镜像id>

docker export container_nginx> nginx_image.tar  

cat nginx_image.tar | sudo docker import  - nginx_image:import
Copy after login

docker export 是直接将容器进行打包   docker save <容器名>或<容器id> 

需要注意两种方法配套的,切不可混用。虽然导入导出时没问题,但是在创建容器时候会报错

如果使用import导入save产生的文件,虽然导入不提示错误,但是启动容器时会提示失败,

会出现类似"docker: Error response from daemon: Container command not found or does not exist"的错误。

类似,使用load载入export产生的文件,也会出现问题。

相关推荐:《Linux视频教程

The above is the detailed content of Linux installation of docker and summary of basic operations of docker. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

See all articles