Understanding Docker Commands: Run, Start, and Exec
When the purpose is to execute a container, there are several instructions that look similar at first glance, but their actual functions are different.
Today we will briefly distinguish the operational differences between docker run, docker start, and docker exec, helping you understand their functionalities and best use cases.
Docker run: Creating and Starting New Containers
The docker run
command is the starting point for creating and launching a new container from a Docker image.
It performs a series of actions: pulling the image from a registry if it’s not available locally, creating a new container, and starting it. The command offers a plant of options to configure the container at creation, including port mappings (-p
), volume bindings (-v
), environment variables (-e
), and more.
Example Command
docker run -d \
--name my-mysql \
-p 3306:3306 \
-v /my/local/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
mysql:latest
-d
: Runs the container in detached mode, meaning the container runs in the background.--name my-mysql
: Names the containermy-mysql
. This name…