DEV Community

Cover image for Docker Basic
Nandes Simanjuntak
Nandes Simanjuntak

Posted on • Edited on

Docker Basic

Introduction

Docker is one of the most popular Container Manager that introduced around 2013. Docker is a free and open source application, so that we can use it for free. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker's methodologies for shipping, testing, and deploying code, you can significantly reduce the delay between writing code and running it in production.

The Docker platform

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security lets you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you don't need to rely on what's installed on the host. You can share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.

Docker provides tooling and a platform to manage the lifecycle of your containers:

  • Develop your application and its supporting components using containers.
  • The container becomes the unit for distributing and testing your application.
  • When you're ready, deploy your application into your production environment, as a container or an orchestrated service. This works the same whether your production environment is a local data center, a cloud provider, or a hybrid of the two.

Docker Architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. When we install Docker, usually it already contains Docker Client and Docker Daemon. The Docker client and daemon can run on the same system and the Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface.

Image description

# command that you can use to check version of docker
docker version
Enter fullscreen mode Exit fullscreen mode

Docker Registry

Docker Registry is where we store Docker Images, with Docker Registry we can store image that we have made and it will can use in Docker Demon anywhere as long as it can connect to Docker Registry.

Example Docker Registry:

Docker Image

Docker Image is similar to an application installer, where in Docker Image there are applications and dependencies. Before we run application in docker, we need to be sure, that have Docker Image from that application.

# To show list docker image
docker image ls

# To download Docker Image from Docker Registry
# You can use http://hub.docker.com/ to find image that you want use
docker image pull imagename:tag

# Example to pull image
docker image pull redis:latest

# Remove Docker Image
docker image rm redis:latest
Enter fullscreen mode Exit fullscreen mode

Docker Container

A container is a runnable of an image. If a Docker Image is like an application installer, then a Docker Container is similar to an application from the installer. One image can use for a lot container with different Docker Container name. When you make Docker Container from Docker Image, the image can't to remove. You must be remove your container firstly then you can remove your image as you want. By default when we create container, the container is not running, we must run it firstly.

# To show list docker container
docker container ls

# Create container
# Note: exampleredis is name of container and
# redis:lates is the image and tag that want we use in container
docker container create --name exampleredis redis:latest

# Run container
docker container start exampleredis

# Stop container
docker container stop exampleredis

# Remove container
docker container rm exampleredis

# View log in container
docker container logs exampleredis

# Execute bash script in container
docker container exec -i -t exampleredis /bin/bash
Enter fullscreen mode Exit fullscreen mode

Container Port

When we run container, a container is relatively isolated from other containers and its host machine. Docker has the ability to do port forwarding, that is forwarding a port on the host system to a Docker Container when we create the container. This method is suitable if we want to expose the ports in the container to the outside via the host system.

# Command to forward port in container to system host.
docker container create --name containername --publish posthost:portcontainer image:tag

# Ex: forward nginx port in container
# Example below nginx using port 80 in container and we publish it to port 8080 to system host.
docker container create --name nginxcontainer --publish 8080:80 nginx:latest
Enter fullscreen mode Exit fullscreen mode

Environment Varible

We can use the environment variables in Docker to dynamically configure the application. We can pass --env as a param when we create a container and fill your configuration.

# Pass --env parameter as environment variable
docker container create --name containername --env KEY=value --env KEY2=value image:tag

# Here the example when we use mongo db image in container and pass the environment variable.
docker container create --name mongocontainer --env MONGO_INITDB_ROOT_USERNAME=root --env MONGO_INITDB_ROOT_PASSWORD=password mongo:latest
Enter fullscreen mode Exit fullscreen mode

Container Resource Limit

When we create container, by default will use all CPU and Memory that given to Docker on Mac and Windows, and will use all CPU and Memory that available in the Linux system host. When one of container to many use CPU and Memory, it will affect to performance in other container or maybe the system host. So that it better to use resource limit when we create container. Pass --memory or --cpus as argument when create container, example below.

# Implement resource limit using nginx image
docker container create --name containername --publish 8081:80 --memory 100m --cpus 0.5 nginx:latest

# If you want to view resource usage, use:
docker container stats
Enter fullscreen mode Exit fullscreen mode

Bind Mounts

Bind Mounts is the ability to mount (share) files or folders on the host system to containers on docker. This feature is very useful when we want to send configurations from outside the container, or for example save data created in the application inside the container into a folder on the host system. If a file or folder does not exist on the host system, it will be automatically created by Docker. To mount, we can use the --mount parameter when creating the container and the contents of the --mount parameter have their own rules.

Top comments (0)