Creating and Using Docker Containers: A Step-by-Step Guide

1
120
Creating docker container

In this third part of our Docker series, we’ll dive into one of the core aspects of Docker—containers. If you’re just joining us, I highly recommend reading the first article on Docker Introduction and Setup and the second article on Docker Networking to get a solid foundation. Now, let’s get into containers, the heart of Docker.

What are Containers?

Containers are the fundamental building blocks of Docker. They allow you to package applications and their dependencies into a single unit that runs consistently across various environments. Think of a container as a lightweight, isolated virtual environment. However, unlike traditional Virtual Machines (VMs), containers share the host system’s kernel, making them more efficient and faster to deploy.

In this article, we will:

  • Understand the basic concepts of containers
  • Learn how to run, manage, and stop containers
  • Use Nginx as our example to interact with Docker containers
  • Explore some essential Docker commands for container management

Installing and Verifying Docker

Before we start with containers, ensure that you have the latest version of Docker installed. Use the following command to check the version of both Docker Client and Server:

docker version

This command will return the version details of your Docker setup, confirming that both the Docker Client and Server are correctly installed and running. If you run into any errors, you may need to refer to the setup steps from our first article.

Running Your First Container

For this section, we will use Nginx, a popular lightweight web server. To start the Nginx container, use the following command:

docker container run -p 80:80 nginx

What Happens When We Run a Container?

When we run a container using the docker container run command, there’s more happening behind the scenes than just executing the container itself. Here’s a breakdown of the key steps:

  1. Image Lookup: Docker first looks for the specified image (e.g., nginx) in its local image cache. If the image isn’t found locally, Docker checks Docker Hub, which is the default remote repository. It downloads the image from Docker Hub and stores it in the local cache.
  2. Image Version: If no specific version is provided in the command (e.g., nginx:latest), Docker automatically pulls the latest version. You can specify a version explicitly by appending the version tag after the colon (e.g., nginx:1.21).
  3. Container Layer: Once Docker has the image, it doesn’t create a duplicate. Instead, it starts a new container and builds a layer on top of the base image. This layer is where any changes specific to this instance of the container will occur.
  4. Networking Setup: Docker assigns a virtual IP address within its internal network for the container. If a port is specified in the command (e.g., -p 80:80), Docker maps the host machine’s port (80) to the container’s port (80). Without the --publish option, no ports are exposed, and the container will not be accessible from the outside.
  5. Container Execution: Finally, the container runs by executing a default command defined in the Dockerfile. This command can be overridden from the command line when launching the container, providing flexibility to customize what the container does at runtime.

Behind this seemingly simple docker run command, Docker performs numerous tasks like image retrieval, networking, and command execution, all seamlessly. Each of these steps can be customized through Docker’s extensive set of options, and we’ll dive deeper into them in later sections.

Checking Your Running Container

After running the above command, open a browser and go to http://localhost. You should see the default Nginx welcome page, confirming that the Nginx container is up and running.

Docker automatically pulled the latest Nginx image from Docker Hub, started a new container, and mapped your local machine’s port 80 to the Nginx web server running inside the container.

To stop the container, press Ctrl + C. This will stop the container but not remove it from your system.

Detaching and Running Containers in the Background

Sometimes you may want to run a container in the background without locking up your terminal. For that, use the detach option:

docker container run -d -p 80:80 nginx

The -d option tells Docker to run the container in the background. Once the container is running, Docker will return a container ID, a unique identifier for your container.

Listing and Managing Containers

You can list all running containers using:

docker container ls

If you also want to see stopped containers, use:

docker container ls -a

To stop a running container, use:

docker container stop <container-id>

You can use the first few characters of the container ID as long as it is unique. For instance:

docker container stop 8a2

Removing Containers

Containers that are stopped will still consume disk space. To clean them up, use the rm command:

docker container rm <container-id>

You can remove multiple containers at once by separating their IDs with spaces. For example:

docker container rm 63f 690 0de

Naming Your Containers

By default, Docker assigns random names to containers. You can give your container a custom name with the --name flag:

docker container run -d -p 80:80 --name mynginx nginx

Now, when you list containers, you’ll see the name mynginx instead of a random name.

Viewing Logs

To see logs from a running or stopped container, use:

docker container logs <container-name>

For example:

docker container logs mynginx

You can also follow the logs in real-time by adding the -f flag:

docker container logs -f mynginx

Viewing Processes Inside a Container

You can inspect what processes are running inside a container using:

docker container top <container-name>

For example:

docker container top mynginx

This will display the processes running inside the Nginx container.

Cleaning Up

Once you are done experimenting with your containers, you can clean up by removing them. First, stop any running containers:

docker container stop mynginx

Then, remove the containers:

docker container rm mynginx

Getting a Shell Inside a Docker Container

In many cases, especially when debugging or configuring software, you’ll need direct access to a running Docker container. Instead of managing everything from outside the container, there are several ways to “get inside” and interact with the system directly, much like you would with SSH on a virtual machine or a physical server. Let’s explore how to do this with Docker.

The first step to getting a shell inside a container is understanding the docker container run command with the -it option. This allows us to launch a new container and directly access an interactive shell within it.

Using docker container run -it

The -it flag combines two options:

  • -i keeps the session open and interactive.
  • -t allocates a pseudo-TTY, giving us a shell prompt.

For instance, if you want to run a new container from the Nginx image and access the shell, you can execute:

docker container run -it nginx bash

This command overrides the default Nginx startup process, replacing it with a Bash shell. Once the container starts, you’ll be inside the shell, allowing you to navigate the file system, run commands, and even install software packages just like you would on a regular Linux system.

Differences in Containerized Linux Distributions

It’s worth noting that the Linux distributions inside containers are usually minimal versions. For example, running Ubuntu inside a container won’t give you the full suite of tools available on a typical Ubuntu installation. However, you can install additional software as needed, using package managers like apt-get.

To start an Ubuntu container and get into its shell, use:

docker container run -it ubuntu bash

Once inside, you can run commands like apt-get to install software:

apt-get update
apt-get install -y curl

Using docker container exec

Now, what if you want to access a running container rather than start a new one? For this, Docker provides the docker container exec command. It allows you to run a new process inside an existing container. For example, if you have a MySQL container running, and you want to open a shell to inspect or troubleshoot it, you can use:

docker container exec -it <container_name> bash

This way, you don’t need to restart the container or stop any of the services running inside it. You simply jump into the container and start working.

Getting a shell inside a Docker container is crucial for effective container management, whether you’re running a simple web server or a more complex service. By using the -it option with run and exec, you have the flexibility to manage both new and running containers interactively, enhancing your ability to troubleshoot, configure, and experiment.

Conclusion

In this article, we’ve explored the basics of working with Docker containers. We started a container, detached it to run in the background, viewed its logs, and stopped it. Containers are the cornerstone of Docker, and mastering them will allow you to deploy applications in isolated environments quickly and efficiently.

Next, we will explore Docker images in more detail, learning how to create and manage your own images. Stay tuned!

If you missed the earlier articles, you can check out the first article on Docker Introduction and second article on Docker Networking.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.