interleap
Published in

interleap

Docker: Understanding Containers

Read this article to get a brief intro on why docker.

Let’s consider a scenario where I have built an application on Ubuntu and I want my friend Keyser Soze to give me a feedback on it.

Keyser Soze uses a mac.

Now in order to run my application, he will have to install all mac version of the dependencies of my app.

To summarise anyone with a different operating system will have to repeat this process unnecessarily.

I want some way which ensures that once I build my application, anyone can run it irrespective of what OS are they using or what version of the dependency they have or even they have it or not.

To achieve this exact thing containers came into being.

A container isolates the application it runs completely from any kind of dependencies including OS or any other external environment.

A container can be thought of as a wrapper over our application.

It packs all of the application’s dependencies including the OS it runs on.

As in the image below container separates nginx application from the outside environment, be it OS or any other thing.

Let’s see all this in action

Check if you have installed docker using docker — version command.

It will return the docker version if it is installed

Docker version 19.03.5, build 633a0ea838

If you have not already installed docker, you can install it from here https://docs.docker.com/install/

Let’s run our first container.

  1. Run the following command to start an nginx container

docker container run -p 80:80 nginx

2. Navigate to http://localhost

3. You will see nginx’s welcome screen

You have successfully started an nginx container without any local installation:)

Breaking down the command

docker container run -p 80:80 nginx

This command can be broken down into 5 parts

  1. docker
  2. container
  3. run
  4. -p 80:80
  5. nginx

Let’s understand all these one by one.

docker

Any command that starts with <docker> is for docker-engine daemon.

All commands will start with docker whenever you want to talk to the daemon.

container

Docker comprises of various components like containers, images, volumes.

<container> tells docker-engine that you are going to do something with container component.

run

<run> command is an action over container component.

In this command we are telling docker to run a container.

-p 80:80

p flag is a shorthand for publish.

publish maps the port of the host machine to the containers port, so that it is possible to talk to the container.

-p <HostPort>:<ContainerPort>

nginx

nginx is the name of the image the containers uses.It can be thought of as a blueprint the container is formed from.

Containers are a running instance of an image.

Running containers in detached mode

By default containers run in foreground.

To run container in detached mode/ background, use -d flag

docker container run -p 80:80 -d nginx //returns CONTAINER ID
d956b7b07859f4ebd1bb0a8247a19d8b99ac8b6cf496c72b25af83ba8bc8aabe

Verifying if the container is running

Once you have started your first container you can check if it is running or not using the command

docker container ls
OR
docker ps

You will see all running containers

CONTAINER ID IMAGE COMMAND                CREATED       STATUS 
d956b7b07859 nginx “nginx -g ‘daemon of…” 2 minutes ago Up 2 minutes
PORTS NAMES
0.0.0.0:84->80/tcp festive_blackwell

To see all containers including the stopped ones run the following command

docker container ls -a
OR
docker ps -a

You will see all the containers some with status as exited

CONTAINER ID IMAGE COMMAND                CREATED       STATUS 
d956b7b07859 nginx “nginx -g ‘daemon of…” 2 minutes ago Up 2 minutes
esld5fb98000 nginx “nginx -g ‘daemon of…” 4 minutes Exited (0) 3 minutes ago
PORTS NAMES
0.0.0.0:84->80/tcp festive_blackwell

Once your application is terminated or it dies the container dies too. As their only job is to run the application in isolation.

Containers are just processes running on your machine. Yes they are just processes.

When you instantiate a container, all you are really doing is tell docker engine to run a process on the host machine with your application in it.

Hope you have a good idea about what containers really are now.

In the next article we will cover images in depth. Do check it out.

Happy Learning:)If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store