Explain Docker run with options
The docker run command is used to create and start a new container based on a Docker image. It has several options that can be used to customize the behavior of the container. Here's an example of how to use the docker run command with multiple options:
Let's break down each option:
- -d
option: This option runs the container in detached mode, which means the
container runs in the background and does not hold your terminal window
open.
- --name
option: This option sets a custom name for the container. In this case,
the container will be named mycontainer.
- -p
option: This option maps a network port on the host to a container port.
In this case, port 8080 on the host is mapped to port 80 in the container.
- -v
option: This option mounts a local folder on the host to a folder in the
container. In this case, the folder /my/local/folder on the host is
mounted to the folder /container/folder in the container.
- myimage:tag
argument: This specifies the name and tag of the Docker image to be used
for the container. In this case, the image is named myimage and has
the tag tag.
Together, these options and
arguments will create a new container named mycontainer from the image myimage:tag,
run it in detached mode, map port 8080 on the host to port 80 in the container,
and mount the local folder /my/local/folder to the folder /container/folder
in the container.
Here are a few more real-time examples of the docker run
command with multiple options:
- Running
an Apache web server container and mapping the host port to the container
port:
docker run -d --name my-apache-container -p
8080:80 httpd:2.4
This command will run a new container using the official httpd:2.4
image, map port 8080 on the host to port 80 in the container, and give it the
name my-apache-container. The container will run in detached mode,
meaning it will run in the background and won't occupy your terminal window.
- Running
a container with a mounted volume:
docker run -d --name my-db-container -v
/my/local/db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword mysql:8.0
This command will run a new container using the official mysql:8.0
image, mount the local folder /my/local/db to the folder /var/lib/mysql
in the container, and set the environment variable MYSQL_ROOT_PASSWORD
to mysecretpassword. The container will run in detached mode and will be
named my-db-container.
- Running
a container with environment variables:
docker
run -d --name my-app-container -e DB_HOST=my-db-host -e DB_PORT=3306 -e
DB_USER=myuser -e DB_PASSWORD=mypassword my-app:latest
This command will run a new container using the my-app:latest
image and set several environment variables. The container will be named my-app-container,
and it will run in detached mode. The environment variables DB_HOST, DB_PORT,
DB_USER, and DB_PASSWORD will be set to the values my-db-host,
3306, myuser, and mypassword, respectively.
- Running
a container with a custom command:
docker
run -d --name my-container my-image:tag sh -c 'echo "Hello, world!"
&& sleep 3600'
This command will run a new container using the my-image:tag
image, give it the name my-container, and run a custom command in the
container's shell. The command will echo the string "Hello, world!"
and then sleep for 3600 seconds (1 hour). The container will run in detached
mode.
- Running
a container with a specific hostname:
docker
run -d --name my-container --hostname my-hostname my-image:tag
This command will run a new container using the my-image:tag
image, give it the name my-container, and set its hostname to my-hostname.
The container will run in detached mode.
- Running
a container with a specific user:
docker
run -d --name my-container --user 1000 my-image:tag
This command will run a new container using the my-image:tag
image, give it the name my-container, and set its user ID to 1000. The
container will run in detached mode with the specified user.
- Running
a container with a specific network:
docker
run -d --name my-container --network my-network my-image:tag
This command will run a new container using the my-image:tag
image, give it the name my-container, and attach it to the my-network
network. The container will run in detached mode on the specified network.
Thanks For Reading..!
Comments
Post a Comment