Run a Docker container in Ubuntu

Published: November 12, 2022

Run a Docker container in Ubuntu

A container basically helps us package our code in such a way that our applications can run in any environment. And, the Docker image contains everything that is required to run our code. In this article, we cover how to run docker container in Ubuntu.

Run a docker container in Ubuntu

The syntax for running a docker container is -

docker container run <container-image> <container-command>

For instance, if we want to run Ubuntu container with echo command then,

docker container run ubuntu echo "Hello World!"

Here, docker is the command-line utility. So, docker container run would mean, we want to run a container. The container image is ubuntu and echo "Hello World!" is the command which will be executed inside the container.

The moment you run a container, it would look for the specific image locally. If it isn't there then the following text you see in the output -

Unable to find image 'ubuntu:latest' locally

It would start Downloading it from Docker Hub if it doesn't find the image. Once the download is complete, it shows - Pull complete. Thereafter, the docker starts the container. So, we asked it to execute echo command. It shows the following as output -

Hello World!

If we try to run the container again, this time around the container image already exists. Therefore, it won't download it from Docker Hub.

It is worth mentioning here that, since we haven't provided a tag here for the container image. Therefore, docker assumes it to be the latest one. If we want to use a specific image then we should do -

docker container run <container-image:tag> <container-command>

For instance, to download ​kinetic-20221024 for Ubuntu -

docker container run ubuntu:kinetic-20221024 echo "Hello World!"

Lastly, to show a list of containers defined in our machine whether they are running or not -

docker container ls -a

In conclusion, we have covered the basics of how to run a docker container in Ubuntu here.