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
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
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
Congratulations! You have run an Nginx server with just one command.
领英推荐
Let’s break down the command we used to understand what we did:
Probably, you want to know which containers are running at any moment. To see this, open a second terminal and run:
$ docker container ls
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: