Using Docker Commands
In the previous post, docker installation process on Linux and Windows machines was written. This article is about docker commands, how to run container, pull and push the images from/to docker hub and how to create docker image from from a jar which built with maven.
Before entering the commands, users must be informed about two docker terms. They are Docker Daemon, Docker Cli.
Originally docker consists of two pieces. First one is Docker Daemon. The other one is Docker Client. Docker Daemon is corresponding of Hypervisor in docker ecosystem. It took place of LXC in Linux Kernel. It helps docker containers to work apart from each other. Docker Daemon such an environment that all complex jobs like CPU and RAM restriction, container’s lifecycle, the file system of a container are handled in there.
Docker Cli (Command-Line Interface)
Docker Cli provides the users with appropriate command set to communicate with Docker Daemon. It has some responsibilities such as to download a new image from registry, getting a new container from the image, to stop running container and reboot that, Assinging RAM and CPU to containers and taking commands from users and receiveto Docker Daemon.
First of all, when docker is installed, users can check version control. This informed them about their docker version and this provides to check docker installation complete. To learn docker version, write “docker version” in the terminal.
As shown above, with docker version command shows both server’s (Docker Daemon) and client’s (Docker CLI) information separately. In client side, the system is windows, on the other hand, in server side, it’s Linux because the Daemon works in Linux machine which is worked by Hypervisor in Windows.
After completion of docker setup’s check, let’s pull an image from docker hub via windows terminal and list that image. The image name is hello-world. To get a copy that image write docker pull hello-world in console.
There doesn’t define a version of hello-world image. Therefore, Docker chooses image that has tag is named latest and download the image. That rule works on every untagged docker image. If there is no tag, docker always download image tagged latest.
To see all images in the computer, write docker images. That command lists images on the computer.
To create a container from the hello-world images, give docker run hello-world command.
I If the image is not never pulled before, write docker run <image_name>. This pulls the image, then run the image automatically.
To show running containers, write docker ps. This command lists all running containers.
In image 1, the machine has one running container and it is showing. If your screen is same as image 2, don’t worry. Because of hello-world container works only once and stop it. To list running and finished containers, write docker ps -a.
Finished container can be started again. docker start -a container_id> this command start an over the container. With parameter -ah, when a stopped container starts again, attaches the terminal to the container.
Same command without -a parameter, the container starts again, but the terminal is not attached. To see unattached containers, use docker logs container_id>
To delete a container, docker rm container_id>. However, when this command is used container should not be running the state. To erase a running container, use -f parameter docker rm -f <container_id>
Let’s start a web server. The Nginx web server is used, forexample. This time, pulling nginx image by using the run command. Use docker run -up 8080:80 nginx. After the downloading process, open a web browser and write localhost: 8080
-p parameter helps port forwarding. This means that, when a request comes to host for 8080 port, this will send that request to container’s 80 port, the host is the computer and the container is nginx container in this example.
Open a new terminal because in the current terminal, nginxcontainer is attached and write docker ps
To open a Bash Shell, write docker exec -it <container_id>/bin/bash in the current terminal.
In container bash write ps -ef to list all working process in the container
Nginx container is working, but it is attached terminal. Mostly, this is not a good case. With parameter -d the container can work detached mode and save the terminal to be blocked. Docker ps shows the container that work detached mode and that container can be forced exit with command docker kill <container_id>
How to create a new Docker Image ?
Docker keeps the container’s structure in a text file. This helps to make the process of creating container repeatable and standard. Container file’s name is called Dockerfile. It has to be written like this because docker Clio is case sensitive. Docker Daemon read Dockerfile’s instructions and create an image with these instructions. In this section, a maven program will be built and pushed to dockerhub.
First, a maven project is created. The project’s screen shot is in below. This is the version of before dockerized.
It is just a simple example for understanding how to decrease a project better. This simple web service takes name and surname, then print the information on the screen. When dockerize the project and run the image, this project works like that. To get dockerized, first dockerfile is created inside of the intellij project. Right click on the name of the project in intellij then new->file. Name the file as “Dockerfile”.
Inside of dockerfile is shown below.
Dockerfiles begin with defining image FROM which the build process starts. In return, provide a new image which is to be used for creating docker containers.
The ADD command gets two arguments a source and a destination. This command copies files from source to destination.
ENTRYPOINT argument sets default application that is used everytime a container is created using the image.
EXPOSE command is used to associate a specified port to enable netwoking between the running process inside the container and host.
After dockerfile is finished, open the pom file then create finalName tag between plugins and build tags and give a name.
Thereafter naming the project, open maven project. Under lifecycle directories, choose clean and install options, thenclick run. This helps to compilation of maven project. After the compilation is finished, open terminal inside of intellijproject.
Write console “build docker -f Dockerfile -t <project_name> .” and run that.
After that, docker image starts to be created. When creation process is finished, docker images can be seen in the terminal. Write docker images in a windows terminal to see images.
To push the image to DockerHub, an account is needed. Write console docker login. The system asks a username and password. Fill necessary information then see login successful. After login part, give tag the image with writing docker tag <image id> dockerhubusername/repository_name
help you understand what this container should be used in conjunction with, or what it represents. If this container contains the analysis for a paper, consider using that paper’s DOI or journal-issued serial number; if it’s meant for use with a particular version of a code or the data version control report, that’s a good choice too -whatever will help you understand what this particular image is intended for.
After that, push the image to the repository which was created with the command of docker push dockerhubusername/repository_name
As a result, this paper aims to help people about learning docker commands, push and pull from dockerhub, also help how to run docker image in the environment and these are shown above.