Docker — Revenge of The Whale

Sarthak
CodeX
Published in
5 min readNov 18, 2022

We left at some docker commands and the basics of docker. Now let’s dive deep into it like a whale and come back up with mastering docker.

Topics left for us to cover are

  1. Docker FIle
  2. Docker Compose
  3. Some helpful commands

Now, if we can create containers with a one-line command why do we need a file for it? what if you had to deploy 10 containers, would you write that command 10 times? and what about the configuration of that file what if I had to run some commands in the container after making it? I will not do it 10 times, again and again, that’s not smart, if you are stupid you can, but if you are savvy like me then we are going to need bigger guns like DockerFIle and Docker Compose.

First, let’s discuss DockerFile. If I put it in layman’s terms, Dockerfile is used to create images. I have talked about images in my previous blog, if you are not aware of what images are then, for the love of god, read the previous blog or just google it, whatever fancies you. In short, we can create custom images with this DockerFile.

How do we write it? Good question, it follows a certain pattern with certain code words if you’d like to say. So the DockerFIle components include.

  1. FROM
  2. RUN
  3. MAINTAINER
  4. COPY
  5. ADD
  6. EXPOSE
  7. WORKDIR
  8. CMD
  9. ENTRYPOINT
  10. ENV

Too much? well, they are easy, let me break them down for you.

“FROM,” tells the docker engine to fetch an image, like ubuntu, arch, kali, OpenSUSE anything you want. and “FROM” always comes first in the docker file.

“RUN,” tells the docker engine to run some commands in the image, suppose you wanted to update the repositories or download something.

“MAINTAINER” basically is the author name of the image creator. that is you! yep, you.

“COPY” copies files from the local system to the image, you have to provide the destination path after it just copies it.

“ADD” is similar to copy but it provides an additional feature, the feature being that you can download from the internet and then include it in your image. “COPY” doesn’t allow this.

“EXPOSE” is used to expose ports in the image. exposing ports mean that we want to open a particular port on the image so that it can communicate with the internet or external networks, for example, port 80 for HTTP or port 22 for ssh, etc.

“WORKDIR” is used to set the working directory for container creation.

“CMD” execute commands but during container creation

“ENTRYPOINT” Similar to CMD but has a higher priority than CMD. So whatever you write in ENTRYPOINT will be considered first

“ENV” Environment Variables

Now for an example of a Docker File, we will keep it simple just for example sake.

This is what a docker file looks like. You can add more components to it as discussed above. Now, let’s discuss docker-compose.

After creating this docker file you can just go to that path where the docker file resides and then run this command

docker build -t <image name>

Now, your image would be built. what about the container now? what do we call a file which spins up containers on demand? Docker compose is here to save the day. Docker compose is a part of the Docker engine as a sub-utility to spin up many containers at once with different configurations. Docker compose file is written in YAML (Yet Another Markup Language). YAML is pretty easy to write and understand. A docker-compose file looks like this.

version defines the docker engine version to use. Under services, we write the container name like here it is named test web. Under that build is where we give the path to where we want our container to be built a “.” means that use the current directory. The image tag speaks for itself about what it does. command again is the shell code or anything you want to run in the container after it starts up.

The main advantage of a compose file is that if you want the container updated, suppose you want new configurations, or add/remove something in the container, docker-compose makes it very easy to do so. You just have to make the right changes in the file and then just run this command to build a new container again.

docker-compose up -d — build

Do remember that this command is to be run in the directory where the compose file stays. the name of the file according to the conventions is “docker-compose.yaml” and the name for the docker file is Dockerfile without any extensions.

Now that we have covered, docker-compose and Dockerfile, it's your turn to play with them, and explore more, these both have huge potential to help you when creating systems for microservices.

Let’s discuss the docker hub briefly, it is just like GitHub, the docker hub contains the images of docker containers, just as GitHub contains codes and software. Docker hub has two kinds of repositories, private and public. The titles Private and Public are enough to tell you what exactly is the nature of these repositories.

If you want to make an image from a container you created, we can use this command

docker commit <container name> <image name>

then you can view the newly created image from

docker images

If you want to see the difference in images, we can use this command

docker diff <image 1> <image 2>

Now let’s see some helpful commands in a real-world scenario.

Stop all running containers:

docker stop $(docker ps -a -q)

Delete all stopped containers:

docker rm $(docker ps -a -q)

Delete all images:

docker rm -f $(docker images -q)

That’s all folks for Docker. Let’s go with some Networks in the next blog. The building blocks of technology.

Peace out!

--

--