Contents
Roadmap info from roadmap website
Running Containers with docker run
In this section, weโll discuss the docker run
command, which enables you to run Docker containers. The docker run
command creates a new container from the specified image and starts it.
The basic syntax for the docker run
command is as follows:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
-
OPTIONS
: These are command-line flags that can be used to adjust the containerโs settings, like memory constraints, ports, environment variables, etc. -
IMAGE
: The Docker image that the container will run. This can be an image from Docker Hub or your own image that is stored locally. -
COMMAND
: This is the command that will be executed inside the container when it starts. If not specified, the default entrypoint of the image will be used. -
ARG...
: These are optional arguments that can be passed to the command being executed.
Commonly used Options
Here are some commonly used options with docker run
:
-
--name
: Assign a name to the container, making it easier to identify and manage. -
-p, --publish
: Publish a containerโs port(s) to the host. This is useful when you want to access the services running inside the container from outside the container. -
-e, --env
: Set environment variables inside the container. You can use this option multiple times to set multiple variables. -
-d, --detach
: Run the container in detached mode, running the container in the background and not showing logs in the console. -
-v, --volume
: Bind mount a volume from the host to the container. This is helpful in persisting data generated by the container or sharing files between host and container.
Examples
Here are some sample commands to help you understand how to use docker run
:
- Run an interactive session of an Ubuntu container:
docker run -it --name=my-ubuntu ubuntu
- Run an Nginx web server and publish the port 80 on the host:
docker run -d --name=my-nginx -p 80:80 nginx
- Run a MySQL container with custom environment variables for configuring the database:
docker run -d --name=my-mysql -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=mydb -p 3306:3306 mysql
- Run a container with a bind-mounted volume:
docker run -d --name=my-data -v /path/on/host:/path/in/container some-image