important docker commands

important docker commands

important docker commands

Important Docker Commands from Beginner to Expert level

Important docker commands I will be going over basic and important definitions and commands that is essential while working with docker images and docker containers

What is docker

The official definition of docker can be found here. But in short, you can consider docker as tool to develop and deploy code using the concept of containers

What is container

Important docker commands might not make any sense if we don’t properly define what a container is:

Container allow the application to “contain” all its dependencies including the OS in one place and making it a whole and self-sufficient application. Without the concept of docker, when you create application in java, you need the OS, the JDK installed, your database installed.. and all other things on your computer.

But, this has its own drawback. When you want to change the some of the dependencies, you will have to uninstall and install. In some cases compiling and installing might not be that easy.

As we will see in Important docker commands, the whole process of installing and uninstalling stuff will be a matter of some commands and can be achieved with short period of time. When we are taking about containers we have to discuss about images as well.

What is docker image

You may consider container as process. It is quite different from it, but for the sake of discussion we can take containers as process. Processes access the resources through the kernel – as they are running on the top of the OS.

In the case of docker, each process is running on its own environment and OS, b/c of that, it needs the docker engine to communicate through the kernel and reach out to the computer resources. For this to happen, containers has to be build in a way they have to be told what they need to package for the application that will run on them.

Important docker commands will go thorough lots of creation of docker containers and how to run them as we move further. But for now let’s concentrate on the concepts.

So, how does the container gets build? The basics of containers will be the Dockerfile Dockerfile can be thought of as recipe. It contains what is needed starting from the OS to all the dependencies and packages that need to be bundled for the application to run.

Once you have the Dockerfile, which is the recipe, the next task will be to build it. That is what we call the image. What happens is, the docker engine reads the Dockerfile and create an image that it will use later as container ( a kind of ).

Remember that container is a self contained application, everything that it needs is already packaged for it. So when you want to run the container, it gets its template from the image. All the bells and whistles are in image – at rest. And those will be used to run it when used as containers.

Installing Docker on my computer

Docker comes with multiple flavors per your OS. Follow the instructions on the https://docs.docker.com/install for the installation.

What version of docker am I running

Once you are done with installation you can verify the version by running

docker version

And you will get the version information like this:


Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:21:31 2018
 OS/Arch:           darwin/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:29:02 2018
  OS/Arch:          linux/amd64
  Experimental:     true

The output you get may vary based on the OS and version you are running.

Very simple container run example with explanation

docker container nginx

nginx docker container

Nginx is a well known webserver specially in the open source arena. Now if you want to run nginx the following important docker command will do the trick.
docker container --publish 80:80 --name dockernginx nginx.

What is going on? The first thing docker will do will be to find an image called nginx from the docker repository. The docker repository, https://hub.docker.com and will pull it to your local machine.

Once, the image is downloaded, it will be reused when you need it for other purposes as well.

Then you see the publish part that exposes port 80 traffic to the docker Mind you, this time the traffic coming through port 80 will all be transferred to the docker’s nginx and it will processed there.
Now if you go to http://localhost you will see the default page of nginx.

The –name argument you see will give the name dockernginx for the image. This will be handy as you will have many containers down the road and it will allow you to identify and work with it accordingly.

As you can see this simple important docker command has a lot of things going under the hood. As we advance, we will see more usages of it.

Running a docker container

docker container run --name my-apache -p 80:80 httpd

Note, the above important docker command, we use -p which is an alias for –publish

Listing all the docker containers

docker container ls

The above command lists all the containers that are running


CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
a65a3f963c67        mysql:5.7           "docker-entrypoint.s…"   2 weeks ago         Up 9 days           3306/tcp, 33060/tcp   my_mysql_1

It shows what port it is using and other important information. To show all the containers both running and not, use the following

docker container ls -a

Show logs of the docker container

docker container logs NAME_OF_CONTAINER_GOES_HERE

Run docker container in the background

docker container run --publish 80:80 --name my-nginx --detach nginx

The above command will run the nginx container and tunnels the port 80. Also, the detach command will force it to run in the background. This time, docker container will be running and the terminal will be free. If you run docker container ls you will the newly created container running.

Deleting the docker container

You may want to delete containers that are lingering around. For this first run the docker run ls and get the container name or hash.

Then issue the following command

docker container rm NAME_OF_CONTAINER_GOES_HERE

You might get an error saying you can’t delete running container if the container is still running. In that case, first stop the container and run the remove command.

Stopping docker container

docker container stop DOCKER_CONTAINER_NAME_GOES_HERE

More to come.

See how you would solve these known algorithm problems

Java solution for checking anagram strings – tell if phrases are anagrams

Get maximum occurring character

Implement Queue Using two Stacks – JavaScript algorithm

Find K Complementary numbers from array Java implementation

Check if there are three numbers a, b, c giving a total T from array A

find longest word in the sentence

Changing decimal number to its binary equivalent

Find the first occurence of number in the sorted array

Array reversal in Recurrsion

String Ordered Permutation Algorithm Problem

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*