Understand Docker basics and run a container
In order to start working with Docker, one must know what terms like Docker hub, Dockerfile, Image, and containers mean. And seeing things in action is the best way to learn something new. So In this post, We will create a container from scratch and on the way, we will learn all those terms as well.
Docker file:
Docker files are configuration files that are used to build the images. It is essentially a sequential set of instructions that instruct Docker enginer on how to build the image.
These instructions can be to specify a Docker image as a base, installing packages on top of base image, copying files to the images, mounting a folder from host to container as a volume, exposing a network port from container to host and many more. Docker file contains a list of commands that get sent to the docker engine.
Let’s start with creating our own Dockerfile.
STEP 1. Create a script file and write top
command in it. The top
command shows all running processes on a linux system. This is the file we will be running inside our container. Also, make sure the file is executable. you can do so with chmod +x filepath
command.
STEP 2. To create a docker file, write the following command into your terminal.
$ touch Dockerfile
STEP 3. Open your code editor in the same folder and write the following in the Dockerfile.
- Docker uses the Linux kernel. So most of the images use a base Linux installation.
- I have used alpine for this purpose. It is an official Linux image on Docker Hub.
FROM alpine
will pull the image for you. It is very popular choice as base due to being very lightweight. COPY
copies `hello.sh`, file into the image.- Specify a command or script with
CMD
that will be run when a container is started for this image.CMD
can accept arguments in multiple ways.
And our Dockerfile is ready.✌️
Docker Image:
A docker image is a configured app itself which when run will provide us the isolated environment on the host OS. It is built from a Dockerfile.
STEP 4. Build an image from the docker file with build
command.
$ docker build .
- `build` command takes a path as an argument. Here, our Dockerfile is in the same folder.
- `build` command performs steps mentioned in the docker file and creates an image. You can see at the bottom
successfully built <imageId>
.
An image can be thought of as a blueprint of the container. When we run an image it creates a container. And image tells a container which processes to run.
Docker Container:
A container is an instance of a docker image. It is called container because processes, running inside the container is isolated from the rest of the system. It has its own file system, network system, configured CPU usage, etc.
We can start, pause, and stop containers. A container is stateless, So It does not remember state/data.
STEP 5. Create a container from the image. Run the following command in the terminal.
$ sudo docker run --name newcontainer 4be0b0462779
run
command creates a container from the image.--name nameOfContainer
gives a name to the container.run
command also takes an ImageId.
- Here we can see only 2 processes are running. The reason why we only see 2 processes is that now we are inside a container and this container does not know about all the other processes running on your host system.
- The first process is
hello.sh
. The first process is the main process and we can not directly shut it down withctrl+c
as it ignores some of the commands.
STEP 6. Stop the container.
- To stop the
hello.sh
, we need to open a new terminal and run the following command.
$ sudo docker stop newcontainer
stop <containerName>
command stops the specified container.
This may take a while. After completion, you will see that the process has ended. 🎊 🎉
This was a very basic use case of docker. We run other processes inside a container the same way we ran a script file eg. databases, operating system, etc.
Docker Hub:
Docker Hub is a service provided by Docker. It allows you to create your repository on the Docker hub and store Docker images.
In general, you can think of Docker Hub as a place where we can push our images and pull someone else’s images. You can think of it as GitHub for Docker images. Popular softwares that are generally a dependency for your deployment environment, maintain their official images on Docker hub such as Postgres, Linux, Node and most that you can imagine.
Docker hub provides much more functionalities to help you and your team to use Docker images. Check Docker Hub to know more about it.
🤓 I Hope, You learned something.