Introduction to Docker

Build the mirror

docker build -t <image-name> .

  • -t Specifies the name of the target mirror to be created
  • . : indicates the directory where Dockerfile resides. You can specify the absolute path of Dockerfile
  • Setting the image label:docker build -t image-name:tag

Run the application

docker run --name <container-name> -d -p 8080:80 <image-name> /bin/echo "Hello world"

  • Docker run: Runs the application
  • <image-name>: Specifies the image to run. Docker first looks up the image from the localhost to see if it exists. If not, Docker downloads the public image from the image repository Docker Hub
  • /bin/echo "Hello world":Commands executed in a started container
  • - the nameSpecify the container name
  • -dThe background
  • -pSpecifying port Mapping

Run interactive containers

Example: docker run -it

/bin/bash

  • T: Specifies a dummy terminal or terminal in the new container.
  • I: Allows interaction with standard input (STDIN) within the container.
  • Exit the container by running the exit command or using CTRL+D

Into the container

Docker exec -it Container ID /bin/bash

Stop the container

Docker stop < container ID >

View the status of all containers

docker ps -a

Start a stopped container

Docker start < container ID>

Exporting a Container Snapshot

Docker export Container ID > name.tar

Import the container

docker import url

Remove the container

Docker rm -f Container ID or docker RM container name

Port mapping

  • -p random mapping
  • Specify port mapping: -p 80:10086
  • View the container port mapping:Docker port Container ID

1### View all mirrors

docker images

Remove the mirror

Docker RmI image name

Use Docker to package local Web projects and publish them to the server

1. Package the local project as a Docker image

docker build -t <image-name> .
Copy the code

2. Push the packaged image to the image container service

To push docker image to Ali Cloud image server, you need to create a space and a warehouse in Ali cloud image service first

# Log in to Aliyun Container service
docker login --username=[username] registry.cn-hangzhou.aliyuncs.com
# tag the imageDocker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/[namespace]/[repository name]:# Push to Ali Cloud container warehouseDocker push registry.cn-hangzhou.aliyuncs.com/[namespace]/[repository name]:[mirror version number]Copy the code

3. Pull ali Cloud container image and run it

The server pulls ali Cloud container image, the premise of pulling is to log in first, as shown above

# pull the image you just uploadedDocker pull registry.cn-hangzhou.aliyuncs.com/[namespace]/[repository name]:[mirror version number]Create and run the containerDocker run --name blog -d -p 55:80 registry.cn-hangzhou.aliyuncs.com/ namespace]Copy the code

Open server IP: port number and you can see the content, perfect