Home Introduction to Docker and Basic Commands (II)(English)
Post
Cancel

Introduction to Docker and Basic Commands (II)(English)

When you’re building and running containers with Docker, you’ll definitely use the docker build and docker run commands.

Here are the basic usages of these two commands (Once you’ve learned these, you can confidently say you know how to use Docker)

Docker Build Command

The docker build command is used to create a Docker image from a Dockerfile.

Here’s the basic command format

1
2
3
4
5
6
7
8
9
docker build -t <image_name>:<tag> <path/URL>
-t: Specifies the name and tag of the image.
<image_name>: Gives the image a name.
<tag>: Tags the image, usually with a version number.
<path/URL>: The path or URL where the Dockerfile is located.

Example:

docker build -t myapp:1.0 .

Docker Run Command

The docker run command is used to execute the created Docker image and create a container instance.

Here’s the basic command format

1
2
3
4
5
6
7
8
9
docker run [options] <image_name>:<tag>
[options]: Various parameters can be used, such as -d (to run in the background), -p (to specify port mapping), etc.
<image_name>: The name of the Docker image created earlier.
<tag>: The tag of the Docker image created earlier.

Example:


docker run -d -p 8080:80 myapp:1.0

In this example, the container will run in the background, and port 8080 on the host will be mapped to port 80 on the container.

You can then check in your Docker Desktop to see that both the image and container have been created.

☝ツ☝

This post is licensed under CC BY 4.0 by the author.

👈 ツ 👍

使用 VS Code + docker 快速建立開發環境(中文)

Docker 介紹與基本指令 (II)(中文)