Docker Containers~!

Docker Containers~!


4 min read

Docker Containers~!

I never knew anything about dockers till about 7–8 weeks ago (as of writing this). I had come into contact with dockers only because of a school project. So why not document it……amiright? hehe lesgo~!

What is a Docker Container?

A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Containers are an abstraction at the app layer that packages code and dependencies together. You can think of it as a virtual machine but way lighter! You can run multiple containers on the same machine whilst sharing the OS kernel with the containers! Containers also consume lesser storage space than VMs.

Docker Containers~!

Docker ebook can be found here

Virtual Machines vs Container

Containers and virtual machines have similar resource isolation and allocation benefits but function differently because containers virtualize the operating system instead of hardware. Containers are more portable and efficient.

Docker Containers~!

Installing Docker [Linux]

Step 1 — Prepping our machine

We gotta make sure the system up to date!

root@kali:~# apt-get update

You should have the following packages in your Kali install. If not run the following command to install them:

root@kali:~# apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common

Step 2— Adding the Docker Repository

Configure Docker apt repository:

root@kali:~# echo 'deb stretch stable' > /etc/apt/sources.list(.d/docker.list)->optional

Step 3— Installing Docker onto the machine

Command to perform the installation:

root@kali:~# apt-get install docker-ce docker-compose

Now that we have it installed Docker successfully, Lets verify the installation!

root@kali:~# docker --versionDocker version 19.03.8, build afacb8b7f0


Docker Search

We have a bunch of images that are provided by docker! Let's search for an image that we can create a container with. For this demo, I’m gonna look for a raspberry image that I can use.

root@kali:~# docker search {image_name}

Docker Containers~!

Docker Pull

Now that we have identified and verified that the image we want is available, let's pull it down to our machine.

root@kali:~# docker pull {image_name}

Docker Containers~!

Docker Images

Now that we have installed the image, let's verify that.

root@kali:~# docker images

Docker Containers~!

As seen above, the ubuntu image we downloaded shows up.

Docker Run

Now let's create a container with the base image that we just downloaded.

root@kali:~# docker run -it --rm --name {containername} {image} {application to launch on start}

-it = Interactive mode (This allows u to interact with the shell as soon as it has launched)

--rm = remove the container totally on exit (wont be able to re enter the container)

--name = Gives the container a name

Docker Containers~!

Now that we have launched a container successfully, let’s verify it with the following command!

root@b0ac437c391b:/# uname -a

^disclaimer:the container launches with a random name everytime its relaunched

Docker Containers~!

Now we are free to do whatever we please on the container :)

Docker Commit

A container starts up fresh unless we commit the contents of a particular container into an image. The first thing we need to do is run the container without the --rm . This will make sure that we are able to commit the container into an image on exit.

Docker Containers~!

Now, make your necessary changes to the container and exit it gracefully.

Docker Containers~!

On exit, enter the following command to commit this container into another image.

root@kali:~# docker commit {container_name} {custom_image_name}

^disclaimer: the custom image name cant have any caps

Docker Containers~!

If you receive the hash as seen above, we have committed the docker container successfully. The next time we launch the container we have to replace the {ubuntu} with {commitedcontainer}

root@kali:~# docker run -it --rm --name {containername} {image}

Docker Containers~!

Docker Remove

Now let’s say we have a container running and we are not inside of it to exit it because we launched it without the — -rm tag to commit it. This is how we shut down a container gracefully.

First, we need to find out what we would like to close. For that, we need to check the docker process list with this command.

root@kali:~# docker ps -a

Docker Containers~!

In the screenshot above, I have 2 containers running. Let's close those 2 containers. Command:

root@b0ac437c391b:/# docker rm {container NAME} --force

Docker Containers~!

That's about it! That's all you need to know to get started with docker! However, you will learn more along the way. Allthebest!