There are two ways to create a Docker image: 1. Use the "docker commit" command to manually build the image based on the existing container; 2. Use Dockerfile to automatically build the image. The Docker program will read the instructions in the Dockerfile build file. Automatically generate images.
The operating environment of this tutorial: linux5.9.8 system, docker-1.13.1 version, Dell G3 computer.
Sometimes the image downloaded from the Docker image warehouse cannot meet the requirements. We can build an own image based on a basic image.
Introduction to image construction
Under what circumstances Next we need to build the image ourselves?
(1) When we cannot find an existing image, such as an application developed by ourselves
(2) We need to add specific functions to the image
docker build There are two ways to image: docker commit
command and Dockerfile
Build file
docker commit build image
Building an image based on an existing container is mainly done through docker commit
command to build a new image.
There are three main steps for dockercommit construction:
- Run the container
- Modify the container
- Save the container as a new image
For example, install the vim editor in the centos image and save it as a new image
(1) Run the container
[root@ken1 docker]# docker run -it centos Unable to find image 'centos:latest' locally latest: Pulling from library/centos a02a4930cb5d: Pull complete Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426 Status: Downloaded newer image for centos:latest
(2) Install the vim editor
vim The editor confirms that it is not installed
[root@69f501e858a6 /]# vim bash: vim: command not found
Install it
[root@69f501e858a6 /]# yum install vim -y
(3) Save it as a new image
First check the currently running image
[root@ken1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69f501e858a6 centos "/bin/bash" 2 minutes ago Up 2 minutes quizzical_torvalds
Use commit Save as a new image
[root@ken1 ~]# docker commit 69f501e858a6 centos-vim sha256:42083b89a179368bc29a8f40d14f8824990183c8e4b28fd84411d440c26342e5
69f501e858a6 is the ID of the running container. You can also use the name below name.
centos-vim is the name of the new image.
Check to see if centos-vim is available. Mirror
Restart the new mirror and verify whether the vim editor can be used
You can find that the new mirror can be used with the vim editor
[root@ken1 ~]# docker run -it centos-vim [root@61d090898bad /]# vim [root@61d090898bad /]# vim test
The above demonstrates how to use commit to create a new image, but docker does not recommend using this method to create an image for the following reasons:
- This is a manual way to create an image. , the container is wrong, and the efficiency is low and the repeatability is weak
- More importantly. The user does not know how the image is created. Is there any malicious program inside?
Dockerfile builds the image
Dockerfile is a file composed of a set of instructions. Each instruction corresponds to a command in Linux. The Docker program reads the Dockerfile The instructions in ultimately generate the image.
The first Dockerfike
Step one: Create a new directory
[root@ken1 ~]# mkdir /test
Step two: Write a Dockerfile
The name is Dockerfile, and The first D needs to be capitalized
[root@ken1 ~]# cat Dockerfile FROM centos RUN yum install vim -y
FROMcentos means using the centos basic image
RUN means installing the vim editor on centos
Step 3: Build the image
[root@ken1 ~]# docker build -t centos-vim2 .
-t is followed by specifying the tag name (tag) of the new image
. The last dot indicates that the docker context is the current directory. Docker will search for the Dockerfile file from the build context by default. We can also specify the location of the Dockerfile through the -f parameter
Step 4: View the image
View the image hierarchical structure
10.CMD
容器启动时运行指定的命令
dockerfile中可以多个CMD指令,但是只要最后一个生效。CMD可以被docker run之后的参数替换
11.ENTRYPOINT
设置容器启东市的命令
dockerfile中可以有多个ENTRYPOINT,但是只有最后一个生效。
CMD或者docker run之后的参数会被当做参数传递给ENTERYPOINT.
Dockerfile演示
下面演示一个比较全面的dockerfile
[root@ken1 test]# cat Dockerfile #my Dockerfile FROM busybox MAINTAINER ken WORKDIR /ken RUN touch test COPY ["ken1","."] ADD ["wordpress.tar.gz","."] ENV name "ken"
注意:Dockerfile支持以#开头的注释
构建镜像
[root@ken1 test]# docker build -t myimage . Sending build context to Docker daemon 4.281MB Step 1/7 : FROM busybox ---> 3a093384ac30 Step 2/7 : MAINTAINER ken ---> Running in 2a73a83507ce Removing intermediate container 2a73a83507ce ---> 8c3df9b3d823 Step 3/7 : WORKDIR /ken ---> Running in 31c6f9fe2195 Removing intermediate container 31c6f9fe2195 ---> a458cf986072 Step 4/7 : RUN touch test ---> Running in e1b08ebd363c Removing intermediate container e1b08ebd363c ---> 41601920009a Step 5/7 : COPY ["ken1","."] ---> 2ebfa0933fca Step 6/7 : ADD ["wordpress.tar.gz","."] ---> d0ad29d3aa34 Step 7/7 : ENV name "ken" ---> Running in fceae6e20e63 Removing intermediate container fceae6e20e63 ---> 7efe0600e48f Successfully built 7efe0600e48f Successfully tagged myimage:latest
查看镜像
运行该镜像
[root@ken1 test]# docker run -it myimage /ken # ls ken1 test wordpress /ken # echo $name ken
- 可以发现当前工作目录为/ken,且自动创建
- ken1是我们从docker context目录中复制过去的
- test是使用touch创建的
- wordpres压缩包已经被自动解压
- $name为变量值为ken
推荐学习:《docker视频教程》
The above is the detailed content of There are several ways to create a Docker image. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Docker and Kubernetes is that Docker is used for containerization, while Kubernetes is used for container orchestration. 1.Docker provides a consistent environment to develop, test and deploy applications, and implement isolation and resource limitation through containers. 2. Kubernetes manages containerized applications, provides automated deployment, expansion and management functions, and supports load balancing and automatic scaling. The combination of the two can improve application deployment and management efficiency.

Installing and configuring Docker on Linux requires ensuring that the system is 64-bit and kernel version 3.10 and above, use the command "sudoapt-getupdate" and install it with the command "sudoapt-getupdate" and verify it with "sudoapt-getupdate" and. Docker uses the namespace and control groups of the Linux kernel to achieve container isolation and resource limitation. The image is a read-only template, and the container can be modified. Examples of usage include running an Nginx server and creating images with custom Dockerfiles. common

The reason for using Docker is that it provides an efficient, portable and consistent environment to package, distribute, and run applications. 1) Docker is a containerized platform that allows developers to package applications and their dependencies into lightweight, portable containers. 2) It is based on Linux container technology and joint file system to ensure fast startup and efficient operation. 3) Docker supports multi-stage construction, optimizes image size and deployment speed. 4) Using Docker can simplify development and deployment processes, improve efficiency and ensure consistency across environments.

Docker's application scenarios in actual projects include simplifying deployment, managing multi-container applications and performance optimization. 1.Docker simplifies application deployment, such as using Dockerfile to deploy Node.js applications. 2. DockerCompose manages multi-container applications, such as web and database services in microservice architecture. 3. Performance optimization uses multi-stage construction to reduce the image size and monitor the container status through health checks.

Select Docker in a small project or development environment, and Kubernetes in a large project or production environment. 1.Docker is suitable for rapid iteration and testing, 2. Kubernetes provides powerful container orchestration capabilities, suitable for managing and expanding large applications.

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

Docker is a containerization tool, and Kubernetes is a container orchestration tool. 1. Docker packages applications and their dependencies into containers that can run in any Docker-enabled environment. 2. Kubernetes manages these containers, implementing automated deployment, scaling and management, and making applications run efficiently.

The purpose of Docker is to simplify application deployment and ensure that applications run consistently in different environments through containerization technology. 1) Docker solves the environmental differences problem by packaging applications and dependencies into containers. 2) Create images using Dockerfile to ensure that the application runs consistently anywhere. 3) Docker's working principle is based on images and containers, and uses the namespace and control groups of the Linux kernel to achieve isolation and resource management. 4) The basic usage includes pulling and running images from DockerHub, and the advanced usage involves managing multi-container applications using DockerCompose. 5) Common errors such as image building failure and container failure to start, you can debug through logs and network configuration. 6) Performance optimization construction


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Chinese version
Chinese version, very easy to use
