How to use the command 'docker run' (with examples)
The docker run
command is used to run a command in a new Docker container. It allows you to create and start a new container from a Docker image, execute a command within that container, and then stop the container. This command is one of the most fundamental commands in Docker as it allows you to deploy and run applications in isolated containers.
Use case 1: Run command in a new container from a tagged image
Code:
docker run image:tag command
Motivation: This use case is useful when you want to run a specific command within a Docker container created from a tagged image. The image and tag determine the exact version of the container you want to use.
Explanation: The docker run
command is followed by the name of the Docker image and its tag. The command
argument specifies the command you want to run inside the container.
Example output:
docker run ubuntu:latest echo "Hello, World!"
The output will be:
Hello, World!
Use case 2: Run command in a new container in background and display its ID
Code:
docker run --detach image command
Motivation: This use case is helpful when you want to run a command in the background and get the container ID for further reference or management.
Explanation: The docker run
command is followed by the --detach
flag, which runs the container in the background and returns the container ID. The image
argument specifies the Docker image, and the command
argument defines the command to be executed within the container.
Example output:
docker run --detach nginx:latest
The output will be the container ID (e.g., 4567f8901234
).
Use case 3: Run command in a one-off container in interactive mode and pseudo-TTY
Code:
docker run --rm --interactive --tty image command
Motivation: This use case is beneficial when you want to run a command within a temporary container in interactive mode, typically for troubleshooting or debugging purposes.
Explanation: The docker run
command is followed by the --rm
flag, which automatically removes the container once it is stopped. The --interactive
and --tty
flags allocate an interactive pseudo-TTY, enabling you to interact directly with the command within the container. The image
argument specifies the Docker image, and the command
argument defines the command to be executed.
Example output:
docker run --rm -it alpine:latest sh
The output will be an interactive shell session within the Alpine container.
Use case 4: Run command in a new container with passed environment variables
Code:
docker run --env 'variable=value' --env variable image command
Motivation: This use case is useful when you need to set environment variables in the container to configure the command being executed or control its behavior.
Explanation: The docker run
command is followed by one or more --env
flags, which allow you to pass environment variables to the container. Each --env
flag consists of a variable and its corresponding value. The image
argument specifies the Docker image, and the command
argument defines the command to be executed.
Example output:
docker run --env 'DB_HOST=localhost' --env 'DB_PORT=5432' postgres:latest psql
The output will be a PostgreSQL command line interface running with the specified environment variables for connecting to the database.
Use case 5: Run command in a new container with bind-mounted volumes
Code:
docker run --volume /path/to/host_path:/path/to/container_path image command
Motivation: This use case is beneficial when you want to share files or directories between the host system and the container, allowing for data persistence and/or live code updates.
Explanation: The docker run
command is followed by the --volume
flag, which binds a directory or file from the host system to a directory or file inside the container. The source path on the host is specified before the colon (:
), and the destination path in the container is specified after the colon. The image
argument specifies the Docker image, and the command
argument defines the command to be executed.
Example output:
docker run --volume /var/logs/nginx:/var/log/nginx nginx:latest
The output will be the Nginx web server running with the logs directory bind-mounted from the host system.
Use case 6: Run command in a new container with published ports
Code:
docker run --publish host_port:container_port image command
Motivation: This use case is useful when you want to expose a container’s network ports to the host system, allowing external access to services running inside the container.
Explanation: The docker run
command is followed by the --publish
flag, which maps a port from the host system to a port inside the container. The source port on the host is specified before the colon (:
), and the destination port in the container is specified after the colon. The image
argument specifies the Docker image, and the command
argument defines the command to be executed.
Example output:
docker run --publish 8080:80 nginx:latest
The output will be the Nginx web server running with port 80 inside the container mapped to port 8080 on the host system.
Use case 7: Run command in a new container overwriting the entrypoint of the image
Code:
docker run --entrypoint command image
Motivation: This use case is helpful when you want to override the default entrypoint specified in the Docker image and run a different command instead.
Explanation: The docker run
command is followed by the --entrypoint
flag, which allows you to specify a different entrypoint command for the container. The image
argument specifies the Docker image, and the command
argument defines the command to be executed.
Example output:
docker run --entrypoint sh alpine:latest
The output will be an interactive shell session within the Alpine container, overriding the default entrypoint.
Use case 8: Run command in a new container connecting it to a network
Code:
docker run --network network image
Motivation: This use case is beneficial when you want to connect the container to a specific Docker network, allowing it to communicate with other containers in that network.
Explanation: The docker run
command is followed by the --network
flag, which specifies the name of the network to which the container should be connected. The image
argument specifies the Docker image.
Example output:
docker run --network my-network nginx:latest
The output will be an Nginx web server container connected to the “my-network” Docker network.
Conclusion:
The docker run
command is a versatile and powerful command in Docker that allows you to create, start, and run commands in new containers. By understanding and utilizing the various options and arguments of this command, you can efficiently manage your containerized applications and leverage the benefits of Docker’s containerization technology.