The advantages of Docker are easily overlooked by web developers. For DevOps, it was considered too technical and unnecessary.
As a web developer, learning to use Docker will help simplify the process of building, testing, and deploying web applications.
In this article, some basic Docker commands will be reviewed.
1. docker build
The first command every front-end developer should know is that docker build
this command is used to Dockerfile
build from a script Docker image
that Dockerfile
contains instructions for building an image.
docker build
The syntax of the command is as follows:
docker build -t image_name dockerfile
-t
The parameter is-tag
the abbreviated form of the parameter, allowing the mirrorimage
to specify the name and optional label (the part after the colon), the label is usually used to distinguish the version of the mirror;image_name
: image name;dockerfile
:dockerfile
file, path can be specifiedpath/dockerfile
2. docker images
To list all images built locally docker
, you can use docker images
the command.
If you run it, you should see all the mirrors listed, note docker images
the equivalent of the command docker image ls
.
3. docker run
After building the image, how to run it? Just use docker run
the command, the syntax is as follows:
docker run -p port:container_port image_name
For example, if you wanted to start a container marked as a mirror and access it on port 80, you would run:
docker run -p 80:80 devpoint:v1
This example assumes that the application is exposed on Dockerfile
the port 80
, to be mapped to a different host port, -p
a different number is specified via the first part of the parameter, the parameter is -p 主机端口:容器端口
.
If you want to run the container in the background, please add the following -d
parameters:
docker run -d -p 80:80 devpoint:v1
This command will return the container ID and return control of the terminal window to the user.
When running a container, Docker will assign it a random name. If you don't want a random name, you can --name
specify the desired name through parameters.
For example, you can name the container running devpoint
the image devpoint-website
as follows:
docker run -d -p 80:80 --name devpoint-website devpoint:v1
4. docker ps
To list all currently running containers, you can run:
docker ps
After running, you can see that all running containers are listed, including container ID, name and image name.
5. docker start/stop
To start or stop a container, the syntax is:
docker start|stop container_name
or
docker start|stop container_id
Once the container is stopped, it will no longer appear in the list of running containers, but it
docker ps
cannot be seen by the user. To list the non-running containers, you can use the commanddocker ps -a
.
6. docker logs
Using docker logs
the command, you can view the logs of a running container. The syntax is:
docker logs container_name
This command helps to debug any startup issues or exceptions thrown in the container.
7. docker exec
Another useful command is docker exec
. This command can enter the running container and run the command, the syntax is:
docker exec -it container_name command_to_run
For example, if you want to devpoint-website
open one in a container shell
, run:
docker exec -it devpoint-website sh
To exit shell
, enter the command exit
.
8. docker login
After building a mirror image for the application and testing the successful operation, if you need to share it with others, you need to register the mirror warehouse.
Docker Hub is a public mirror repository, and anyone can access and download the images stored in it, unless the user makes the repository private.
To log into Docker Hub (assuming you have an account), you can use the following command:
docker login -u username
9. docker push
To push an image to Docker Hub, you need to use docker push
the command. The syntax is:
docker push username/image_name
Similar to how GitHub is used.
The standard conventions when building Docker images are:
docker build -t username/image_name:tag_name
For example, to build an image for my own Docker Hub account, run the following command:
docker build — t chaoy2010/vue2:v1
Docker Hub will automatically vite2
tag the image named as v1
.
10. docker pull
Once logged into Docker Hub , you can docker pull
pull an existing image with the following command:
docker pull chaoy2010/vue2:v1
Summarize
Learning these basic Docker commands can increase developer productivity.