Docker is an Operating System-level virtualization technology that enables you to build, test, and deploy your application. Docker runs your application as a container, like a virtual machine, but more independently from the host operating system and more resource-friendly.
Let’s learn how to install Docker in the CentOS server and how to use basic docker commands. Read this article on how to install Docker in Ubuntu 20.04
Prerequisites
- CentOS 7 server
- A non-root user with sudo privileges
It is recommended that you uninstall older Docker versions before proceeding with the installation.
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
Install Docker on CentOS 7
Installing Docker from the official Docker repository is recommended since the CentOS repository may not have the latest Docker versions. You can use the convenience scripts to install Docker. But it is not recommended for production servers.
$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
Now the Docker repository is added, and you can install the required Docker engine (community edition) and containerd packages as follows.
$ sudo yum install docker-ce docker-ce-cli containerd.io
After successfully installing Docker, you can enable and start the Docker service.
$ sudo systemctl enable docker.service
$ sudo systemctl start docker.service
Make sure the Docker service is started and enabled.
$ systemctl status docker
You should see the output similar as follows.
OUTPUT docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2020-06-15 15:04:20 +0530; 2s ago Docs: https://docs.docker.com Main PID: 8623 (dockerd) Tasks: 20 Memory: 86.9M CGroup: /system.slice/docker.service ├─8623 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Executing Docker Command Without Sudo
By default, sudo privileges are required to execute Docker commands; hence you always have to use sudo with Docker commands. Therefore as a solution, you can add the user to the Docker group, which gets created at the installation automatically.
$ sudo usermod -aG docker $USER
$USER
will add current logged in user to Docker group. If you wish to add different users, add by giving username in the following command.
$ sudo usermod -aG docker username
NOTE: You have to logout from the current session and login again to apply the above changes.
Now you have completed Docker installation and are ready to use Docker. Let’s learn some basic Docker commands.
Using the Docker Command-Line Interface (Docker CLI)
Docker except commands according to the following syntax.
$ docker [option] [command] [arguments]
You can see command help by
$ docker [command] --help
List all available docker commands
$ docker
Docker Images
Docker image is an immutable binary file that contains all the required binaries, libraries, and other essential data for running a specific application. In other words, the Docker image is a snapshot of a Docker image. By default, Docker-Hub is the cloud-based Docker image registry.
You can search for Docker images in Docker-hub by running,
$ docker search image-name
NOTE: Please be cautious when selecting Docker images since anyone can push Docker images to Docker-Hub. Try to use trusted Docker images whenever it is possible.
You can download Docker images by executing the following command.
$ docker pull image-name
To list all the downloaded images, you can run the following command.
$ docker images
To remove the downloaded image,
$ docker rmi image-name
You cant remove a Docker image that is already used for run Docker containers.
Docker Containers
Docker container is a running instance of a Docker image. But there should be a long-term running process to keep the container in the “UP” state. Otherwise, the container will be in the “DOWN” state.
Let’s run our first Docker container, “Hello World”
$ docker run hello-world
OUTPUT Hello from Docker.
This message shows that your installation appears to be working correctly.
We can use -i
and -t options with docker run command to interactively work with container shell. You will be bind to the container shell instead of the previously working user shell of your computer.
$ docker container run -it centos /bin/bash
OUTPUT [[email protected] /]#
Note that 1923a41b62b7 is the container-ID and, it is a unique hexadecimal number for each container.
Press Ctrl + D
or type exit to return to your computer shell.
All the running docker containers can be listed from the following command. You can find container-ID under the ‘CONTAINER ID’ column.
$ docker ps
OUTPUT CONTAINER ID IMAGE COMMAND CREATED a6c32edc1a6dc centos "/bin/bash" 1 hours ago
Use -a option to list all running Docker containers and exited containers.
$ docker ps -a
To stop running Docker container,
$ docker stop container-name/container-id
You can execute the docker command as follows to start/ restart the container.
$ docker start/restart container-name/container-id
You can check container logs by running the following command.
$ docker logs container-name/container-id
Use -f
option to follow log output.
$ docker logs -f container-name/container-id
How to Create a Docker Image from Docker Container
Once you start a docker container from a Docker image, you can add binaries, libraries, or any package. Subsequently, you can create a Docker image from that modified container.
$ docker commit -m "Commit message " -a "Author Name" container-ID repository/image_name
‘-m
’ option is used to give a meaningful commit message, and the ‘-a
‘ option is used for setting the author name who makes the changes. Container-ID should be the specific container that we are going to make a Docker image of.
Then we can push that created Docker image to the Docker-hub repository or private Docker registry after giving the credentials. You can refer to creating a private Docker registry tutorial here.
$ docker push docker_registry-username/docker_image-name
Conclusion
Here we have explained how to install Docker on CentOS 7 and how to use basic Docker commands. There are more advanced topics in the Docker ecosystem.
You can refer to official docker documentation for more information.
We would like to hear your feedback and answer your questions. Please feel free to leave a comment here.
1 comment
[…] How to Install Docker on CentOS 7 […]