Linux installation of docker and summary of basic operations of docker

WBOY
Release: 2021-12-30 17:50:40
forward
2368 people have browsed it

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!