This post will cover topic related to ‘How to Create and Run Docker Containers from an Image’ with multiple docker command examples and different scenerios. So this will help you to understand the command docker and options available in it. Also this post will explain you how to use docker command.
When working with Docker, the docker run
command is essential for creating and running containers based on Docker images. This command allows you to specify various options and parameters to customize how your container behaves.
Let’s explore several examples to understand how to use docker run
effectively:
Example 1: Running a basic container from an image:
docker run hello-world
This command fetches the hello-world
image from Docker Hub and runs it. The output should display a welcome message confirming that Docker is properly installed and running.
Verification: Look for the message “Hello from Docker!” in the output, indicating the successful execution of the container.
Example 2: Running a container in detached mode:
docker run -d nginx
Here, nginx
is the name of the Docker image. The -d
flag tells Docker to run the container in the background (detached mode), allowing you to continue using the terminal.
Verification: Use docker ps
to list all running containers and verify that the nginx
container is present.
Example 3: Mapping ports between the host and the container:
docker run -p 8080:80 nginx
This command maps port 8080
on the host to port 80
on the nginx
container. Now, you can access the nginx web server from your host machine using http://localhost:8080
.
Verification: Open a web browser and navigate to http://localhost:8080
to see the default nginx welcome page.
Example 4: Mounting volumes to persist data:
docker run -v /path/on/host:/path/in/container nginx
With this command, you mount a directory /path/on/host
from your host system into the /path/in/container
directory within the nginx
container. This allows data to persist beyond the container’s lifecycle.
Verification: Create files in /path/on/host
on your host system and verify their presence in /path/in/container
within the running nginx container.
Example 5: Setting environment variables:
docker run -e MYSQL_ROOT_PASSWORD=password mysql
This command sets the environment variable MYSQL_ROOT_PASSWORD
with the value password
for the mysql
container, allowing configuration customization.
Verification: Connect to the MySQL instance and authenticate using the specified root password to confirm its effectiveness.
Example 6: Limiting container resources:
docker run --memory=512m --cpu-shares=2 nginx
Here, Docker restricts the nginx
container to use a maximum of 512MB of memory and shares CPU resources accordingly.
Verification: Monitor the container’s resource usage using docker stats
or similar tools to ensure resource limitations are enforced.
Example 7: Running a container with a specific name:
docker run --name my-nginx nginx
This assigns the name my-nginx
to the nginx
container, making it easier to identify and manage.
Verification: Use docker ps -a
to list all containers (including stopped ones) and confirm that my-nginx
appears with the status.
Example 8: Running a container with a specific network:
docker network create my-network
docker run --network my-network nginx
First, create a custom network my-network
using docker network create
, then run the nginx
container within that network.
Verification: Use docker network inspect my-network
to verify that the nginx
container is connected to my-network
.
Example 9: Running a container with specific runtime constraints:
docker run --runtime=nvidia tensorflow/tensorflow:latest-gpu
This command specifies the NVIDIA runtime for running the tensorflow
container with GPU support.
Verification: Check the container logs or use nvidia-smi
to ensure GPU resources are properly utilized.
Example 10: Interactive mode with terminal access:
docker run -it ubuntu:latest /bin/bash
This starts an ubuntu
container in interactive mode with a terminal (/bin/bash
) prompt.
Verification: Interact with the container’s terminal, execute commands like ls
or pwd
to confirm the interactive session is operational.
Also check similar articles.
How to Manage Kubernetes Plugins
How to Manage Kubernetes Networks
How to Manage Kubernetes Image Manifests
How to Manage Kubernetes Images
How to Manage Kubernetes Contexts
Discussion about this post