Home > Article > Operation and Maintenance > Let’s talk about how to create a Docker image
Docker is a popular containerization platform that allows applications to run in a lightweight, portable, and deployable manner. Docker images are the core concept in Docker, they are the basis of containers created from the file system. In this article, we will discuss how to create a Docker image.
Docker images are built from one or more Dockerfiles. A Dockerfile is a text file that contains a set of instructions that define the process of generating a Docker image. These instructions describe how to make the image contain specified software and configuration. The following is an example of a Dockerfile:
# 基于 Ubuntu 18.04 镜像 FROM ubuntu:18.04 # 设置工作目录 WORKDIR /app # 复制应用程序文件到工作目录 COPY . . # 安装应用程序依赖 RUN apt-get update && apt-get install -y \ python3 \ python3-pip \ && pip3 install -r requirements.txt # 暴露应用程序端口 EXPOSE 5000 # 启动应用程序 CMD ["python3", "app.py"]
In this Dockerfile, we first specify the operating system image used: ubuntu:18.04. Then, we set up the working directory and copied all the files in the current directory to the Docker image. Next, we used the apt-get command to install Python and Python dependencies, and used the pip3 command to install Python dependencies. After that, we exposed the application port so that it is accessible outside the Docker container. Finally, we launch the application using the CMD command.
The next step is to create a Docker image:
$ docker build -t my_image .
In this command, the -t
parameter specifies the label for the created Docker image, and .
represents searching for Dockerfile in the current directory.
$ docker run -p 5000:5000 my_image
In this command, the -p
parameter maps the port of the Docker container to the port of the host, and my_image
is the name of the running Docker image.
To publish the Docker image to Docker Hub, you need to use the following command:
$ docker login $ docker tag my_image <your-docker-id>/my_image $ docker push <your-docker-id>/my_image
docker login
The command is used to log in to Docker Hub. The docker tag
command is used to tag the Docker image to be published to Docker Hub. The docker push
command is used to publish the Docker image to Docker Hub.
In short, Docker image is the core concept in Docker, which is built from Dockerfile. The steps to create a Docker image include creating a Dockerfile, building the Docker image, running the Docker image, and publishing the Docker image to Docker Hub. They are all very simple and easy to understand. I hope this article can help you better understand how to create a Docker image.
The above is the detailed content of Let’s talk about how to create a Docker image. For more information, please follow other related articles on the PHP Chinese website!