Skip to main content

Docker - Images and Containers

Greetings!

Docker image to container(s)

Image

An image is a read-only template with instructions for creating a Docker container. It is a combination of file system and parameters. Often, an image is based on another image with some additional customization.
We can use existing images or create our own images.

Container

A container is a runnable instance of an image. We can create as many as we want from an image. Container is isolated from the host by default. We can modify it's behavior using network, volume, etc.
When a container is created, we can stop, restart, remove it.

Download an Image

We can download a Docker image using 2 methods.
  • pull - we can use pull command to get an image
$ docker image pull nginx
  • create a container - when we create a container from an image, it downloads the image from the repository if it is not available in the host.
$ docker container run nginx

Docker command structure

There are many commands. So Docker has grouped them together into a common format.
docker COMMAND SUBCOMMAND
docker container ls

Useful command summary

  • docker image ls - list available images
  • docker image rm - remove an image
  • docker image inspect - inspect an image
  • docker container run - run a container
  • docker container ls - list containers
  • docker container stop - stop a running container

Dry run

Let's test these commands with nginx.
$ docker container run --name my-nginx -p 80:80 nginx
$ docker image ls
$ docker container ls
Visit localhost in your browser. You should see nginx is running.
$ docker container inspect my-nginx
$ docker image inspect nginx

Now let's stop our container
$ docker container stop my-nginx
$ docker container ls
$ docker container ls -a

Let's start it again
$ docker container start my-nginx

Let's remove our container completely.
$ docker container stop my-nginx
$ docker container remove my-nginx

Let's remove image as well.
$ docker image remove nginx

Comments