samwellwang

samwellwang

coder
twitter

Using Docker

Now software development is using containerization technology, and Docker is the most popular containerization platform. This article is to teach you how to use Docker, including installation, startup, viewing, entering, pulling, deleting, and starting containers, as well as various parameters and commands. Just like at work, you need to know how to use various tools, such as hammers, drills, and screwdrivers, to get things done. Now Docker is an essential tool in your toolbox, using it to build development environments and deploy applications, it's so easy!

Installation of Docker#

The installation of Docker is very simple, just download the corresponding installation package from the official website, and then follow the prompts to install it. After the installation is complete, you can verify whether Docker is installed successfully by using the following command:

docker version

If the version information of Docker can be output, it means that Docker has been successfully installed.

Startup of Docker#

After the installation is complete, you can start Docker by using the following command:

systemctl start docker

If you want Docker to start automatically when the system starts, you can use the following command:

systemctl enable docker

Viewing image information#

Before using Docker, you need to understand the concept of Docker images. Docker image is a lightweight, executable, standalone software package that contains all the necessary content to run an application, including code, runtime environment, libraries, environment variables, and configuration files, etc.

To view the local Docker image list, you can use the following command:

docker images

This command will list all local images, including image ID, image name, image tag, and image size, etc.

Entering an image#

Sometimes you need to enter a specific image for operation, you can use the following command:

docker exec -it image name /bin/bash

Where the -it parameter indicates entering the container interactively and opening a terminal. /bin/bash indicates that the default command executed after entering the container is the Bash terminal.

Pulling images#

If the required Docker image is not available locally, you can pull it using the following command:

docker pull image name

This command will download the specified image from Docker Hub and save it locally.

Deleting containers#

When you no longer need a container, you can use the following command to delete it:

docker rm container ID or container name

Starting containers#

To start a container, you can use the following command:

docker run -d -p host port:container port image name

Where the -d parameter indicates running the container in the background. The -p parameter indicates mapping the host port to the container port. For example, mapping the host's 8080 port to the container's 80 port:

docker run -d -p 8080:80 nginx

Starting a new image with an existing image#

Sometimes you need to make modifications on an existing image and generate a new image. You can use the following steps:

  1. Start a container and enter it:

    docker exec -it image name /bin/bash
    
  2. Make modifications in the container and save the changes.

  3. Exit the container and commit the changes:

    docker commit container ID new image name
    
  4. View the newly generated image:

    docker images
    

Various parameters when starting#

When starting a container, you can specify various parameters to meet specific requirements. Here are some commonly used parameters:

  • -d: Run the container in the background.
  • -p: Map the host port to the container port.
  • -v: Mount the host directory to the container directory.
  • --name: Specify a name for the container.
  • --restart: Specify the restart policy for the container after it stops.

For example, when starting a MySQL container, you can use the following command:

docker run -d --name mysql -p 3306:3306 -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

This command specifies the container name as mysql, maps the host's 3306 port to the container's 3306 port, and mounts the host directory /root/mysql/data to the container directory /var/lib/mysql. It also specifies MySQL's root user password and character set parameters.

In addition to the commands mentioned above, Docker also provides many other related commands, such as:

  • docker ps: View running containers.
  • docker stop: Stop a running container.
  • docker logs: View container logs.
  • docker exec: Execute commands in a running container.
  • docker build: Build a new image based on the Dockerfile.

The above are just some commonly used commands. Docker also provides many other powerful features and commands, which can be learned and used according to actual needs.

Summary#

This article introduces the basic usage of Docker and other related commands. Docker is a very powerful and flexible tool that can help developers to be more efficient in software development and deployment.

Most of the content in this article comes from Docker - 从入门到实践 and my own work records.

PS#

Since using Docker, I have learned that many components can be directly installed using Docker, such as MySQL, Redis, Nginx, etc. This way, you don't have to worry about version issues or various problems during the installation process. Just pull the image and start it, it's very convenient.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.