Docker containers are lightweight, portable, and self-sufficient units that include everything needed to run a piece of software, including code, runtime, system tools, libraries, and settings. Containers are based on images, which are read-only templates specifying how the container should behave.

In this tutorial, we will show you how to create a Docker container.

Prerequisites

  • A server running Ubuntu with Docker installed.
  • A root user or a user with sudo privileges.

Step 1 – Pulling a Docker Image

To create a Docker container, start with a Docker image. You can either pull an existing image from Docker Hub or create your own. Let’s start by pulling an image.

Let’s pull the Ubuntu image.

docker pull ubuntu

This will download the Ubuntu image from the Docker Hub registry.

Step 2 – Running a Docker Container

Next, create and start a container from the pulled image.

docker run -it --name my_ubuntu_container ubuntu

This command creates a container named my_ubuntu_container from the Ubuntu image and opens an interactive shell.

root@68cf357568f9:/#

Explanation:

  • -it: Interactive terminal.
  • –name: Name of the container.
  • ubuntu: The Docker image to use.

Step 3 – Interacting with a Docker Container

Once inside the container, you can interact with it as you would with any other terminal. For example:

apt update -y

This will update the package index in the Ubuntu container.

To exit from the container, run:

exit

Step 4 – Managing Docker Containers

List all running containers:

docker ps

List all containers including both start and stopped containers.

docker ps -a

Stop a running container.

docker stop my_ubuntu_container

Remove a stopped container.

docker rm my_ubuntu_container

Conclusion

Creating Docker containers is straightforward and highly beneficial for consistent application deployment. By following this guide, you now have the skills to create, manage, and customize Docker containers. Explore more by experimenting with different images and building your own to suit specific needs. Try to create a Docker container on dedicated server hosting from Atlantic.Net!