Article directory
1. Docker installation and deployment
Use aliyun docker yum source to install the new version of docker
docker-ce enterprise version docker
docker-ee community version docker
1. Delete the installed Docker
Note: This step is optional. If docker exists in the system, we will delete it.
[root@localhost ~]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
2. Configure Alibaba Cloud Docker Yum source
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 git
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3. Install Docker
1) Install the specified version of Docker:
First check all versions of Docker and then select the version you want to install.
[root@localhost ~]# yum list docker-ce --showduplicates
You need to specify the complete rpm package name and add the parameter - -setopt=obsoletes=0. Take the installation of version 17.03.2 as an example:
[root@localhost ~]# yum install -y --setopt=obsoletes=0 docker-ce-17.03.2.ce-1.el7.centos.x86_64 docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch
2) Install the latest version of Docker (the one I use)
[root@localhost ~]# yum -y install docker-ce
4. Start Docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
#Start on boot
Check Docker version
[root@localhost ~]# docker -v
Check docker running status
[root@localhost ~]# docker info
2. Use of domestic and foreign mirror warehouses
1. Use of domestic mirrors
Domestic mirror website
https://www.daocloud.io is
a domestic docker mirror warehouse. It not only supports obtaining docker images through commands, but also supports obtaining docker images graphically.
These two methods can be used to download any image. Here is an example of obtaining the docker image of tomcat:
1) In command mode
Use the command to obtain the tomcat image. You can refer to the official command to obtain it:
follow the command to download the image
. Follow the command to use the image.
The server operation is as follows: first download the image, then use the image
[root@localhost ~]# docker pull daoclould.io/library/tomcat:8.0.45
#Download Mirror
[root@localhost ~]# docker run -itd -p 8080:8080 --rm daocloud.io/library/tomcat:8.0.45
#Using Mirror
In fact, you don’t need to execute the first command to download the mirror. You can directly execute the second sentence. When it is detected that there is no local mirror to be used, it will be automatically downloaded.
2) Graphically
Get the tomcat image graphically :
First, you need to associate daocloud with the server
. Execute the command on the server
to associate the server with daocloud. After the association is completed, select the image you want to download (tomcat is selected here). At this time , use the server ip + the port mapped by the container
in the browser. Visit tomcat.
2. Use of foreign mirrors
Foreign mirror website
https://hub.docker.com
Foreign mirror websites are also the official mirror warehouse of docker. They can only download mirrors through commands.
Taking the nginx image as an example, the official command will be provided
Download image
Use mirroring
The server operates as follows:
[root@localhost ~]# docker pull nginx
[root@localhost ~]# docker run -itd --name zy -p 666:80 nginx
At this time , nginx can be accessed in the browser using the server IP + the port mapped by the container.
3. Mirror accelerator
When using Docker, you need to frequently obtain images from the official repository. However, due to obvious network reasons, the process of pulling images is very time-consuming, which seriously affects the experience of using Docker. Therefore, DaoDloud launched an accelerator tool to solve this problem. Through intelligent routing and caching mechanisms, it has greatly improved the speed of domestic network access to Docker Hub. It currently has a wide user base and is highly recommended by Docker officials.
Click the little rocket icon on the daocloud website to see the command document for configuring the accelerator.
1. Configure Docker mirror station
[root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
2. Restart docker
[root@localhost ~]# systemctl restart docker
4. Log in and exit Docker Hub
To log in to your own Docker register, you need to have a Docker Hub registered account
Login
[root@localhost ~]# docker login
quit
[root@localhost ~]# docker logout
3. Docker command usage
1. Basic commands
(1) Basic commands
[root@localhost ~]# systemctl start docker
#Start docker
[root@localhost ~]# systemctl stop docker
#Close docker
[root@localhost ~]# systemctl restart docker
and restart docker''
[root@localhost ~]# systemctl enable docker
#Start on boot
(2) Help manual
[root@localhost ~]# docker --help
#View the help documentation of docker
[root@localhost ~]# docker run --help
#View the help documentation of the docker run command
(3) View version and information
[root@localhost ~]# docker -v
[root@localhost ~]# docker info
2. Docker image commands
(1) View local mirror
[root@localhost ~]# docker images
(2) Pull the image.
If the version number tag is not added, the latest version of the image will be pulled. If the tag is added, the specified version will be pulled.
[root@localhost ~]# docker pull 镜像名
[root@localhost ~]# docker pull 镜像名:版本
(3) Delete the image
[root@localhost ~]# docker rmi 镜像名称:版本
or
[root@localhost ~]# docker rmi 镜像id
3. Docker container commands
(1) View running containers
[root@localhost ~]# docker ps
Field meaning:
CONTAINER ID: the ID of the container
IMAGE: the image used
COMMAND: the command run when starting the container
CREATED: the time when the container was created
STATUS: the status of the container
PORTS: the port information of the container and the link type used
NAMES: the name of the container
(2) View all containers
[root@localhost ~]# docker ps -a
(3) View the IDs of all containers
Can be used to delete containers in batches
[root@localhost ~]# docker ps -a -q
#-a displays all, -q displays only ID
[root@localhost ~]# docker rm $(docker ps -a -q)
#Delete all containers in batches
(4) View detailed information of a container
[root@localhost ~]# docker inspect 容器名或容器id
(5) Run the container
How does the container come about? Use the run command to build an image into a container.
Parameters of run command:
-d #后台运行,并返回容器id
-i #以交互模式运行容器,通常与-t同时使用
-t #为容器重新分分配一个伪输入终端,通常与-i同时使用
-p #端口映射,格式为:主机(宿主)端口:容器端口
-v #文件映射,格式为:主机目录:容器目录
-h #定义容器主机名
--name “名称” #为容器指定一个名称
--privileged #以特权模式运行
example:
To run the container, use the daocloud.io/library/tomcat:8.0.45 image to run the container in background mode. The container is named tomcat-zy.
[root@localhost ~]# docker run -itd --name tomcat-zy daocloud.io/library/tomcat:8.0.45
Run the container, use the nginx:v1 image, start a container in background mode, name the container nginx, map the container's port 80 to the host's port 8088, and map the container's directory /usr/local/nginx/html to the host's On the directory /root/dist
[root@localhost ~]# docker run -itd --name nginx -p 8088:80 -v /root/dist:/usr/local/nginx/html nginx:v1
(6) Enter inside the container
[root@localhost ~]# docker exec -it 容器名字或容器id /bin/bash
(7) Stop the container
[root@localhost ~]# docker stop 容器名字或容器id
(8) Start the container
[root@localhost ~]# docker start 容器名字或容器id
(9) Delete container
To delete a container, you need to stop the container first
[root@localhost ~]# docker rm 容器名字或容器id
(9) Copy files
To delete a container, you need to stop the container first
[root@localhost ~]# docker rm 容器名字或容器id
4. Import and export of containers and images
1) export, import container import and export
Pack the container’s file system into a tarball
export: There are two ways.
The first one:
[root@localhost ~]# docker export -o zy.tar zhangyu
The second type:
[root@localhost ~]# docker export zhangyu > zy.tar
import imports
the container tar package, which is a mirror after import.
[root@localhost ~]# docker import xxx.tar newname:tag
For example:
[root@localhost ~]# docker import zy.tar tomcat:v1
Note: When running the imported image, you must add /bin/bash, otherwise an error will be reported Error response from daemon: No command specified.
2) Import and export of save and load images
Make the image into a tar package and import it into other hosts
save packaged image
[root@localhost ~]# docker save -o centos.tar daocloud.io/library/centos:7
load loads the packaged image
[root@localhost ~]# docker load < centos.tar
3) Commit to build the image
Commit can create a new image from the operated container
docker commit [容器名称|ID] 生成新的镜像名字
Example:
[root@localhost ~]# docker commit -m "my images version1" -a "zhangyu" 108a85b1ed99 daocloud.io/ubuntu:v2
Common parameters:
-a: Author of the submitted image
-c : Use dockerfile instructions to create the image
-m: Description text when submitting
-p : When committing, Pause running containers
4. Create an image using Dockerfile
Parameters/instructions in Dockerfile
1. FROM specifies the base image
2. MAINTAINER description information
3. ENV sets environment variables 4.
RUN runs the command
5. ADD adds files to the container. If it is a compressed package, it will be automatically decompressed
6. COPY copies the file. Different from ADD, the compressed package will not be automatically decompressed
7. WORKDIR specifies the working directory
8. EXPOSE specifies the working directory
9. CMD specifies the command to run the image
example:
Build an nginx image yourself and write a dockerfile file. The content of the file is based on a centos image. After compiling and installing nginx in the container, rebuild it into a new image.
First prepare a centos7 docker base image
Write dockerfile
Each line of command is written in the form of instruction (uppercase) + list.
The content is:
FROM daocloud.io/library/centos:7
MAINTAINER "wuyang0304" yuzhang0304@163.com
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum clean all && yum makecache fast
RUN yum -y install gcc openssl openssl-devel pcre-devel zlib-devel make
ADD nginx-1.22.0.tar.gz /usr/local/
WORKDIR /usr/local/nginx-1.22.0
RUN ./configure --prefix=/usr/local/nginx
RUN make && make install
WORKDIR /usr/local/nginx
RUN rm -rf /usr/local/nginx-1.22.0
ENV NGINX_HOME=/usr/local/nginx
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/nginx/sbin
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
Run the dockerfile file to create the image
[root@localhost ~]# docker bulid -t nginx-zy:v1 ..
"." is used to indicate the current directory of the Dockerfile file we use. You can use the - -file, -f parameters to specify the full path of the Dockerfile.
At this point a new image has been created, which can be used locally or copied to other docker servers.
Run the created image
[root@localhost ~]# docker run -itd --name nginx-zy -p 81:80 nginx-zy:v1
You can directly access nginx through server IP + port, without having to redeploy nginx.