Docker basics for beginners
Docker is a containerization platform. Following are some basic commands and their purposes
Let’s understand the basics.
:::tip Docker Images A docker image is like a template for a containerized application. Docker image defines what is the base image, what dependencies need to be installed and how to run the application. :::
Let’s take a look at the commands that are for docker images.
List all the docker images available in the system
docker images
The output will be look as follows:
REPOSITORY TAG IMAGE ID CREATED SIZE
openadr_dummy-ven20b latest 83b824be968e 5 weeks ago 269MB
REPOSITORY: the name of the image.
TAG: the version of the image (latest being the newest).
IMAGE ID: the unique identifier for the image.
CREATED: the time when the image was created.
SIZE: the size of the image in MB.
Docker image ls is similar to the above command
docker image ls
Let’s say you need to run a container from an image that you do not currently have. To achieve this you need to first pull the docker image from a docker repository. Different cloud service providers offer different docker registries/repositories. Docker hub is the most popular one and provides space to host public dockers.
Let’s try to pull an image from the docker hub. For this example I will use alpine image from docker
docker pull alpine
Please note that we used the image name directly as we are pulling from the default docker registry (docker hub). If you need to pull an image from a different container registry, you need to provide the exact URL. For example, to pull from AWS ECR (Elasic Container Registry)
docker pull aws_account_id.dkr.ecr.us-west-2.amazonaws.com/amazonlinux:latest
Once you have pulled the image, you can run a container from it. A docker container stays alive only if there is a program running inside the container. Since this alpine image does not have an active program running inside, it will run and terminate. Let’s run it and see.
docker run alpine
You would not see any difference, due to the reason above.
Let’s look at how to create an image with an application we need. For this purpose, we need to build a docker image. To build a docker image, a Dockerfile is required.
:::tip Dockerfile A Dockerfile is an instruction list on how to create a docker image. A dockerfile has to be renamed exactly as Dockerfile for docker to recognize it. :::
:::tip Docker Containers A docker container is a running instance of a docker image. :::
Docker Container: A docker container is a running instance of a docker image.
docker-compose up - download dependencies and run the application
docker-compose down --rmi all - remove all the dependencies
tell docker to package our app to image
ask docker to run the app in a container
docker run
docker images
docker pull <image>
docker images ---> shows images
docker image ls ---> shows images
docker ps
docker ps -a >> lists all containers used so far
docker stop <image>
docker rm <image> remove the container
docker rmi <image name> removes the image
Create docker file then run the command
docker build -t <new_image_name> . << dot required
OR
docker build -t <new_name> -f Dockerfile
docker run <image>
docker run <image> sleep 5 >> sleeps for 5 seconds
docker run -i <image> interactive but not able to take inputs
docker run -it <image> ----> opens the shell to the docker image
docker exec <image> <command>
docker run -p 80:5000 <image> >> port mapped from 80 to 5000
docker run -e APP_COLOR=blue <container> >> APP_COLOR is an environment variable, app can use it
docker inspect <container>
docker run >> attached
docker run -d >> detached
docker attach <id>
Docker stop all containers
docker stop $(docker ps -a -q)