DEV Community

Cover image for Introduction to Docker
Freweini Beyene
Freweini Beyene

Posted on • Updated on

Introduction to Docker

Docker allows users to package an app with all of its dependencies into a standardized unit for software development. Docker is a tool to easily deploy applications in a sandbox(called containers) to run on the host operating system i.e. Linux. The sandbox for kids is a box filled with sand and toys they can play with, it’s placed in the middle of the grasses in the backyard where they can play.

What is a Containers:
A container is a sandboxed process running on a host machine that is isolated from all other processes running on that host machine. container is an “instance” of an image. A running container uses an isolated filesystem.

What is an Images:
The isolated filesystem is provided by an image, image must contain everything like dependencies, configurations, scripts, binaries etc.

Difference between images and containers
Docker images and containers are closely related terms and its common to confuse the two for beginners. Docker images are built by executing commands in the Docker File and what it primarily does is that it captures the run-time environment parameters so that if it runs again in some other machine, it runs exactly the same as the current machine. On the other hand, a container is an “instance” of an image.In simple words, docker image be a blueprint of a building, and a container would then be the actual building!

Writing a Docker file
Docker file is used to specify instructions to create a Docker container. It is essentially a list of commands in a text file( Docker file has no extension).
You open a dockerfile using the touch command.
$ touch dockerfile

  • A dockerfile is typically constructed from a base image such as Ubuntu(OS), SQL to allow a primary setting of the environment. This is done as follows:
    Syntax: FROM
    Example: FROM ubuntu:latest

  • It is then essential to establish the working directory. You either create one using mkdir command or setting an existing directory as working directory by using
    WORKDIR /

    And then copy the contents in the container

    COPY . /

  • You can then expose ports or run preferred commands. Some examples are below →

    Exposing port 8080

    EXPOSE 8080

    Installing dependencies

    RUN apt-get install -y python

    Running Python

    CMD ["python", "main.py"]

Note: The difference between RUN and CMD is that the RUN command is executed while building the image and CMD command is executed when we start the container.

In the end, it will look like this:
FROM ubuntu:latest
FROM node:18
# Create app directory
WORKDIR /app
# Install app dependencies
COPY package*.json ./package.json
COPY package-lock.json ./package-lock.json
RUN npm install
# Bundle app source
COPY . /app
EXPOSE 8080
CMD [ "node", "index.js" ]

Once the Docker file is created, you can just build the image using the command
$ docker build
Docker will find the dockerfile in the current directory and build a docker image based on it.

To see the list of docker images available locally
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 6 months ago 13.3kB

Similarly, you can get a list of containers in a tabular format using the command
$ docker ps
CONTAINER ID IMAGE COMMAND NAMES
e90b8831a4b8 nginx "/bin/bash -c 'mkdir'" my_nginx
00c6131c5e30 telegraf:1.5 "/entrypoint.sh" my_telegraf

Create a container from a docker image and run it
$ docker run [ or ]

You can start or stop a container using the start and stop command
$ docker start
$ docker stop

You can pull or push an image to Docker Hub. Docker Hub is the world's largest library and community for container images.
$ docker push
$ docker pull

You can also remove images and containers using rm or rmi command
$ docker rm
$ docker rmi

Hope that has helped you, thanks for reading.

Top comments (1)

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

Hello! Welcome to DEV!

You know, for quite some time I've heard of Docker, but I didn't realize what actually was until not so long ago. And is so good to test new tools. Specially when using the templates from dockerhub.