• About Us
  • Privacy & Policy
HowTo's
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
HowTo's
No Result
View All Result
Home Docker

How to Create and Run Docker Containers from an Image

July 8, 2024
in Docker, Docker Commands Examples, Docker Commands Tutorial, Docker Tutorial
A A
0
11
SHARES
101
VIEWS
Share on FacebookShare on Twitter

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

Tags: DockerDocker Commands ExamplesDocker Commands TutorialDocker Tutorial
Previous Post

How to Manage Kubernetes Plugins

Next Post

Executing Commands Inside Docker Containers

Related You may like!

howto

Managing Swarm Services

August 21, 2024
howto

Managing Docker Volumes

August 21, 2024

Managing Docker Networks

August 21, 2024

Managing Swarm Secrets

July 8, 2024

Managing Swarm Nodes

July 8, 2024

Managing Swarm Configurations

July 8, 2024
Next Post
howto

Executing Commands Inside Docker Containers

howto

Listing Docker Containers

howto

Building Docker Images from a Dockerfile

Discussion about this post

Latest Updated

howto

How to Use -iname for Case-Insensitive Filename Searches in find

August 21, 2024
howto

Search for Files with Case-Insensitive Pattern Matching Using -ilname in find

August 21, 2024
howto

Find Files by Group Name with -group in find Command

August 21, 2024
howto

Locate Files by Group ID Using -gid in find Command

August 21, 2024
howto

How to Search for Filesystems with -fstype in find Command

August 21, 2024

Trending in Week

  • howto

    Using BTRFS Subvolume for User Home Directory in Linux

    22 shares
    Share 9 Tweet 6
  • Downloading Docker Images from a Registry

    13 shares
    Share 5 Tweet 3
  • Configuring SSL Connection Mode in mysqldump

    17 shares
    Share 7 Tweet 4
  • Omit Tablespace Information in mysqldump Output

    13 shares
    Share 5 Tweet 3
  • Setting MySQL Dump Compatibility Mode

    18 shares
    Share 7 Tweet 5
  • Setting Network Buffer Length in mysqldump

    13 shares
    Share 5 Tweet 3
  • Logging out from Docker Registries

    13 shares
    Share 5 Tweet 3
  • Scheduling Nodes in Kubernetes with kubectl uncordon

    12 shares
    Share 5 Tweet 3
  • Managing Default User Creation Settings in Linux

    15 shares
    Share 6 Tweet 4
  • Using Extended INSERT Syntax in mysqldump

    12 shares
    Share 5 Tweet 3
  • About Us
  • Privacy & Policy

© 2024 All Rights Reserved. Howto.swebtools.com.

No Result
View All Result

© 2024 All Rights Reserved. Howto.swebtools.com.