Docker basics day 2

Docker basics day 2

Docker Images

When you start using Docker, you will come across the terms "Containers" and "Images". We have seen what a container is in the previous article. Now, let us understand about?Images.?

A Docker Image in a logical way is a container's template. If you think of an OOP paradigm as an example, we a "container class" as an image. Like, We instantiate an object from classes just here we instantiate containers from images. A Docker image contains all the instructions and binaries necessary to build and run a containerized app you want. You can find docker images for any framework, language and technology.?Docker Hub is a large repository where all primary images are stored.?

To install the Docker, you can find the documentation right here.

A "Hello World" in Docker

$ docker run hello-world
        


A docker hello-world image

If everything goes well, you will be able to see the above image. This image doesn't do much. So let's try something more interesting.

Pulling an Nginx Docker image

When we pull a docker image, we download that image from the remote repository usually the docker hub and build it into our host system. As I said in Day 1 Article, you can find images for almost any technology. Take an Nginx server for example.

$ docker pull nginx         

Here we download the latest official nginx image from the docker hub. After running the above command, you will notice some line outputs

No alt text provided for this image

If at any moment you want to see all the images you have in your local machine, you can list them by running:

$ docker image ls        

Running an Nginx container

We already learned how to pull an image (Nginx) from the docker hub. However, we cannot do much with just a docker image. If we want to run a Ngnix server from that image, we need to create and run a container using it.?

$ docker container run --rm -p 80:80 --name myNginx nginx        

Now let’s check the localhost of your PC. We should see the Nginx default page

No alt text provided for this image

Congratulations! You have run an Nginx server with just one command.

Let’s break down the command we used to understand what we did:

  • docker container run: is the docker instruction to run containers?duh??If you want to know more about how to use it, you can write?--help?after it.
  • — rm: this flag indicates the container will be deleted once is stopped. Otherwise, we’re going to have to remove it manually.
  • -p 80:80: indicates the ports to be published. The number before the colon is the port of your host machine. The number after the colon is the port used by the container. Thus, in this case, we are saying:?direct the connections from port 80 from my machine to port 80 from the container, and vice-versa.
  • — name myNginx: is giving our container the name “myNginx.” If we don’t indicate this, our container will default to a random name from the main Docker project contributors.
  • nginx: is the name of the image, which is the only required parameter of the command.

Probably, you want to know which containers are running at any moment. To see this, open a second terminal and run:

$ docker container ls        
No alt text provided for this image

Running a container in “detach” mode.

$ docker container run --rm -d -p 81:80 --name myNginxBG nginx        

As you can see, this is very similar to the first command we used. The only significant difference is that we included a detach flag (-d) and we are also using a different host port (81) and name (“myNginxBG”) which is done to avoid conflicts with the first container — the one running in the foreground.

Containers Management

In Docker, we can start, restart, stop and remove containers. These commands are very straightforward to understand:

$ docker container <action> <container_name>        

Let’s restart our nginx container running in “detach” mode:

$ docker container restart myNginxBG        

Now let’s stop it:

$ docker container stop myNginxBG        

And Finally, let’s remove this container manually:

$ docker container rm -f myNginxBG        

The “f” flag is used to force the container to stop beforehand.

That’s it for today

We have already learned a lot of things:

  • Pulling images from the docker hub
  • Listing images
  • Running containers
  • Listing containers
  • Running container in detach mode
  • Starting, restarting, stopping and removing containers


要查看或添加评论,请登录

Sathya Pramod DS的更多文章

  • Why cloud is a necessary part of organizations?

    Why cloud is a necessary part of organizations?

    Cloud computing is a model for delivering information technology services over the internet. It provides access to…

  • Docker Basics day 3

    Docker Basics day 3

    Welcome to the third article of Docker Basics. Previously, we learned some important concepts of Docker and also basic…

  • Docker Basics Day 1

    Docker Basics Day 1

    Firstly, we need to have a clear understanding of containerized applications. Let's see the official definition of What…

    4 条评论

社区洞察

其他会员也浏览了