• 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 a New Container in Kubernetes?

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

In this post, we will cover the topic ‘How to Create and Run a New Container in Kubernetes?’ with multiple docker command examples and different scenerios wherever it is applicable. So this will help to understand the options available in docker command and how to use those commands and its option.

In Docker, the run command is essential for creating and launching containers based on specified images. When working within a Kubernetes environment, managing containers efficiently is crucial for scalable and resilient applications. Let’s explore how to create and run a new container in Kubernetes using Docker’s run command.

1. Run a Basic Container:
To start, use the following command to run a simple container:

docker run hello-world

This command pulls the hello-world image from Docker Hub if it’s not already present on the local machine, and then runs a container based on that image. The output should display a message confirming the successful execution of the container.

Verification Step: To verify that the hello-world container executed successfully, check the output in your terminal or Docker logs for a message indicating the container ran without issues.

2. Run a Container in Detached Mode:
When you need a container to run in the background, use the -d flag:

docker run -d nginx

This command starts a new container based on the nginx image in detached mode, meaning it runs in the background. The output will typically be the container ID once it’s started.

Verification Step: Check the running containers with docker ps and confirm that the nginx container is listed.

3. Name the Container:
Assign a specific name to a container using the --name flag:

docker run --name my-nginx nginx

This command creates a container named my-nginx using the nginx image. The output will show the container ID if successfully started.

Verification Step: Use docker ps -a to list all containers, and verify that my-nginx appears in the list with its status.

4. Map Ports:
To expose and map ports from the container to the host machine, use the -p flag:

docker run -p 8080:80 nginx

This command maps port 8080 on the host to port 80 inside the nginx container. The output will typically show the container ID once it’s started.

Verification Step: Access http://localhost:8080 in a web browser to ensure that the NGINX welcome page or application is accessible, confirming that port mapping is working.

5. Set Environment Variables:
Define environment variables for the container with the -e flag:

docker run -e MYSQL_ROOT_PASSWORD=password mysql

This command sets the MYSQL_ROOT_PASSWORD environment variable to password for the MySQL container. The output will show the container ID once it’s started.

Verification Step: Verify the environment variable is correctly set inside the container by connecting to MySQL and checking the password configuration.

6. Mount Volumes:
Mount host directories or volumes into the container using the -v flag:

docker run -v /path/on/host:/path/in/container nginx

This command mounts the /path/on/host directory on the host to /path/in/container inside the nginx container. The output will show the container ID once it’s started.

Verification Step: Create a file in /path/on/host and verify its presence and contents inside the container at /path/in/container.

7. Attach to Container:
To interactively attach to a running container’s console, use the -it flags together:

docker run -it ubuntu

This command starts an interactive shell session inside a new Ubuntu container. The output will show a terminal prompt inside the container.

Verification Step: Execute commands inside the interactive shell session and verify their outputs to confirm interactive access to the container.

8. Run with Resource Limits:
Restrict container resources like CPU and memory using the --cpu and --memory flags:

docker run --name limited-nginx --cpu=0.5 --memory=512m nginx

This command starts a new nginx container named limited-nginx with limited CPU to 50% and memory to 512 MB. The output will show the container ID once it’s started.

Verification Step: Monitor the container’s resource usage using Docker commands or system monitoring tools to ensure the limits are enforced.

9. Run with Restart Policies:
Specify container restart policies using the --restart flag:

docker run --restart=always nginx

This command configures the nginx container to always restart if it stops unexpectedly. The output will show the container ID once it’s started.

Verification Step: Stop the container manually and verify that Docker automatically restarts it based on the specified restart policy.

10. Run with Health Checks:
Implement health checks for containers using the --health-cmd and --health-interval flags:

docker run --health-cmd="curl -f http://localhost/health || exit 1" --health-interval=5s nginx

This command configures a health check for the nginx container, checking the health endpoint every 5 seconds. The output will show the container ID once it’s started.

Verification Step: Monitor the container’s health status using docker inspect or docker ps to ensure the health checks are running and reporting correctly.

Also check similar articles.

Interacting with Kubernetes Plugins using kubectl plugin
Configuring kubectl and kubeconfig Files
Exploring Kubernetes API Versions with kubectl api-versions
Understanding Kubernetes API Resources with kubectl api-resources
Generating Shell Completion Code with kubectl completion

Tags: DockerDocker Commands ExamplesDocker Commands TutorialDocker Tutorial
Previous Post

Interacting with Kubernetes Plugins using kubectl plugin

Next Post

Execute Commands Inside Running Kubernetes 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

Execute Commands Inside Running Kubernetes Containers

howto

Build Docker Images for Kubernetes

howto

Download Docker Images for Kubernetes

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.