Running a Docker image as a container

DockerOperating SystemOpen Source

Docker allows you to create containerized, packaged, and isolated environments called Docker containers using Docker images. Inside these Docker containers, you can build, test, and even deploy your applications. This allows you to easily share your application run-time environment with other developers. All of this is possible because of a read-only template called Docker images.

You can pull Docker images directly from any Docker registry such as Dockerhub or use a bse image inside a Dockerfile to build your own custom images. You can then use the Docker build command to build your Docker images. Once you have your Docker image ready, you can use the Docker run command to create a container associated with that image. You can then have access to the applications runtime environment through the bash of the container.

All of these can be done using simple Docker commands. Let’s discuss all the relevant commands to run a Docker image as a container.

Running Docker Image as a Container

To run a container associated with an image, you need to have an image already existing in your machine. There are two different ways to create an image.

  • Pull an Image directly from Dockerhub.
  • Create an Image using a Dockerfile.

First, let’s try to pull an image directly from the Dockerhub registry. We will try to pull the Ubuntu image of the latest version using the Docker pull command. To do so, execute the command mentioned below.

$ docker pull ubuntu:latest

If you don’t already have an image with the same digest existing in your machine, the daemon will try to pull it from the Dockerhub. If you already have a similar image, it won’t pull a new one and simply create a copy of the existing one.

To check whether the pull was successful or not, you can list all the images.

$ docker images

You will find that the image has been successfully pulled.

Now that you have the ubuntu image ready, let’s use the Docker run command to run a container associated with the image.

$ docker run -it --name=ubuntucontainer ubuntu bash

In the above command, we have used the interactive and pseudo-TTY options which will allow us to interact with the container interactively through a terminal driver. We have used the name option to give a name to the container. Finally, we have specified the name of the image and the command that needs to be run once the container is started. In this, the command is bash which will give us access to the bash of the container.

Once you have executed this command, you should have access to an Ubuntu environment inside the container. You can interact with the container the same way you would do with an Ubuntu machine. To check the status of the container, you can use the following commands.

$ docker ps

This command is used to list all the active containers.

$ docker ps -a

This command is used to list all the existing containers in your machine.

Another way is to create an image using the Dockerfile. Consider the Dockerfile below.

FROM ubuntu:latest
WORKDIR /app

In the above Dockerfile, we have used the FROM instruction that will allow us to use the ubuntu:latest image as our base image. We have used the WORKDIR instruction to set the /app directory as the default working directory. You can now use the Docker build command to build an image associated with this Dockerfile.

$ docker build -t myubuntu:latest .

Here, we have used the -t option which will allow us to specify a name and tag to the image. The dot at the end specifies the path of the Dockerfile which is the current directory. Once you have executed this command, you can check the image creation by listing the images.

$ docker images

Now, you can use the Docker run command again to run a container associated with the image.

$ docker run -it --name=ubuntucontainer myubuntu:latest bash

Even if you don’t have an image existing in your machine, you can use the Docker run command directly. Suppose, you want to create a container associated with a centos image. You can use the command below directly.

$ docker run -it --name=centosinstance centos:latest bash

On executing the Docker run command, the daemon will first check whether there is a centos image on your system or not. If it does not find it, it will execute the Docker pull command at the background to pull the centos image. Once done, it will then execute the Docker start command to start a container associated with the centos image in the background. After that, it will use the Docker run command to run the container. All of these takes place in the background.

To sum up, you can either directly use the Docker run command to run a Docker container associated with an image even if you don’t have one. Or you can pull or build a Docker image first, and then use the Docker run command to create a container.

raja
Published on 06-Aug-2021 12:27:49
Advertisements