What is docker, docker image, docker container? Basic Docker commands.
Simply docker is a tool used to create a magic box of an application to run on any system without environment or OS issues. This magic box is called a docker image. If we run this image, we are running a copy of the magic box. This copy is called a container. Many docker containers can be created using the same docker image.
Creating docker images
We use something called Dockerfile to create a docker image of the application. Here is a sample Dockerfile for a python based application
FROM python:3.8
ADD ./path/to/myapp /app
RUN pip install -r /app/requirements.txt
WORKDIR /app
ENTRYPOINT [“/app/run.sh”]
Initially, we import a base docker image. One can find many base docker images for an application in Dockerhub.
Then we copy all the files from the application to a directory name app.
Install the required packages. RUN will execute the given command in the terminal.
Now we set the working directory to the path with all these files. That makes the bash script we run will take the file paths from the ‘app’ directory.
docker build -t myimage:tagv1 -f ./path/to/Dockerfile ./path/to/app/files
The above command creates the docker image. The ‘t’ flag is to name the image and tag it with some name like versioning.
docker images
The above command gives you the list of all the docker images. Each image will have a name, tag, image ID.
docker rmi -f imageID
The above command removes the image. The ‘f’ flag is to remove any traces of the image.
Now we have the image we can create a container to run the application. Let’s say our application was running on port 8989 on local. But now I want the application to run on some different. I should make code changes, create the docker image again, and run it. Nope. We can run the application inside docker on port 8989 but connect the container to a different port on the server.
docker run -d — name mycontainer -p 8080:8989 myimage:tagv1
This will connect the application to run on port 8080 with the name mycontainer. The ‘d’ flag is for running the container in the background.
docker run -it — name myNewContainer -p 8081:8989 myimage:tagv1
This will connect the application to run on port 8081 with the name myNewContainer. As you see I change the port and name of the container because as we are running another container on port 8080 it is busy, so we can’t use that, also the name mycontainer is already taken so we can’t use that either. But the image & tag we use is the same. The ‘it’ tag here will let you run the container in interactive mode so you can check what exactly is happening inside the container via logs.
docker ps -a
The above command gives you a list of all the containers. Each container will have a name & container id. We can pause, unpause, remove a container using the container id or name.
docker pause containerID
docker unpause containerID
docker rm -f containerID
docker pause containerName
docker unpause containerName
docker rm -f containerName
Docker is a very powerful tool making the whole process of running an application on different servers easy as everything is already set up and just a command away from starting the service. We have a lot to explore related to docker.
Thank you