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
Discussion about this post