OneBite.Dev - Coding blog in a bite size

run a docker container

Simple docker step by step how to run a docker container with explanation

Getting started with running a Docker container is straightforward once you get the basics right.

Step 1: Install Docker

Before you can start running Docker containers, the first prerequisite is to install Docker. Docker can be installed on many different operating systems, including most popular ones like Ubuntu, Red Hat, and Windows (both Windows 10 and Windows server).

To install Docker, visit the official Docker website and download the suitable version for your operating system. Follow the installation guide included.

Step 2: Choose or Build a Docker Image

Before running a Docker container, you need a Docker image. An image is required to start a container. In this tutorial, let’s run a Docker container with Ubuntu image. You can either download an Ubuntu image from Docker hub or create your own.

Search for Ubuntu image in Docker hub through your command-line interface using the command

docker search ubuntu

To pull the image, use:

docker pull ubuntu

Step 3: Run the Docker Container

Now that you have the Docker image, the next step is to run the Docker container. The command to start a new Docker container is:

docker run -it ubuntu bash

With the -it flag, Docker will start a new container and a bash shell where users can interact with the container.

Once you’re in the Docker container, you should be able to see a different command prompt. This is the Ubuntu bash shell running within your Docker container.

Step 4: Control the Docker Container

To stop a running Docker container, use the Ctrl+D command within the Docker terminal or from another terminal, use the docker stop command followed by the container ID or name.

You can restart the stopped container using the docker start command followed by the container ID or name.

Step 5: Remove the Docker Container

If you wish to remove a container, first, you need to stop it, then use the docker rm command followed by the container ID or name.

In Summary

This is a mini-tutorial on how to run a Docker container. With these steps, you can start, play with, and stop your Docker container with ease. With the Docker concept, tasks related to creating, deploying and running applications become more instinctive and easy to manage.

docker