Home  >  Article  >  Operation and Maintenance  >  How to build your own docker container

How to build your own docker container

PHPz
PHPzOriginal
2023-04-04 10:39:344197browse

With the popularity of cloud computing and microservices, Docker has become the development and deployment standard for a new generation of enterprise-level applications. And self-built Docker containers have become the choice of more and more developers. Let's discuss how to build your own Docker container.

1. Introduction to Docker

Docker is an open source application container engine that can easily package applications into containers to run in a variety of environments. Docker fundamentally changes the way applications are delivered, making them more lightweight, portable, and deployable. Advantages of Docker include:

  1. Lightweight

Docker containers are lightweight and efficient because they can run multiple virtualized containers on the same hardware middle. This makes Docker containers ideal for running in distributed environments.

  1. Portability

Docker containers can be easily built and tested on your local machine and then deployed directly into a production environment. This avoids configuration differences in different environments and the problem of "it can't run here" caused by differences in environments.

  1. Rich ecosystem

The Docker ecosystem includes a large number of Docker images (which can be understood as templates for Docker containers), which can save the time and time required to build applications. energy.

2. Install Docker

Before building your own Docker container, you need to install Docker first. Docker supports a variety of operating systems, including Windows, Linux, and Mac OS X. In the Ubuntu system, you can install Docker through the following command:

sudo apt-get update
sudo apt-get install docker.io

After the installation is completed, use the following command to test whether Docker is installed correctly:

sudo docker run hello-world

3. Build the Docker image

  1. Preparing Dockerfile

Dockerfile is a script used to build a Docker image. It contains starting from the base image and gradually adding instructions to modify the application and configure the environment. For example, here is a Dockerfile used to build a simple Node.js application:

# 使用Node.js作为基础镜像
FROM node

# 复制应用程序文件
COPY app.js /app/

# 切换工作目录
WORKDIR /app

# 安装依赖
RUN npm install

# 设置默认环境变量
ENV PORT 3000

# 暴露3000端口
EXPOSE 3000

# 启动应用程序
CMD ["npm", "start"]

Explanation of Dockerfile:

  • Base Image: Use the FROM directive to specify the required base Mirror, use node as the base image;
  • Copy files: use the COPY instruction to copy application files to the container;
  • Command execution: use the RUN instruction to execute commands in the container to install dependencies;
  • Set environment variables: Use the ENV instruction to set the environment variables, that is, the variable values ​​when running in the Docker container;
  • Expose port numbers: Use the EXPOSE instruction to specify which ports the container will expose;
  • Startup command: Use the CMD command to specify the command to be executed when the container starts.
  1. Building an image

The process of building a Docker image is very simple:

  • Create a new folder or enter an existing file folder, put the Dockerfile into it;
  • Open the terminal and enter the folder where the Dockerfile is located;
  • Run the commanddocker build -t imagename, where imagename is the new Docker Image name.

For example, the following is a sample command for a node application to build a Docker image:

cd myapp
docker build -t myapp .

Where, myapp is the custom image name.

4. Start the container based on the Docker image

Once the Docker image is created, you can create one or more containers based on it to run the application. The command format to start a Docker container is as follows:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Among them, OPTIONS represents the startup options of the container, COMMAND represents the command to be executed, and ARG represents any parameters to be passed to the command. For example:

docker run -d -p 8080:3000 myapp

Where:

  • -d: Indicates that the container will run in the background;
  • -p: Indicates that the port of the container is mapped to the host port;
  • 8080: is the host port;
  • 3000: is the port exposed by the container;
  • myapp: is the referenced custom image name.

Finally, you can view all running Docker containers through the command docker ps, and you can stop the running Docker container through the command docker stop CONTAINER_ID.

5. Conclusion

Self-built Docker containers can easily deploy applications and improve the portability of applications. In this article, we introduce the process of installing Docker, building a Docker image, and starting a container based on the Docker image. As you build your own Docker containers, combine these steps to start using Docker containers in your own environment.

The above is the detailed content of How to build your own docker container. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn