Docker command

发布时间 2023-10-09 22:16:01作者: ashet

reference  https://docs.docker.com/get-started/overview/

Images

An image is a read-only template with instructions for creating a Docker container. // image是一个只读模板,包含创建 Docker container 的说明。

无论是Java程序还是C++或其他程序,都可以按照Dockerfile规范统一打包成Image,然后由Docker创建container运行。

Containers

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. 

 

Images

As you see,we need pull image to our Docker Host(Docker宿主机,即你使用的computer)from Docker Hub,  then use command create a container by client.

docker pull nginx

docker images // you can see nginx's image

 

You can also create an image by Dockerfile, like this

upload your-app.jar to current folder

vim Dockerfile

FROM openjdk:17-jdk

# 设置容器的工作目录
WORKDIR /app

# 复制 JAR 文件到容器中
COPY you-app.jar .

EXPOSE 8080

CMD ["java", "-jar", "you-app.jar"]

 Save & Exit

docker build -t you-app . // then Docker will create an image by your-app & Dockerfile of the current folder

docker images // you can see you-app's image

  

Containers

运行容器:docker run (-it) --name alias name -p hostPrt:containerPort # -it 以交互模式启动容器并返回终端

列出所有的容器:docker ps -a

退出容器:exist (退出并shutdown容器) 、ctrl + p + q (退出,但不shutdown)

重启容器:docker restart image_id/name

停止容器:docker stop image_id/name

注销容器:docker kill image_id/name

删除已停止容器:docker rm image_id

守护线程启动容器:docker run -d name # Docker容器后台运行,不阻塞控制台的继续使用