Basic Docker commands

  • docker run hello-world 

Docker looks for this image on our local system. If its not on local then,it downloads hello-world image from docker hub and runs               it .

  • docker run -it ubuntu bash

We downloaded an Ubuntu Linux image and started a login shell as root inside it. The -it flags allow us to interact with the shell.

  • docker ps -a

It lists all running containers on our system.

[email protected]:/etc/bash_completion.d$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
830992bb76cd hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago sleepy_fermi

From this, we can see that the hello-world container is still in memory. The status column tells us that it’s exited. The names column has a name, sleepy_fermi, that Docker assigned to the container for us.

Let’s run this image again with docker run hello-world. This time console output will not be same as image will not be downloaded again as its already downloaded.

 Run docker ps -a again , what it will show?

We see two stopped instances of hello-world, with two different names. Docker created an additional container. It didn’t reuse the first. When we told Docker to run an image named hello-world, it did exactly that; it ran a new instance of the image. If we want to reuse a container, we refer to it by name.

  • docker stop sleepy_fermi

This will stop the running instance in a container. We need to pass CONTAINER NAME as a parameter to this command.

  • docker rm sleepy_fermi

This will remove the container from docker . We need to pass CONTAINER NAME as a parameter to this command.

  • docker image ls

This produces a listing of images on our system. We see Ubuntu and the single hello-world image since we only needed that single image to run two containers.

  • docker image rm <<IMAGE_NAME>>

Removes the image which we have downloaded on local .

  • docker build -t <<IMAGE_NAME>> .

This will build the image for any custom application from the configuration done in Dockerfile

-t  – This option is for

<<IMAGE_NAME>> – We can assign any name as per our wish to image.

dot(.) – This is reference to the current directory which might be used for building war/jar file.

  • docker run -v /full/path/to/html/directory:/usr/share/nginx/html:ro -p 8080:80 -d nginx 

Sharing local resources with Container –

As of now , we have run independent images. But we have nginx image which can share the data on host OS with the containers.

Here we an share the data on host OS or Network resources with containers.

[email protected]:/var/test$ docker run -v /var/test/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
be8881be8156: Pull complete
32d9726baeef: Pull complete
87e5e6f71297: Pull complete
Digest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
Status: Downloaded newer image for nginx:latest
ee6c7a8fd3a7964c9ea6da35793a7244bf878d367d2d0930061da140380cd14d

Command:   docker run -v /full/path/to/html/directory:/usr/share/nginx/html:ro -p 8080:80 -d nginx

When we execute this command line, we see Docker download the Nginx image and then start the container.

/full/path/to/html/directory – Path on host OS where we can keep our files to be hosted.

/usr/share/nginx/html:ro – Path to container

-p 8080:80 – maps network service port 80 in the container to 8080 on our host system.

-d  – detaches the container from our command line session.

nginx – the name of the image.

Leave a Comment

Bitnami