Predictive Hacks

Docker Quick Start

How to search Docker images from Docker Hub using the terminal.

Let’s say that we want to search for the Python images. We can run the command:

docker search python
Docker Quick Start 1

As we can see we get the NAME, the DESCRIPTION, the STARTS and if it is OFFICIAL and/or AUTOMATED. So, always try to get images with many starts and prefer to be official.

How to get images from Docker Hub

You can get an image from a public repository by running the command docker pull and the name of the image. Let’s say that we want to get the “hello-world” image.

docker pull hello-world:latest
Docker Quick Start 2

Notice that if you do not use any tag i.e.”:latest”, we get by default the latest. However, you can specify a particular version.

How to run a Docker Image

We can run the docker image with the run command. Let’s run the “hello-world” image.

docker run hello-world:latest

and as expected we get the message “Hello from Docker!” since that was intended to do.

Docker Quick Start 3

How to get the list of Images

Simply by running docker images you get the list of your images. In our case we have only the hello-world.

docker images
Docker Quick Start 4

How to get the Running Containers

The command to get the running containers is:

docker ps

How to inspect an Image and or Container

You can inspect a container as follows:

docker inspect <image_name>

How to run a Container in Interactive Mode

If you want to run a container in an interactive mode using the terminal you should use the -it. Let’s run the centos image.

docker run -it centos:latest /bin/bash
Docker Quick Start 5

Now we are in the container operating system! As we can see the container is running:

Docker Quick Start 6

and if we want to exit it we type exit.

Docker Quick Start 7

How to run a Container in Backgroung

You should use the flag -d (detached) and the command is:

docker run -d nginx:latest

How to Stop a Container

First we need to find the container id or the container name using the ps command.

docker stop nervous_babbage

How to Execute a command in a Container

If you have a running container and you want to execute a command in an interactive mode then you can run:

docker exec -it <container_name> /bin/bash

Above, we ran the bin/bash so that to be able to start interacting with the container using the terminal.

How to Remove an Image

The image that you want to remove must not be used and depend in any container. Then you can run the

docker rmi <image_name>

You can use the flag -f to force to remove the image even if it is used in any running container.

docker rmi -f <image_name>

How to Remove ALL Non-Running Containers

You can remove all non-running containers with the command:

docker rm $(docker ps -a -q)

How to map Ports

You can define the local ports using the flag -p as follows:

docker run -d -p 8080:80 --name=MyWebServer nginx:latest
Docker Quick Start 8

How to Mount Data

You can mount data in your container using the flag -v from volume.

docker run -d -p 8080:80 --name=MyWebServer2 -v /mnt/data nginx:latest

If we want to map the volume mount we can run the command:

docker run -d -p 8080:80 --name=MyWebServer2 -v /home/user/data:/user/share/nginx/my_folder nginx:latest

Now, whenever we make changes within the data folder which is in our local PC, will pass automatically to the my_folder which is within the container.

Dockerfile

You can build an image from a Dockerfile. The name of the dockerfile must be just Dockerfile without any extension and you can build your image by running:

docker build .

Notice the period . which refers to your working directory where the Dockerfile should be. The structure of the Dockerfile is:

FROM debian:stable

RUN apt-get update &amp;&amp; apt-get upgrade -y

ENV MYVALUE my-value

EXPOSE 80

CMD ["/usr/sbin/apach2ctl", "-D", "FOREGROUND"]
  • FROM: Get the base image
  • RUN: Run the commands on the base image
  • ENV: Create environment values
  • EXPOSE: Expose the port
  • CMD: Run within the container

References

[1] Linux Academy

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore