preface
This lecture is from Docker series explanation courses , a separate section, Previous chapters It is mainly theory + practice. Since this chapter, it has entered the advanced application of Docker.
Before browsing this article, it is recommended to understand Use of Dockerfile core commands,Optimization of Dockerfile
After mastering the use of Dockerfile, it is helpful to learn yaml files in later k8s. They all complete the automatic deployment of containers by writing configuration files.
1, Customize Redis local container through Dockerfile
1. Download the source code package of the specified version on the official redis website and upload it to the specified folder of the host computer
note appended: redis official website ,redis-6.0.6.tar.gz Download
cd && mkdir -p test/image/docker-redis #Switch to the root directory and create a directory cd test/image/docker-redis #Switch to the image storage directory rz #Upload the downloaded redis source package
2. Edit Dockerfile
ls vim Dockerfile
FROM centos RUN ["yum","install","-y","gcc","gcc-c++","net-tools","make"] # Install the operating environment that IDS depends on WORKDIR /usr/local ADD redis-6.0.6.tar.gz . # Unzip the source package to the current WORKDIR WORKDIR /usr/local/redis-6.0.6/src RUN make && make install # Compile and install in the extracted src directory WORKDIR /usr/local/redis-6.0.6 RUN ["mkdir","/etc/redis"] && ["cp","redis.conf","/etc/redis/6379.conf"] # Copy a configuration file to etc (try not to modify the original configuration file) EXPOSE 7000 # Specifies the port on which the container is exposed CMD ["../bin/redis-server","redis.config"] # Specify the profile to start Redis service
3.docker build -t build container
docker build -f Dockerfile -t succ.com/dockr_redis .
Special note: if the dockerfile itself is in the current directory when the docker build command is executed, you can not specify the location of the dockerfile with - f; The following image name can also be customized according to the actual situation. Tail, If you're interested in him, Click to enter (see Chapter 3).
If there is no syntax error in your Dockerfile, you can see the following prompt for successful construction and return the id of the image
4.docker images view container list
docker images #View and verify whether the self built image is built successfully
You can see that the image you built is successful
5.docker run runs redis container
docker run -d --name redis -p 7000:6379 succ.com/dockr_redis
6. After docker PS checks the container id, exec enters the container
docker ps -a docker exec -it redis /bin/bash
2, Use the Redis container provided on the docker official website
In practice, Redis images are rarely customized. Go directly to the official to find the image name and pull through docker Official image Name is enough.
docker pull reids:6.2.6 #Pull the specified redis version image docker images #View mirror list docker run -d redis:6.2.6 docker exec -it a9 /bin/bash #Enter the inside of the container according to the initial of the container ID cd /usr/local/bin/ ls redis-cli #Run redis client set k2 hell get k2
summary
The use of dockerfile is relatively simple compared with yaml. It is mainly to specify the benchmark image FROM, then ADD the downloaded installation package FROM the host to the container through ADD or COPY, then execute some commands through run, and finally use CMD command, Start the container service (so that when using the image, you can automatically start the internal service of the container when running the image directly through the docker run command).
Of course, in actual work, the official image is usually pulled directly, and then some modifications, configurations, clusters and other operations are done on the basis of this image. This article will not expand in depth, but mainly introduce the simple use of Dockerfile.
Last words
The content of this lecture is from Docker introduction to advanced The content extracted from it, although the knowledge points are relatively simple, is also an indispensable part of docker's learning. I hope it will be helpful to you. If there is anything wrong, please criticize and correct it. On the way of learning, we will forge ahead together!
note appended
Guess what you might be interested in
1,Eight core commands of Dockerfile | use of Dockerfile
5,Docker container life cycle | the difference between kill and stop