All you need to know about Docker with some helpful commands

Rohan Paris
6 min readMay 18, 2022

--

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure to deliver software quickly.

Overview:
1. What is Docker?
2. What is a Docker Container?
3. Container vs Virtual Machine
4. Useful docker commands
5. How to create a docker image
6. Reference

1. What is Docker?

We all must have faced the issue when an application works perfectly well in our local system but the same application generates an error when used on another machine. This typically is an issue when the code is used for testing by the QA team. The QA and development team might have systems configured with different hardware specifications/ OS/ libraries/ etc which are causing the application to not run as expected. Docker is used to solve this problem specifically.

image credits: https://www.reddit.com/r/ProgrammerHumor/comments/cw58z7/it_works_on_my_machine/

Docker is an open-source containerization platform. It helps in environment standardization by enabling developers to package applications into containers — standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

2. What is Docker Container?

A docker container is a collection of dependencies and code organized as software that enables applications to run quickly and efficiently in a range of computing environments.

A docker container is built using a docker image which is a blueprint that specifies how to run an application. It contains executable code along with the required libraries and dependencies required to run a container.

A container is a running instance of a docker image and it lives only till the process inside it is running. Containers built using the same docker image share the same OS kernel. This means that a container built using a Windows image cannot be used with a container built using a Ubuntu image. Containers built using Fedora, Debian, and Ubuntu share the same Linux kernel. A Ubuntu container can be used on a Windows OS by using a Linux virtual machine. Here the container is built on the virtual machine which is present on the Windows OS.

3. Container vs Virtual Machine

image credits: Author

Virtual machines can take up a lot of storage space. They can quickly grow to several gigabytes in size. This can lead to disk space shortage issues on the virtual machines host machine. Containers require less storage space.

Virtual machines are time-consuming to build and regenerate because they encompass a full-stack system. Because containers are lightweight and only include high-level software, they are very fast to modify and iterate on.

4. Useful docker commands

Docker can be installed by following the instructions from this docker docs link.

i. Command to get the installed docker version

docker --version

ii. Command to run a docker container

docker run -d image_name:tag

Here ‘tag’ means the image version which we need to run. If the tag is not provided then the latest version of the docker image from the docker hub is used.

Docker downloaded and then ran the latest Alpine image from the docker hub as the Alpine image was not available on my system.

By default, Docker creates containers with random names. These names can be used while stopping or removing a container. We can use the following command to run a container by giving it the desired name.

docker run -d --name container_name image_name

iii. Command to download the docker image

The below command will only download the latest image from the docker hub

docker pull image_name

iv. Command to list the available docker containers

docker ps -a

v. Command to stop a docker container

docker stop container_name/container_id

vi. Command to remove a stopped container

docker rm container_name/container_id

vii. Command to list the available docker images

docker images

viii. Command to remove a docker image

Before removing a docker image all the dependent containers must be stopped and then removed.

docker rmi image_name

ix. Command to get detailed information about the docker container

docker inspect container_name/container_id

5. How to create a docker image

A Dockerfile is used to create a docker image. It contains a set of instructions required to create a docker image.

image credits: https://www.freecodecamp.org/news/how-to-dockerize-a-flask-app/

We will now try to create a sample docker image for a flask application. Please find the code from this GitHub repository.

Dockerfile:

# syntax=docker/dockerfile:1FROM python:3.8-slim-busterWORKDIR /python-dockerCOPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .CMD [ “python3”, “-m” , “flask”, “run”, “ — host=0.0.0.0”]

Now let us try to understand what each of the above code lines means.

# syntax=docker/dockerfile:1

The above code should be the first line of every Dockerfile — it tells the Docker builder what syntax to use while parsing the Dockerfile and the location of the Docker syntax file.

FROM python:3.8-slim-buster

The above line tells Docker which base image to use — in this case, a Python image.

WORKDIR /python-docker

The above line tells which working directory to use.

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

The above line is used to install all the dependencies. The requirements file is created by using the below command

pip freeze>requirements.txt

The remaining files from the local working directory will be copied to the directory in the docker image using the below command

COPY . .

The docker image now has all the required files and dependencies required to run the flask application. Next, we need to tell Docker how to run this created image inside a container.

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

Build a Docker image:

We will build the docker image using the following command

docker build --tag python-docker .

We will then view the available docker images

Run a docker container:

We will now run a container based on the created docker image. The container name will be ‘docker_sample_flask’ and it will be open on port 5000

docker run -d --name docker_sample_flask -p 5000:5000 python-docker

App opened in browser:

flask app output in the browser

Uploading a docker image to docker registry:

In order to upload an image to the Docker registry, we must first build the image using the docker_username

docker build --tag docker_username/docker_imagename .docker push docker_username/docker_imagename

6. Reference

--

--

Rohan Paris
Rohan Paris

No responses yet