...

/

Docker CLI: Docker Containers

Docker CLI: Docker Containers

Learn useful Docker CLI commands related to Docker containers.

Launch a container from an image

To launch the container from an image, docker run or docker container run can be used. The command docker run was available before Docker version 1.13. Version 1.13 introduced docker container command which can be used to run, list, start, etc., the containers. This allows more effective grouping of commands as compared to docker run.

So to start an image as a container instance, we can use the both options as follows:

docker run [options] <image> [command] [args...]
docker container run [options] <image> [command] [args...]

The following command launches a container named mysql (--name), from the latest mysql image in interactive mode (-it shows a terminal log), that exposes port 3306 to the host (-p), stores data in a Docker volume named mysqldata (--mount), sets the root user password to mysecret (-e environment variable), and deletes the container once it has stopped (-rm):

docker run \
  -it --rm --name mysql \
  -p 3306:3306 \
  --mount "src=mysqldata,target=/var/lib/mysql" \
  -e MYSQL_ROOT_PASSWORD=mysecret \
  mysql

Click heredockerImageOptions for more useful options.

Access this course and 1400+ top-rated courses and projects.