Continuous deployment tutorial (4) to make docker alive

1 Introduction

In the previous chapters, we explained how the server is built, how to install and optimize docker, and also obtained a container through a command. In this chapter, we explain the basic operation commands of docker, so that docker not only stays in concept, but in our Get started to live and act as a weapon in our hands.

2. General instructions

2.1 View various information of docker

docker version #查看docker版本 
docker info    #查看docker信息 
docker --help #查看docker的帮助

2.2 Start and notify the docker service

systemctl start docker  #启动docker服务
systemctl stop docker  #停止docker服务

3. Mirror

The image is the basis of the container. Only when you have the image first can the corresponding container be generated. At the same time, we can also generate the container as the image we need.
First of all, we must first have a mirror. Through the commands of the previous article, we ran a container. If you skipped this chapter, it does not matter, please output the following command, have a ubuntu 18.10 version of the container, and get A mirror.

docker run  -p 8080:80  --name ubuntu18_base   ubuntu:18.10

3.1 Basic operation

  • View local image
docker images

Insert picture description here

  • View local image and summary information
docker images --digests

Insert picture description here

  • Only pull the image to the local. With the command, we can pull the image without creating a container.
docker pull ubuntu:18.10

Insert picture description here

  • Delete the image The image ID is queried by docker images and deleted by Id
docker rmi ["imges Id"]

Insert picture description here

  • Forcibly delete the image If the -f parameter is added, the image will be judged when the image is deleted. If the currently pre-deleted image has a running container, it is not allowed to be deleted. If the -f parameter is added, this condition will be ignored and forced to be deleted.
docker rmi -f ["imges Id"]

3.2 View mirror

Docker hopes to be able to encapsulate all applications in the image to achieve out of the box. This concept is also supported by many major manufacturers, so many companies have launched their own mirrors, such as mysql, redis, etc. Next, we will talk about how to retrieve the corresponding mirror file on the website and implement the download.
Registering DockerHub not only retrieves and obtains images from other manufacturers, but also pushes your images to it, which is defined as private or public, which is convenient for everyone and you to download.

step 1: Visit DockerHub

Visit DockerHub, the URL is: https://hub.docker.com The network speed is very slow, you need a little patience, after successful access, registration and login success.
Insert picture description here

step 2: search mysql in the search bar

We can retrieve many versions of the mysql image, we open the first one.
Insert picture description here
We can see that the official website provides us with a command to push the image to the local, paste it to the local execution, and the corresponding image can be obtained after the execution is successful.
Insert picture description here

4. container

4.1 Starting the container

docker run    [option]  [镜像名称]
parameter Explanation Sample
–name Define a container name ubuntu_base
-d Background process -d
-it Run the container in interactive mode -it
-p Specify mapping ports outside and inside the container 8080:80

Wouldn't it be a lot clearer to read this command again?

docker run  -p 8080:80  --name ubuntu18_base   ubuntu:18.10

4.2 View container

#View the starting container

docker ps

#View all containers (starting + history running)

docker ps  -a 

#View container information

docker inspect <容器名称 or 容器Id> 

#View the process in the container

docker top <容器名称 or 容器Id> 

4.3 Starting and stopping containers

#Restart the container

docker restart <容器名称 or 容器Id> 

#Stop container

docker stop <容器id/容器名>   #停止容器
docker kill <容器id/容器名>   #强制停止容器
docker rm   <容器id/容器名>   #删除容器

# Load a container subsequent positive start will be frequently used here is the key here is the important point here is the key

docker attach <容器名称 or 容器Id> 
exit 		#容器停止并退出容器
ctrl+p+q 	#组合键 容器不停止并退出

4.4 View container logs

docker logs -f -t --tail <容器id>

4.5 Generate image

step 1: Image generation through command

By executing the following three commands, we realized the process of generating the container as a mirror.

#下载容器到本地
docker run  -p 8080:80  --name ubuntu18_base   ubuntu:18.10
#提交容器为镜像
docker commit -a 'zhangbo' -m 'add jdk 1.8 add vim add nginx' ubuntu18_base mdjzbsq/docker_ubuntu_base:1.0
#查看镜像生成情况
docker images

Insert picture description here

step 2: Command explanation

docker commit [option] [容器名称] [REPOSITORY[:TAG]]
  • option
parameter Explanation Sample
-a Author who submitted the image zhangbo
-m What's added to the image add jdk 1.8 add vim add nginx
  • [Container name]
    is the name of the image container you need to submit.
  • [REPOSITORY [: TAG]]
    is the name of the image you need to upload, the format is [warehouse name] / [image name]: [version number]

4.6 Push image

Next, we will push the generated image to DockerHub so that we can download it later.

step 1: Create the corresponding warehouse on DockerHubInsert picture description here

step 2: Push the image to the warehouse

#输入命令后 会要去输入 username、password,输入DockerHub上申请的帐号密码即可。返回login Succeeded则表示登录成功
docker login
#推送已经生成的镜像文件
docker push mdjzbsq/docker_ubuntu_base:1.0

Insert picture description here

step 3: go to DockerHub to verify the push result

As shown in the figure, this is the image we pushed to the DockerHub official website.
Insert picture description here
In order to verify the effect, we can download this image to the local (in order to verify the effect, please manually delete the containers and images that already exist locally ).
Run the following command to specify that the downloaded image is the image and version that we pushed to the external network.

docker run  -p 8080:80  --name my_ubuntu18  mdjzbsq/docker_ubuntu_base:1.0

Insert picture description here

summary

In this chapter, we introduced how to operate the image, how to operate the container, how to convert the container into an image and push it to the official website. Next, we will start from actual combat and build a dedicated front-end application container through the relevant knowledge we have learned today to prepare for the sustainable deployment of subsequent chapters.

Published 17 original articles · praised 0 · visits 467

Guess you like

Origin blog.csdn.net/weixin_36008116/article/details/105513097