Accessing and Managing Docker Container

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful tool for deploying applications in containers, which are lightweight and portable environments that can run on any system with Docker installed. In this lab, we will learn how to access and manage Docker containers using the Docker command line interface (CLI). We will cover basic container management tasks, including starting and stopping containers, accessing container logs, and executing commands inside a container.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker/ContainerOperationsGroup -.-> docker/rm("`Remove Container`") docker/ContainerOperationsGroup -.-> docker/exec("`Execute Command in Container`") docker/ContainerOperationsGroup -.-> docker/logs("`View Container Logs`") docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") subgraph Lab Skills docker/rm -.-> lab-7770{{"`Accessing and Managing Docker Container`"}} docker/exec -.-> lab-7770{{"`Accessing and Managing Docker Container`"}} docker/logs -.-> lab-7770{{"`Accessing and Managing Docker Container`"}} docker/ps -.-> lab-7770{{"`Accessing and Managing Docker Container`"}} docker/run -.-> lab-7770{{"`Accessing and Managing Docker Container`"}} docker/stop -.-> lab-7770{{"`Accessing and Managing Docker Container`"}} end

Starting a Container

In this step, we will start a container and access its logs.

  1. Run the following command to start a new container:
docker run -d --name my-container nginx

This command starts a new container based on the nginx image and gives it a name of my-container. The -d option to run the container in the background.

Docker will remotely download the latest version for use if there is no corresponding image locally.

Views the result:

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
2f44b7a888fa: Pull complete
8b7dd3ed1dc3: Pull complete
35497dd96569: Pull complete
36664b6ce66b: Pull complete
2d455521f76c: Pull complete
dc9c4fdb83d6: Pull complete
8056d2bcf3b6: Pull complete
Digest: sha256:4c0fdaa8b6341bfdeca5f18f7837462c80cff90527ee35ef185571e1c327beac
Status: Downloaded newer image for nginx:latest
e34003d56d6c4bb78856c7cbba3b662836870bf03912e80fa9292ebee3db57d9
  1. Run the following command to access the logs of the container:
docker logs my-container

This command prints the logs of the container to the terminal:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/01/19 03:24:38 [notice] 1#1: using the "epoll" event method
2024/01/19 03:24:38 [notice] 1#1: nginx/1.25.3
2024/01/19 03:24:38 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/01/19 03:24:38 [notice] 1#1: OS: Linux 5.15.0-56-generic
2024/01/19 03:24:38 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/01/19 03:24:38 [notice] 1#1: start worker processes
2024/01/19 03:24:38 [notice] 1#1: start worker process 29
2024/01/19 03:24:38 [notice] 1#1: start worker process 30

Stopping a Container

In this step, we will stop a running container.

  1. Run the following command to stop the container we started in the previous step:
docker stop my-container

This command stops the running container with the name my-container.

  1. Run the following command to confirm that the container has been stopped:
docker ps -a

This command lists all containers, including stopped containers. You should see the my-container container in the list with a status of Exited.

View the output:

CONTAINER ID   IMAGE                                 COMMAND                  CREATED          STATUS                      PORTS                                                                                                                                  NAMES
e34003d56d6c   nginx                                 "/docker-entrypoint.โ€ฆ"   10 minutes ago   Exited (0) 36 seconds ago                                                                                                                                          my-container

3ac7663a03de   gcr.io/k8s-minikube/kicbase:v0.0.37   "/usr/local/bin/entrโ€ฆ"   10 months ago    Exited (255) 6 months ago   127.0.0.1:49157->22/tcp, 127.0.0.1:49156->2376/tcp, 127.0.0.1:49155->5000/tcp, 127.0.0.1:49154->8443/tcp, 127.0.0.1:49153->32443/tcp   minikube

Executing Commands in a Container

In this step, we will execute a command inside a running container.

  1. Run the following command to start a new container with a shell:
docker run -d --name my-shell-container ubuntu sleep 3600

This command starts a new container based on the ubuntu image and gives it a name of my-shell-container. In this example, we have the container perform sleep 3600 and remain active for 3600 seconds (1 hour) even when the container is running in the background.

  1. Run the following command to execute a command inside the running container:
docker exec my-shell-container echo "Hello World"

This command executes the echo "Hello World" command inside the my-shell-container container. And it print the string "Hello World" on the standard output of this terminal.

Hello world
  1. Run the following command to stop the container we started in the previous step:
docker stop my-shell-container

This command stops the running container with the name my-shell-container.

Removing a Container

In this step, we will remove a container.

  1. Run the following command to remove the container we started in the previous step:
docker rm my-shell-container

This command removes the container with the name my-shell-container.

  1. Run the following command to confirm that the container has been removed:
docker ps -a

This command lists all containers, including stopped containers. You should not see the my-shell-container container in the list.

Summary

In this lab, we learned how to access and manage Docker containers using the Docker CLI. We covered basic container management tasks, including starting and stopping containers, accessing container logs, executing commands inside a container, and removing containers. With these skills, you can start exploring more complex Docker container management scenarios and get hands-on experience with Docker.

Other Docker Tutorials you may like